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);
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken authenticationToken) throws AuthenticationException
{
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();
session.setAttribute("uid",user.getId());
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("登录");
}