繁体   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