自动装配在 Apache Shiro 自定义领域中不起作用 class
Autowiring is not working in Apache Shiro custom Realm class
目前我在 spring 引导项目中使用 apache shiro 身份验证。我为 Cassandra DB 编写了 Custom Realm。在提交登录详细信息时自动装配 class 内部领域对象 returns 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();
}
我的应用领域Class:
@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;
}
是否遗漏了任何注释?
您似乎配置了不匹配的注释。您的 ApplicationRealm.java 不应该有 @Configuration 注释。 @Component 对于这个客户领域来说已经足够了。
/*@Configuration*/
@ComponentScan("com.scm.auth")
public class ApplicationRealm extends AuthorizingRealm{
/**/
}
目前我在 spring 引导项目中使用 apache shiro 身份验证。我为 Cassandra DB 编写了 Custom Realm。在提交登录详细信息时自动装配 class 内部领域对象 returns 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();
}
我的应用领域Class:
@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;
}
是否遗漏了任何注释?
您似乎配置了不匹配的注释。您的 ApplicationRealm.java 不应该有 @Configuration 注释。 @Component 对于这个客户领域来说已经足够了。
/*@Configuration*/
@ComponentScan("com.scm.auth")
public class ApplicationRealm extends AuthorizingRealm{
/**/
}