java - 使用 Shiro 、 NullPointerException 时对 Spring 进行注释

标签 java spring annotations shiro

@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 无法注入(inject),得到一个 NullPointerException

token 首先在我的 AccountController 中构建。

我已经确定 spring 正在扫描这个类,并且 token 不为空。

有好心人求shiro配置,这里有

<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是spring配置错误,与shiro无关。

您正在将 spring 注释与 xml 配置混合在一起。

在您的 AuthcRealm 中,您尝试 Autowiring 用户服务:

@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 配置部分来进行 Autowiring :

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

关于java - 使用 Shiro 、 NullPointerException 时对 Spring 进行注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25114907/

相关文章:

java - ORA-01654 : unable to extend index

java - 通过基于 Java Spring 框架构建的 RESTful API 服务器对客户端进行身份验证

spring - 调用 Spring RestController 时使用 Spring 的 RestTemplate 转义 URL 变量的正确方法是什么?

java - 使用计时器在 JFreeChart 上闪烁 XYImageAnnotation

php - 使用 Doctrine 2 Annotation reader 进行自定义注释

iphone - 如何在 annotationView 上找到按钮以在 iphone 中执行操作?

Java InputStreamReader 不变的停顿

java - 命令行参数

java - For 循环使用参数 start 和 end 搜索字符的出现

java - 从 Android 调用使​​用 Spring Security 保护的 REST Web 服务