簡體   English   中英

使用Shiro時在Spring上進行注釋,NullPointerException

[英]Annotations on Spring while using Shiro , NullPointerException

@DependsOn(value="userService")
public class AuthcRealm implements Realm {

@Autowired
@Lazy(value=false)
@Qualifier(value="userService")
private UserServiceImpl userService;//here I did the injection

public AuthcRealm() {
    System.out.print("realm constructor");
}

public String getName() {
    return "AuthenticateRealm";
}

public boolean supports(AuthenticationToken token) {
    return token instanceof UsernamePasswordToken;
}

public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    //serServiceImpl userService=new UserServiceImpl();
    User user=new User();
    user.setUsername((String)token.getPrincipal());
    user.setCredential(new String((char[])token.getCredentials()));
    System.out.println((String)token.getPrincipal());
    System.out.println(new String((char[])token.getCredentials()));
    System.out.println(userService.getClass().toString());//at here I got a NullPointerException

    if(!userService.isExist(user)){
        System.out.println("user not exist");
        throw new UnknownAccountException();
    }

    if(!userService.isValid(user)){
        throw new IncorrectCredentialsException();
    }

    return new SimpleAuthenticationInfo(user.getUsername(), user.getCredential(), getName()); 

    }
}

tomcat給我下面的錯誤輸出:

    Serious: Servlet.service() for servlet [spring] in context with path [/Fentalk] threw exception [Request processing failed; nested exception is org.apache.shiro.authc.AuthenticationException: Authentication failed for token submission [org.apache.shiro.authc.UsernamePasswordToken - ADMIN, rememberMe=false].  Possible unexpected error? (Typical or expected login exceptions should extend from AuthenticationException).] with root cause
java.lang.NullPointerException
    at com.fentalk.shiro.realm.AuthcRealm.getAuthenticationInfo(AuthcRealm.java:46)
    at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doSingleRealmAuthentication(ModularRealmAuthenticator.java:180)
    at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doAuthenticate(ModularRealmAuthenticator.java:267)
    at org.apache.shiro.authc.AbstractAuthenticator.authenticate(AbstractAuthenticator.java:198)
    at org.apache.shiro.mgt.AuthenticatingSecurityManager.authenticate(AuthenticatingSecurityManager.java:106)
    at org.apache.shiro.mgt.DefaultSecurityManager.login(DefaultSecurityManager.java:270)
    at org.apache.shiro.subject.support.DelegatingSubject.login(DelegatingSubject.java:256)
    at com.fentalk.ctrl.AccountController.doLogin(AccountController.java:58)

大多是告訴我我的userService無法注入,得到了NullPointerException

令牌首先建立在我的AccountController

我已經確定spring正在掃描此類,並且令牌不為null。

一些好的家伙要求四郎配置,這是

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <property name="realm" ref="authRealm" /> 
</bean>
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
    <property name="securityManager" ref="securityManager" />  
    <property name="loginUrl" value="/home/index.jsp" />  
    <property name="successUrl" value="/main/index" />  
    <property name="filterChainDefinitions">
    </property>
</bean>

<bean id=" authRealm" class="com.fentalk.shiro.realm.AuthcRealm">   
</bean>


<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

userService為null是彈簧配置錯誤,與shiro無關。

您正在將Spring注釋與xml配置混合在一起。

您正在AuthcRealm中嘗試自動連接用戶服務:

@Autowired
@Lazy(value=false)
@Qualifier(value="userService")
private UserServiceImpl userService;//here I did the injection

但是隨后您將在xml中顯示spring配置。

<bean id=" authRealm" class="com.fentalk.shiro.realm.AuthcRealm">   
</bean>

您要么忘記了xml中的以下spring config部分來進行自動裝配:

<context:annotation-config/>

我們將用戶服務顯式放入,例如:

<bean id="userService" class="<yourpackage>.UserServiceImpl">   
   <!-- other injections -->
</bean>
<bean id="authRealm" class="com.fentalk.shiro.realm.AuthcRealm">
    <property name="userService" ref="userService"/>   
</bean>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM