AuthorizingRealm中有两个需要被重写的方法,分别是
doGetAuthenticationInfo() //验证功能
和 doGetAuthorizationInfo() //授权功能
在login登录方法中,使用login()方法触发自定义的 myRealm.doGetAuthenticationInfo()
*方法,
/*登录方法*/
public void login(){
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken usernamePasswordToken =
new UsernamePasswordToken(user.getUsername(),user.getPassword());
subject.login(usernamePasswordToken);
}
/*myRealm extends AuthorizingRealm*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken authenticationToken) throws AuthenticationException
{
//加这一步的目的是在Post请求的时候会先进认证,然后在到请求
if (authenticationToken.getPrincipal() == null) {
return null;
}
//获取用户信息
String name = authenticationToken.getPrincipal().toString();
User user = userService.readByUsername(name);
if (user == null) {
//这里返回后会报出对应异常
return null;
} else {
Session session = SecurityUtils.getSubject().getSession();
//将用户id存储到session中
session.setAttribute("uid",user.getId());
//这里验证authenticationToken和simpleAuthenticationInfo的信息
return new SimpleAuthenticationInfo(name, user.getPassword(), getName());
}
}
然后我在授权方法中打印文字,发现完全没调用到授权doGetAuthorizationInfo()
方法
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection)
{
System.out.println("用户授权中...");
//获取登录用户名
String loginId = (String) principalCollection.getPrimaryPrincipal();
//根据用户名去数据库查询用户信息
User user = userService.readByUsername(loginId);
//添加角色和权限
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//用户类型
simpleAuthorizationInfo.addRole(user.getType().toString());
//用户权限
Set<String> permissions = new HashSet<>() {{
add("shiroRealm >> 权限");
}};
simpleAuthorizationInfo.setStringPermissions(permissions);
SecurityUtils.getSubject().getSession().setAttribute("userType",user.getType());
SecurityUtils.getSubject().getSession().setAttribute("permissions",permissions);
return simpleAuthorizationInfo;
}
后面根据百度和自己的猜测,应该是初始化的时候,不会走这个方法,只有要用到权限功能时,才会进这个方法。
一、 在控制器上加 @RequiresRoles
或者 @RequiresPermissions
注解,这两个注解功能是用于验证用户角色、权限是否足够进入这个方法,权限不足时会报错。不过加注解的方法若没调用到,也不会进入到doGetAuthorizationInfo()
,所以若是在授权中存放session了,那最好在登录成功后跳转的页面上加这两个注解。
@PostMapping("/test")
@RequiresRoles("0")
public String test(){
Integer userType = SessionUtil.getUserType();
System.out.println(userType);
return "成功访问,用户权限:"+userType;
}
@PostMapping("/test2")
public String test2(){
Integer userType = SessionUtil.getUserType();
System.out.println(userType);
return "成功访问,用户权限:"+userType;
}
举例,我有两个方法test
和test2()
,我在授权方法中,将用户角色和权限存放在了session中,所以我要是不进入doGetAuthorizationInfo()
方法中,这个session就不会初始化。
当我登录成功后,先访问了test2()
方法,获取session就是null了,这边我把获取session方法放在了util工具类中。我只有访问了test()
方法后,才能在任意方法中取到session的值。
/*
* 获取当前用户类型
* */
public static Integer getUserType(){
Session session = SecurityUtils.getSubject().getSession();
return Integer.parseInt(session.getAttribute("userType").toString());
}
二、在登录中直接访问一次
我将登录方法改进了一下,多了一个hasRole()
方法。这个方法应该是判断我们设置的权限中有无指定权限,返回boolean值,这边我们不用关心返回值,我们只需要他能够触发到doGetAuthenticationInfo()
方法就行了。括号内的权限文字可以随便打。
/*登录方法*/
public void login(){
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken usernamePasswordToken =
new UsernamePasswordToken(user.getUsername(),user.getPassword());
subject.login(usernamePasswordToken);
subject.hasRole("登录");//用于触发realm中的授权doGetAuthorizationInfo()方法
}