|
@@ -30,69 +30,70 @@ import com.xt.js.gkaq.frame.system.FrameParam;
|
|
|
*
|
|
|
*/
|
|
|
public class GkaqAuthorizingRealm extends AuthorizingRealm {
|
|
|
- @Autowired
|
|
|
- private UserService userService;
|
|
|
- @Autowired
|
|
|
- private RoleService roleService;
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+ @Autowired
|
|
|
+ private RoleService roleService;
|
|
|
|
|
|
- /**
|
|
|
- * 清除所有用户授权信息缓存.
|
|
|
- */
|
|
|
- public void clearAllCachedAuthorizationInfo() {
|
|
|
- Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();
|
|
|
- if (cache != null) {
|
|
|
- for (Object key : cache.keys()) {
|
|
|
- cache.remove(key);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 清除所有用户授权信息缓存.
|
|
|
+ */
|
|
|
+ public void clearAllCachedAuthorizationInfo() {
|
|
|
+ Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();
|
|
|
+ if (cache != null) {
|
|
|
+ for (Object key : cache.keys()) {
|
|
|
+ cache.remove(key);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 更新用户授权信息缓存.
|
|
|
- */
|
|
|
- public void clearCachedAuthorizationInfo(UserModel user) {
|
|
|
- SimplePrincipalCollection principals = new SimplePrincipalCollection(user, getName());
|
|
|
- clearCachedAuthorizationInfo(principals);
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 更新用户授权信息缓存.
|
|
|
+ */
|
|
|
+ public void clearCachedAuthorizationInfo(UserModel user) {
|
|
|
+ SimplePrincipalCollection principals = new SimplePrincipalCollection(user, getName());
|
|
|
+ clearCachedAuthorizationInfo(principals);
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 认证回调函数, 登录时调用.
|
|
|
- */
|
|
|
- @Override
|
|
|
- protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
|
|
|
- throws AuthenticationException {
|
|
|
- UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
|
|
|
- UserModel user = userService.findByLoginName(token.getUsername());
|
|
|
- if (user != null) {
|
|
|
- return new SimpleAuthenticationInfo(user, user.getLoginPassword(), getName());
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 认证回调函数, 登录时调用.
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken)
|
|
|
+ throws AuthenticationException {
|
|
|
+ UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
|
|
|
+ UserModel user = userService.findByLoginName(token.getUsername());
|
|
|
+ if (user != null) { return new SimpleAuthenticationInfo(user, user.getLoginPassword(), getName()); }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用.
|
|
|
- */
|
|
|
- @Override
|
|
|
- protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
|
|
|
- UserModel loginUser = (UserModel) principals.fromRealm(getName()).iterator().next();
|
|
|
- // 首先从session中获取权限
|
|
|
- Session s = SecurityUtils.getSubject().getSession();
|
|
|
- // 缓存中不存在,则从数据库查询
|
|
|
- UserModel user = userService.findById(loginUser.getId());
|
|
|
- if (user != null) {
|
|
|
- SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
|
|
|
- List<RoleModel> roles = roleService.findByUserId(user.getId());
|
|
|
- for (RoleModel role : roles) {
|
|
|
- info.addRole(role.getCode());
|
|
|
- }
|
|
|
- // List<String> perms =
|
|
|
- // securityMgr.getUserService().findUserPermission(user.getId());
|
|
|
- // for (String p : perms) {
|
|
|
- // info.addStringPermission(SystemParam.getAppName() + ":" + p);
|
|
|
- // }
|
|
|
- s.setAttribute(FrameParam.SESSION_KEY_AUTH, info);
|
|
|
- return info;
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用.
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
|
|
|
+ UserModel loginUser = (UserModel) principals.fromRealm(getName()).iterator().next();
|
|
|
+ // 首先从session中获取权限
|
|
|
+ Session s = SecurityUtils.getSubject().getSession();
|
|
|
+ // 缓存中不存在,则从数据库查询
|
|
|
+ UserModel user = userService.findById(loginUser.getId());
|
|
|
+ if (user != null) {
|
|
|
+ SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
|
|
|
+ List<RoleModel> roles = roleService.findByUserId(user.getId());
|
|
|
+ for (RoleModel role : roles) {
|
|
|
+ info.addRole(role.getCode());
|
|
|
+ }
|
|
|
+ List<String> menuPerms = userService.selectAuthMenuByUserid(user.getId());
|
|
|
+ for (String p : menuPerms) {
|
|
|
+ info.addStringPermission(p);
|
|
|
+ }
|
|
|
+ List<String> buttonPerms = userService.selectAuthButtonByUserid(user.getId());
|
|
|
+ for (String p : buttonPerms) {
|
|
|
+ info.addStringPermission(p);
|
|
|
+ }
|
|
|
+ s.setAttribute(FrameParam.SESSION_KEY_AUTH, info);
|
|
|
+ return info;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|