@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正在扫描此类,并且令牌不为空。
一些好的家伙要求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"/>
答案 0 :(得分:0)
userService为null是弹簧配置错误,与shiro无关。
您正在将spring注释与xml配置混合使用。
在您的AuthcRealm中,您正尝试自动装配用户服务:
@Autowired
@Lazy(value=false)
@Qualifier(value="userService")
private UserServiceImpl userService;//here I did the injection
然后用xml显示你的弹簧配置。
<bean id=" authRealm" class="com.fentalk.shiro.realm.AuthcRealm">
</bean>
您忘记了xml中的以下spring配置部分以进行自动装配:
<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>