我是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
如何实现我的自定义领域?
发布于 2017-11-07 23:35:52
看起来你扩展了错误的JDBCRealm领域,应该是JdbcRealm` `检查你的导入语句。
https://stackoverflow.com/questions/47131535
复制相似问题