【发布时间】:2018-04-03 13:55:45
【问题描述】:
目前我在我的 spring boot 项目中使用 apache shiro 身份验证。我为 Cassandra DB 编写了自定义领域。在提交登录详细信息时,自动装配领域对象内的类返回 null。我的应用程序配置(使用了@component 注释):
@Bean(name = "realm")
@DependsOn("lifecycleBeanPostProcessor")
public ApplicationRealm realm() {
ApplicationRealm realm = new ApplicationRealm();
realm.init();
return realm;
}
@Bean
public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
return new LifecycleBeanPostProcessor();
}
我的应用领域类:
@Configuration
@ComponentScan("com.scm.auth")
public class ApplicationRealm extends AuthorizingRealm {
@Autowired
IAuthRepository authRepo;
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
Set<String> roles = new HashSet<String>();
try {
roles.add("admin");
} catch (Exception rEx) {
throw new AuthorizationException(rEx);
}
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roles);
info.setRoles(roles);
return info;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
SimpleAuthenticationInfo info = null;
UsernamePasswordToken upToken = (UsernamePasswordToken) token;
User user = authRepo.findByUserName(upToken.getUsername(), true); // my model class
try {
if (user.getCurrentPwd().equals(upToken.getPassword())) {
info = new SimpleAuthenticationInfo(user, user.getCurrentPwd(), getName());
} else {
throw new AuthenticationException("Login name [" + upToken.getUsername() + "] not found!");
}
} catch (Exception idEx) {
throw new AuthenticationException(idEx);
}
return info;
}
是否遗漏了任何注释?
【问题讨论】:
标签: java spring-boot spring-security annotations shiro