MeterSphere/backend/src/main/java/io/metersphere/security/realm/LocalRealm.java

140 lines
5.0 KiB
Java
Raw Normal View History

2021-01-19 15:49:20 +08:00
package io.metersphere.security.realm;
2020-02-03 11:21:55 +08:00
2020-02-19 11:43:16 +08:00
import io.metersphere.base.domain.Role;
2020-07-14 17:28:53 +08:00
import io.metersphere.commons.constants.UserSource;
2020-05-13 10:33:16 +08:00
import io.metersphere.commons.user.SessionUser;
import io.metersphere.commons.utils.SessionUtils;
2020-02-13 11:51:42 +08:00
import io.metersphere.dto.UserDTO;
2020-06-04 14:43:27 +08:00
import io.metersphere.i18n.Translator;
2020-02-13 11:51:42 +08:00
import io.metersphere.service.UserService;
2020-02-27 15:09:01 +08:00
import org.apache.commons.lang3.StringUtils;
2020-06-22 18:46:30 +08:00
import org.apache.shiro.SecurityUtils;
2020-02-03 11:21:55 +08:00
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
2020-02-19 11:43:16 +08:00
import org.apache.shiro.authz.SimpleAuthorizationInfo;
2020-02-03 11:21:55 +08:00
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2020-02-27 15:09:01 +08:00
import org.springframework.beans.factory.annotation.Value;
2020-02-03 11:21:55 +08:00
2020-02-13 11:51:42 +08:00
import javax.annotation.Resource;
2020-07-14 20:58:52 +08:00
import java.util.Collections;
2020-02-19 11:43:16 +08:00
import java.util.Set;
import java.util.stream.Collectors;
2020-02-13 11:51:42 +08:00
2020-02-03 11:21:55 +08:00
/**
* 自定义Realm 注入service 可能会导致在 service的aop 失效例如@Transactional,
* 解决方法
* <p>
* 1. 这里改成注入mapper这样mapper 中的事务失效<br/>
* 2. 这里仍然注入service在配置ShiroConfig 的时候不去set realm, 等到spring 初始化完成之后
* set realm
* </p>
*/
2021-03-19 13:58:21 +08:00
public class LocalRealm extends AuthorizingRealm {
2020-02-03 11:21:55 +08:00
2021-03-19 13:58:21 +08:00
private Logger logger = LoggerFactory.getLogger(LocalRealm.class);
2020-02-13 11:51:42 +08:00
@Resource
private UserService userService;
2020-02-03 11:21:55 +08:00
2020-02-27 15:09:01 +08:00
@Value("${run.mode:release}")
private String runMode;
2021-01-12 15:08:07 +08:00
@Override
public String getName() {
return "LOCAL";
}
2020-02-03 11:21:55 +08:00
/**
* 权限认证
*/
@Override
2020-02-19 11:43:16 +08:00
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
2020-08-17 10:09:19 +08:00
String userId = (String) principals.getPrimaryPrincipal();
return getAuthorizationInfo(userId, userService);
}
2020-02-27 15:09:01 +08:00
2020-08-17 10:09:19 +08:00
public static AuthorizationInfo getAuthorizationInfo(String userId, UserService userService) {
2020-02-19 11:43:16 +08:00
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
// roles 内容填充
2020-08-17 10:09:19 +08:00
UserDTO userDTO = userService.getUserDTO(userId);
2020-02-19 11:43:16 +08:00
Set<String> roles = userDTO.getRoles().stream().map(Role::getId).collect(Collectors.toSet());
authorizationInfo.setRoles(roles);
return authorizationInfo;
2020-02-03 11:21:55 +08:00
}
/**
* 登录认证
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
2020-07-14 17:28:53 +08:00
String login = (String) SecurityUtils.getSubject().getSession().getAttribute("authenticate");
2020-02-03 11:21:55 +08:00
String userId = token.getUsername();
String password = String.valueOf(token.getPassword());
2020-07-14 17:28:53 +08:00
if (StringUtils.equals("local", runMode)) {
UserDTO user = getUserWithOutAuthenticate(userId);
userId = user.getId();
SessionUser sessionUser = SessionUser.fromUser(user);
SessionUtils.putUser(sessionUser);
return new SimpleAuthenticationInfo(userId, password, getName());
}
if (StringUtils.equals(login, UserSource.LOCAL.name())) {
return loginLocalMode(userId, password);
}
UserDTO user = getUserWithOutAuthenticate(userId);
userId = user.getId();
SessionUser sessionUser = SessionUser.fromUser(user);
SessionUtils.putUser(sessionUser);
return new SimpleAuthenticationInfo(userId, password, getName());
}
private UserDTO getUserWithOutAuthenticate(String userId) {
2020-02-13 11:51:42 +08:00
UserDTO user = userService.getUserDTO(userId);
String msg;
if (user == null) {
2020-06-11 14:27:17 +08:00
user = userService.getUserDTOByEmail(userId);
if (user == null) {
msg = "The user does not exist: " + userId;
logger.warn(msg);
throw new UnknownAccountException(Translator.get("password_is_incorrect"));
2020-06-11 14:27:17 +08:00
}
2020-02-13 11:51:42 +08:00
}
2020-07-14 17:28:53 +08:00
return user;
}
2020-06-11 14:27:17 +08:00
2020-07-14 17:28:53 +08:00
private AuthenticationInfo loginLocalMode(String userId, String password) {
2020-07-14 20:58:52 +08:00
UserDTO user = userService.getLoginUser(userId, Collections.singletonList(UserSource.LOCAL.name()));
2020-07-14 17:28:53 +08:00
String msg;
if (user == null) {
2020-07-24 15:04:38 +08:00
user = userService.getUserDTOByEmail(userId, UserSource.LOCAL.name());
2020-07-14 17:28:53 +08:00
if (user == null) {
msg = "The user does not exist: " + userId;
logger.warn(msg);
throw new UnknownAccountException(Translator.get("password_is_incorrect"));
2020-07-14 17:28:53 +08:00
}
userId = user.getId();
}
2020-02-27 15:09:01 +08:00
// 密码验证
if (!userService.checkUserPassword(userId, password)) {
2020-06-04 14:43:27 +08:00
throw new IncorrectCredentialsException(Translator.get("password_is_incorrect"));
2020-02-27 15:09:01 +08:00
}
2020-02-13 11:51:42 +08:00
SessionUser sessionUser = SessionUser.fromUser(user);
2020-02-19 14:33:23 +08:00
SessionUtils.putUser(sessionUser);
2020-02-03 11:21:55 +08:00
return new SimpleAuthenticationInfo(userId, password, getName());
}
@Override
public boolean isPermitted(PrincipalCollection principals, String permission) {
return true;
}
}