【问题标题】:JdbcRealm in apache shiroapache shiro 中的 JdbcRealm
【发布时间】:2018-04-18 07:07:16
【问题描述】:

我是apache-shiro 的新手。我试图实现我的CustomRealm。但我遇到了错误。

public class MyCustomRealm extends JDBCRealm {

private Map<String, String> credentials = new HashMap<>();
private Map<String, Set<String>> roles = new HashMap<>();
private Map<String, Set<String>> perm = new HashMap<>();

{
    credentials.put("user", "password");
    credentials.put("user2", "password2");
    credentials.put("user3", "password3");

}

protected AuthenticationInfo doGetAuthenticationInfo( AuthenticationToken token ) throws AuthenticationException
{

    UsernamePasswordToken uToken = (UsernamePasswordToken) token;

    if( uToken.getUsername() == null || uToken.getUsername().isEmpty()
            || !credentials.containsKey(uToken.getUsername()) )
    {
        throw new UnknownAccountException("username not found!");
    }

    return new SimpleAuthenticationInfo(uToken.getUsername(), credentials.get(uToken.getUsername()), getName());
}
}

我的SecurityManager 实现是

@Component
public class CustomSecurityManager {
public Subject getSubject()
{
    Realm realm = (Realm) new MyCustomRealm();
    SecurityManager securityManager = new DefaultSecurityManager(realm);
    SecurityUtils.setSecurityManager(securityManager);
    return SecurityUtils.getSubject();
}

}

但是当我运行应用程序时,它会抛出错误声明Cannot cast MyCustomRealm to shiro.Realm

如何实现我的自定义领域?

【问题讨论】:

    标签: spring-boot shiro


    【解决方案1】:

    看起来您扩展了错误的JDBCRealm 领域,应该是 JdbcRealm` 检查您的导入语句。

    【讨论】:

      猜你喜欢
      • 2015-10-02
      • 2012-08-03
      • 2014-12-01
      • 2015-01-01
      • 2011-11-19
      • 2017-09-20
      • 2016-05-07
      • 2017-01-04
      • 2016-06-01
      相关资源
      最近更新 更多