https://blog.csdn.net/u014745069/article/details/100812556
https://blog.csdn.net/yankun01/article/details/78269222
文件
//文件
UserRealm.java (com.ruoyi.framework.shiro.realm.UserRealm)
ShiroConfig.java (com.ruoyi.framework.config)
ShiroUtils.java (com.ruoyi.framework.util)
可直接用对象与方法 SecurityUtils 和 SecurityUtils.getSubject()
//1.SecurityUtils
Subject subject = SecurityUtils.getSubject()
汪方法汇总
//1.登录方法 三步骤 a.获取subject b.获取登录信息创建令牌 c.subject.login(token)
@PostMapping("/login")
public AjaxResult ajaxLogin(String username, String password, Boolean rememberMe)
{
UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
Subject subject = SecurityUtils.getSubject();
subject.login(token);
}
//2.ShiroUtils.getSysUser
public static SysUser getSysUser()
{
SysUser user = null;
Object obj = getSubject().getPrincipal();
if (StringUtils.isNotNull(obj))
{
user = new SysUser();
BeanUtils.copyBeanProp(user, obj);
}
return user;
}
//3.自定义Realm类 a.继承AuthorizingRealm b.实现认证和授权方法
public class UserRealm extends AuthorizingRealm{
@Override //授权
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0){
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
return info;
}
@Override //登录认证
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException{
SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, password, getName());
return info;
}
// 清理缓存权限
public void clearCachedAuthorizationInfo()
{
this.clearCachedAuthorizationInfo(SecurityUtils.getSubject().getPrincipals());
}
}