|
@@ -2,6 +2,7 @@ package com.xingxi.framework.shiro.realm;
|
|
|
|
|
|
|
|
import java.util.HashSet;
|
|
import java.util.HashSet;
|
|
|
import java.util.Set;
|
|
import java.util.Set;
|
|
|
|
|
+
|
|
|
import org.apache.shiro.authc.AuthenticationException;
|
|
import org.apache.shiro.authc.AuthenticationException;
|
|
|
import org.apache.shiro.authc.AuthenticationInfo;
|
|
import org.apache.shiro.authc.AuthenticationInfo;
|
|
|
import org.apache.shiro.authc.AuthenticationToken;
|
|
import org.apache.shiro.authc.AuthenticationToken;
|
|
@@ -34,11 +35,10 @@ import com.xingxi.system.service.ISysRoleService;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 自定义Realm 处理登录 权限
|
|
* 自定义Realm 处理登录 权限
|
|
|
- *
|
|
|
|
|
|
|
+ *
|
|
|
* @author ruoyi
|
|
* @author ruoyi
|
|
|
*/
|
|
*/
|
|
|
-public class UserRealm extends AuthorizingRealm
|
|
|
|
|
-{
|
|
|
|
|
|
|
+public class UserRealm extends AuthorizingRealm {
|
|
|
private static final Logger log = LoggerFactory.getLogger(UserRealm.class);
|
|
private static final Logger log = LoggerFactory.getLogger(UserRealm.class);
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
@@ -54,8 +54,7 @@ public class UserRealm extends AuthorizingRealm
|
|
|
* 授权
|
|
* 授权
|
|
|
*/
|
|
*/
|
|
|
@Override
|
|
@Override
|
|
|
- protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
|
|
|
SysUser user = ShiroUtils.getSysUser();
|
|
SysUser user = ShiroUtils.getSysUser();
|
|
|
// 角色列表
|
|
// 角色列表
|
|
|
Set<String> roles = new HashSet<String>();
|
|
Set<String> roles = new HashSet<String>();
|
|
@@ -63,13 +62,10 @@ public class UserRealm extends AuthorizingRealm
|
|
|
Set<String> menus = new HashSet<String>();
|
|
Set<String> menus = new HashSet<String>();
|
|
|
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
|
|
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
|
|
|
// 管理员拥有所有权限
|
|
// 管理员拥有所有权限
|
|
|
- if (user.isAdmin())
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (user.isAdmin()) {
|
|
|
info.addRole("admin");
|
|
info.addRole("admin");
|
|
|
info.addStringPermission("*:*:*");
|
|
info.addStringPermission("*:*:*");
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ } else {
|
|
|
roles = roleService.selectRoleKeys(user.getUserId());
|
|
roles = roleService.selectRoleKeys(user.getUserId());
|
|
|
menus = menuService.selectPermsByUserId(user.getUserId());
|
|
menus = menuService.selectPermsByUserId(user.getUserId());
|
|
|
// 角色加入AuthorizationInfo认证对象
|
|
// 角色加入AuthorizationInfo认证对象
|
|
@@ -84,47 +80,30 @@ public class UserRealm extends AuthorizingRealm
|
|
|
* 登录认证
|
|
* 登录认证
|
|
|
*/
|
|
*/
|
|
|
@Override
|
|
@Override
|
|
|
- protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
|
|
|
UsernamePasswordToken upToken = (UsernamePasswordToken) token;
|
|
UsernamePasswordToken upToken = (UsernamePasswordToken) token;
|
|
|
String username = upToken.getUsername();
|
|
String username = upToken.getUsername();
|
|
|
String password = "";
|
|
String password = "";
|
|
|
- if (upToken.getPassword() != null)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (upToken.getPassword() != null) {
|
|
|
password = new String(upToken.getPassword());
|
|
password = new String(upToken.getPassword());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
SysUser user = null;
|
|
SysUser user = null;
|
|
|
- try
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ try {
|
|
|
user = loginService.login(username, password);
|
|
user = loginService.login(username, password);
|
|
|
- }
|
|
|
|
|
- catch (CaptchaException e)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ } catch (CaptchaException e) {
|
|
|
throw new AuthenticationException(e.getMessage(), e);
|
|
throw new AuthenticationException(e.getMessage(), e);
|
|
|
- }
|
|
|
|
|
- catch (UserNotExistsException e)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ } catch (UserNotExistsException e) {
|
|
|
throw new UnknownAccountException(e.getMessage(), e);
|
|
throw new UnknownAccountException(e.getMessage(), e);
|
|
|
- }
|
|
|
|
|
- catch (UserPasswordNotMatchException e)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ } catch (UserPasswordNotMatchException e) {
|
|
|
throw new IncorrectCredentialsException(e.getMessage(), e);
|
|
throw new IncorrectCredentialsException(e.getMessage(), e);
|
|
|
- }
|
|
|
|
|
- catch (UserPasswordRetryLimitExceedException e)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ } catch (UserPasswordRetryLimitExceedException e) {
|
|
|
throw new ExcessiveAttemptsException(e.getMessage(), e);
|
|
throw new ExcessiveAttemptsException(e.getMessage(), e);
|
|
|
- }
|
|
|
|
|
- catch (UserBlockedException e)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ } catch (UserBlockedException e) {
|
|
|
throw new LockedAccountException(e.getMessage(), e);
|
|
throw new LockedAccountException(e.getMessage(), e);
|
|
|
- }
|
|
|
|
|
- catch (RoleBlockedException e)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ } catch (RoleBlockedException e) {
|
|
|
throw new LockedAccountException(e.getMessage(), e);
|
|
throw new LockedAccountException(e.getMessage(), e);
|
|
|
- }
|
|
|
|
|
- catch (Exception e)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
log.info("对用户[" + username + "]进行登录验证..验证未通过{}", e.getMessage());
|
|
log.info("对用户[" + username + "]进行登录验证..验证未通过{}", e.getMessage());
|
|
|
throw new AuthenticationException(e.getMessage(), e);
|
|
throw new AuthenticationException(e.getMessage(), e);
|
|
|
}
|
|
}
|
|
@@ -135,8 +114,7 @@ public class UserRealm extends AuthorizingRealm
|
|
|
/**
|
|
/**
|
|
|
* 清理指定用户授权信息缓存
|
|
* 清理指定用户授权信息缓存
|
|
|
*/
|
|
*/
|
|
|
- public void clearCachedAuthorizationInfo(Object principal)
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ public void clearCachedAuthorizationInfo(Object principal) {
|
|
|
SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());
|
|
SimplePrincipalCollection principals = new SimplePrincipalCollection(principal, getName());
|
|
|
this.clearCachedAuthorizationInfo(principals);
|
|
this.clearCachedAuthorizationInfo(principals);
|
|
|
}
|
|
}
|
|
@@ -144,13 +122,10 @@ public class UserRealm extends AuthorizingRealm
|
|
|
/**
|
|
/**
|
|
|
* 清理所有用户授权信息缓存
|
|
* 清理所有用户授权信息缓存
|
|
|
*/
|
|
*/
|
|
|
- public void clearAllCachedAuthorizationInfo()
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ public void clearAllCachedAuthorizationInfo() {
|
|
|
Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();
|
|
Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();
|
|
|
- if (cache != null)
|
|
|
|
|
- {
|
|
|
|
|
- for (Object key : cache.keys())
|
|
|
|
|
- {
|
|
|
|
|
|
|
+ if (cache != null) {
|
|
|
|
|
+ for (Object key : cache.keys()) {
|
|
|
cache.remove(key);
|
|
cache.remove(key);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|