shiro+spring+_hibernate shiro加入注解后 service層service對象注入失敗 報java.lang.NullPointerException [問題點


shiro加入注解后 service層service對象注入失敗 報java.lang.NullPointerException [問題點

使用shiro進行權限控制 開啟shiro框架注解支持

<property name="filterChainDefinitions">
            <value>
                /css/** = anon
                /js/** = anon
                /images/** = anon
                /validatecode.jsp* = anon
                /login.jsp = anon
                /userAction_login.action = anon
            <!-- perms過濾器檢查當前登錄用戶是否具有取派員列表查詢權限 -->
            <!-- 如果說當前登錄用戶具有這個權限 它就能夠訪問成功 沒有這個權限會跳轉到unauthorized.jsp -->
                /page_base_staff.action = perms["staff-list"]
                /page_base_region.action = perms["region-list"]
            <!-- 一切的請求都會經過這個過濾器處理 -->
            <!-- 這個過濾器用來檢查你是否已經認證過了 就是說你是否已經登錄過了 -->    
                /* = authc
            </value>
        </property>
    </bean>
    
    <!-- 注冊安全管理器對象 -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="bosRealm"/>
    </bean>
    
    <!-- 注冊realm -->
    <bean id="bosRealm" class="com.itheima.bos.realm.BOSRealm">
    </bean>
    
    <!-- 開啟shiro框架注解支持 -->
    <bean id="defaultAdvisorAutoProxyCreator"
        class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
            <!-- 必須使用cglib方式為Action對象創建代理對象 -->
        <property name="proxyTargetClass" value="true"/>
    </bean>
    
    <!-- 配置shiro框架提供的切面類,用於創建代理對象 -->
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager"></property>
    </bean>

realm簡單授權機制

public class BOSRealm extends AuthorizingRealm{

    @Autowired
    private IUserDao userDao;
    @Autowired(required=true)
    private IStaffService staffService;
    
    public void setStaffService(IStaffService staffService) {
        this.staffService = staffService;
    }
    //授權
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.addStringPermission("staff-list");//硬編碼了  看看當前登錄用戶是誰  他所實際對應的權限查出來
        //然后循環調用這個方法
        return info;
    }
    //認證  如果返回null說明認證失敗了
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("自定義認證方法調用了...");
        //根據用戶名查詢數據庫中的密碼
        //認證邏輯是定死的不能更改
        UsernamePasswordToken passwordToken = (UsernamePasswordToken) token;
        String username = passwordToken.getUsername();
        User user = userDao.findUserByusername(username);
        //框架負責對比數據庫中的密碼和頁面輸入的密碼是否一致
        if(user==null){
            //頁面錄入用戶名不存在
            return null;
        }
        //第一個參數放USER 就是為了綁定該線程  最終放到session里面
        AuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), this.getName());
        return info;
    }
}

XXXaction 注入service時候 service為null

報錯java.lang.NullPointerException
at cn.itheima.bos.web.action.StaffAction.pageQuery(StaffAction.java:46)
at cn.itheima.bos.web.action.StaffAction$$FastClassBySpringCGLIB$$6a8175fc.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)

快速解決方法

目標還沒有深入了解shiro框架的運行機制

解決方式

spring配置文件

  <bean name="staffAction" class="cn.itheima.bos.web.action.StaffAction">
        <property name="staffService" ref="staffService"></property>
    </bean>  
  Service加入注解

@Service("staffService")
@Transactional
public class IStaffServiceImpl implements IStaffService {}

action文件

@Autowired(required=true)
    private IStaffService staffService;
    public IStaffService getStaffService() {
        return staffService;
    }

    public void setStaffService(IStaffService staffService) {
        this.staffService = staffService;
    }

即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM