【发布时间】: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