Skip to content

Commit

Permalink
将用户授权信息缓存放入Session优化可能出现用户缓存混乱问题。
Browse files Browse the repository at this point in the history
  • Loading branch information
think-gem committed Nov 14, 2016
1 parent e4124ac commit 82b9ecc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public class SystemAuthorizingRealm extends AuthorizingRealm {
private Logger logger = LoggerFactory.getLogger(getClass());

private SystemService systemService;

public SystemAuthorizingRealm() {
this.setCachingEnabled(false);
}

/**
* 认证回调函数, 登录时调用
Expand Down Expand Up @@ -86,6 +90,28 @@ protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcTo
return null;
}
}

/**
* 获取权限授权信息,如果缓存中存在,则直接从缓存中获取,否则就重新获取, 登录成功后调用
*/
protected AuthorizationInfo getAuthorizationInfo(PrincipalCollection principals) {
if (principals == null) {
return null;
}

AuthorizationInfo info = null;

info = (AuthorizationInfo)UserUtils.getCache(UserUtils.CACHE_AUTH_INFO);

if (info == null) {
info = doGetAuthorizationInfo(principals);
if (info != null) {
UserUtils.putCache(UserUtils.CACHE_AUTH_INFO, info);
}
}

return info;
}

/**
* 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public class UserUtils {
public static final String USER_CACHE_ID_ = "id_";
public static final String USER_CACHE_LOGIN_NAME_ = "ln";
public static final String USER_CACHE_LIST_BY_OFFICE_ID_ = "oid_";


public static final String CACHE_AUTH_INFO = "authInfo";
public static final String CACHE_ROLE_LIST = "roleList";
public static final String CACHE_MENU_LIST = "menuList";
public static final String CACHE_AREA_LIST = "areaList";
Expand Down Expand Up @@ -92,6 +93,7 @@ public static User getByLoginName(String loginName){
* 清除当前用户缓存
*/
public static void clearCache(){
removeCache(CACHE_AUTH_INFO);
removeCache(CACHE_ROLE_LIST);
removeCache(CACHE_MENU_LIST);
removeCache(CACHE_AREA_LIST);
Expand Down

0 comments on commit 82b9ecc

Please sign in to comment.