我是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
如何实现自定义领域?
答案 0 :(得分:0)
看起来你正在扩展错误的JDBCRealm
域,它应该是JdbcRealm`检查你的import语句。