JdbcRealm在apache shiro中

时间:2017-11-06 07:07:30

标签: spring-boot shiro

我是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

如何实现自定义领域?

1 个答案:

答案 0 :(得分:0)

看起来你正在扩展错误的JDBCRealm域,它应该是JdbcRealm`检查你的import语句。