set roles

This commit is contained in:
Captain.B 2020-02-19 11:43:16 +08:00
parent 14a6b787e0
commit 182d5b2450
1 changed files with 15 additions and 3 deletions

View File

@ -1,18 +1,22 @@
package io.metersphere.security; package io.metersphere.security;
import io.metersphere.base.domain.Role;
import io.metersphere.dto.UserDTO; import io.metersphere.dto.UserDTO;
import io.metersphere.service.UserService; import io.metersphere.service.UserService;
import io.metersphere.user.SessionUser; import io.metersphere.user.SessionUser;
import org.apache.shiro.SecurityUtils; import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*; import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection; import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.Set;
import java.util.stream.Collectors;
/** /**
@ -34,8 +38,16 @@ public class ShiroDBRealm extends AuthorizingRealm {
* 权限认证 * 权限认证
*/ */
@Override @Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
return null; String userName = (String) principals.getPrimaryPrincipal();
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
// roles 内容填充
UserDTO userDTO = userService.getUserDTO(userName);
Set<String> roles = userDTO.getRoles().stream().map(Role::getId).collect(Collectors.toSet());
authorizationInfo.setRoles(roles);
return authorizationInfo;
} }
/** /**
@ -53,7 +65,7 @@ public class ShiroDBRealm extends AuthorizingRealm {
logger.warn(msg); logger.warn(msg);
throw new UnknownAccountException(msg); throw new UnknownAccountException(msg);
} }
// TODO 密码验证roles 等内容填充 // TODO 密码验证
SessionUser sessionUser = SessionUser.fromUser(user); SessionUser sessionUser = SessionUser.fromUser(user);
SecurityUtils.getSubject().getSession().setAttribute("user", sessionUser); SecurityUtils.getSubject().getSession().setAttribute("user", sessionUser);