Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shiro-redis从2.4升级到3.0后报错 #56

Closed
yunzhao824 opened this issue May 30, 2018 · 8 comments
Closed

shiro-redis从2.4升级到3.0后报错 #56

yunzhao824 opened this issue May 30, 2018 · 8 comments

Comments

@yunzhao824
Copy link

错误信息如下
org.crazycake.shiro.exception.PrincipalInstanceException: Principal must implement org.crazycake.shiro.AuthCachePrincipal.
shiro-redis will get the key for store authorization object in Redis from org.crazycake.shiro.AuthCachePrincipal
So please use AuthCachePrincipal to tell shiro-redis how to get the cache key
For example: There is a class UserInfo which implements org.crazycake.shiro.AuthCachePrincipal. You can use this class to initial SimpleAuthenticationInfo like this:
UserInfo userInfo = new userInfo();
new SimpleAuthenticationInfo(userInfo, "123456", "realm1")
at org.crazycake.shiro.RedisCache.getRedisKeyFromPrincipalCollection(RedisCache.java:134) ~[shiro-redis-3.0.0.jar:na]
at org.crazycake.shiro.RedisCache.getStringRedisKey(RedisCache.java:123) ~[shiro-redis-3.0.0.jar:na]
at org.crazycake.shiro.RedisCache.getRedisCacheKey(RedisCache.java:112) ~[shiro-redis-3.0.0.jar:na]
at org.crazycake.shiro.RedisCache.get(RedisCache.java:61) ~[shiro-redis-3.0.0.jar:na]
at org.apache.shiro.realm.AuthorizingRealm.getAuthorizationInfo(AuthorizingRealm.java:328) ~[shiro-core-1.3.2.jar:1.3.2]
at org.apache.shiro.realm.AuthorizingRealm.isPermitted(AuthorizingRealm.java:462) ~[shiro-core-1.3.2.jar:1.3.2]
at org.apache.shiro.realm.AuthorizingRealm.isPermitted(AuthorizingRealm.java:458) ~[shiro-core-1.3.2.jar:1.3.2]
at org.apache.shiro.authz.ModularRealmAuthorizer.isPermitted(ModularRealmAuthorizer.java:223) ~[shiro-core-1.3.2.jar:1.3.2]

@alexxiyang
Copy link
Owner

alexxiyang commented May 30, 2018 via email

@yunzhao824
Copy link
Author

yunzhao824 commented May 30, 2018 via email

@alexxiyang
Copy link
Owner

alexxiyang commented May 30, 2018 via email

@alexxiyang
Copy link
Owner

alexxiyang commented May 30, 2018 via email

@yunzhao824
Copy link
Author

按照你的文档改了,仍然报同样的错。
代码如下:
//登录验证
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
String userCode = (String) authenticationToken.getPrincipal();
String password = new String((char[]) authenticationToken.getCredentials());

    // 查询用户信息
    SysUserDTO sysUser = sysApiService.getLoginUserByUserCode(userCode);

    if (null == sysUser) {
        throw new UnknownAccountException();
    }
    if (!sysUser.getPassword().equals(MD5Util.MD5(password + sysUser.getSalt()))) {
        throw new IncorrectCredentialsException();
    }
    if (sysUser.getUseable() == 0) {
        throw new LockedAccountException();
    }

    UserInfo userInfo = new UserInfo();
    BeanUtils.copyProperties(sysUser, userInfo);
    
    AuthenticationInfo authcInfo = new SimpleAuthenticationInfo(userInfo, password, getName());

UserInfo.java
public class UserInfo implements Serializable, AuthCachePrincipal {
private static final long serialVersionUID = -6477843453976551075L;

private String id;

private String userCode;

private String userName;

...

@Override
public String getAuthCacheKey() {
    return this.getUserCode();
}

}

@alexxiyang
Copy link
Owner

可否断点调试 RedisCache 类 line 133:

if (!(principalCollection.getPrimaryPrincipal() instanceof AuthCachePrincipal)) {
            throw new PrincipalInstanceException();
        }

观察此处 principalCollection.getPrimaryPrincipal() 取出的对象是否是UserInfo对象?

@alexxiyang
Copy link
Owner

Any updates?

@alexxiyang
Copy link
Owner

请升级至3.1.0,3.1.0取消了AuthCachePrincipal接口,转而使用反射来获取auth的redis对象id,请在 cacheManager.principalIdFieldName 中配置id字段,具体请参考README.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants