简体   繁体   中英

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 gives me error output below:

    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)

Mostly telling me my userService failed to inject , getting a NullPointerException

The token is firstly build in my AccountController .

I already made sure spring is scanning this class , and the token is not null.

Some good fellas ask for shiro configuration, here it is

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

The userService being null is a spring misconfiguration, which has nothing to do with shiro.

You are mixing spring annotations with xml configuration.

In your AuthcRealm you are trying to autowire a user service:

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

but then you show your spring configuration in xml.

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

You either forgot the following spring config part in your xml to do the autowiring:

<context:annotation-config/>

Our you put the userservice in explicitly, something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM