技术漫谈shiro校验框架
FANSEAshiro
AuthorizingRealm
配置用户角色权限
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
@Slf4j public class UserRealm extends AuthorizingRealm {
@Autowired private ISysRoleService roleService;
@Autowired private SysMenuService menuService;
@Autowired private LoginService loginService;
@Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { SysUser user = ShiroUtils.getSysUser(); SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); boolean isRoot = ShiroUtils.isRoot(); if (isRoot) { info.addRole(ShiroUtils.ROOT); info.addStringPermission("*:*:*"); } else { Set<String> roles = roleService.listRoleKeys(user.getUserId()); Set<String> menus = menuService.listMenusCodeByUserId(user.getUserId());
info.setRoles(roles); info.setStringPermissions(menus); } return info; }
public void clearCachedAuthorizationInfo() { this.clearCachedAuthorizationInfo(SecurityUtils.getSubject().getPrincipals()); } }
|
用户认证
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
@Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { if (token instanceof JwtToken){ JwtToken jwtToken = (JwtToken) token; SysUser user=loginService.loginByJwtToken(jwtToken); return new SimpleAuthenticationInfo(user,new char[]{}, getName()); } UsernamePasswordToken upToken = (UsernamePasswordToken) token; String username = upToken.getUsername(); String password = ""; if (upToken.getPassword() != null) { password = new String(upToken.getPassword()); } SysUser user = null; try { String host = upToken.getHost(); if(StringUtils.isBlank(host)|| Objects.equals(EnumHost.PC.getCode(),host)){ user = loginService.login(username, password); } if(Objects.equals(EnumHost.MOBILE.getCode(),host)){ String phone=username; user = loginService.loginByPhone(phone); }
} catch (UserNotExistsException e) { throw new UnknownAccountException(e.getMessage(), e); } catch (UserBlockException e) { throw new LockedAccountException(e.getMessage(), e); } catch (UserPasswordNotMatchException e) { throw new IncorrectCredentialsException(e.getMessage(), e); } catch (UserPasswordRetryLimitExceedException e) { throw new ExcessiveAttemptsException(e.getMessage(), e); } catch (UserDeleteException e) { throw new DisabledAccountException(e.getMessage(), e); } return new SimpleAuthenticationInfo(user, password, getName()); }
|
会话保存
配置Session,以redis为介质,登录返回给浏览器cookie,浏览器请求携带cookie校验登录状态
登录逻辑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| @PostMapping("/login") @ResponseBody public AjaxResult ajaxLogin(String username, String password, String host) { if (StringUtils.isBlank(password)) { password = username; } UsernamePasswordToken token = new UsernamePasswordToken(username, password, host); Subject subject = SecurityUtils.getSubject(); try { subject.login(token); } catch (AuthenticationException e) { String msg = "用户名或密码错误"; if (e.getMessage() != null) { msg = e.getMessage(); } return new AjaxResult(AjaxResult.Type.WARN, msg); } if (!licLicenseService.verifyLicense()) { return new AjaxResult(AjaxResult.Type.WARN, "非授权用户,请联系开发商"); } return new AjaxResult(AjaxResult.Type.SUCCESS, "登陆成功"); }
|