【问题标题】:Autowiring is not working in Apache Shiro custom Realm class自动装配在 Apache Shiro 自定义领域类中不起作用
【发布时间】: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


    【解决方案1】:

    似乎您配置了不匹配的注释。您的 ApplicationRealm.java 不应该有 @Configuration 注释。 @Component 对于这个 Custome Realm 来说已经足够了。

     /*@Configuration*/
     @ComponentScan("com.scm.auth")
     public class ApplicationRealm extends AuthorizingRealm{
     /**/
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-19
      • 2016-03-12
      • 2012-09-17
      • 2012-03-01
      • 2018-03-14
      • 2015-06-06
      • 2016-08-13
      • 2012-09-22
      相关资源
      最近更新 更多