dataease-dm/backend/src/main/java/io/dataease/auth/server/AuthServer.java

127 lines
4.8 KiB
Java
Raw Normal View History

2021-02-26 11:27:33 +08:00
package io.dataease.auth.server;
import io.dataease.auth.api.AuthApi;
import io.dataease.auth.api.dto.CurrentRoleDto;
import io.dataease.auth.api.dto.CurrentUserDto;
import io.dataease.auth.api.dto.LoginDto;
2021-03-08 18:19:57 +08:00
import io.dataease.auth.config.RsaProperties;
2021-02-26 11:27:33 +08:00
import io.dataease.auth.entity.SysUserEntity;
2021-03-08 18:19:57 +08:00
import io.dataease.auth.entity.TokenInfo;
2021-02-26 11:27:33 +08:00
import io.dataease.auth.service.AuthUserService;
import io.dataease.auth.util.JWTUtils;
2021-03-08 18:19:57 +08:00
import io.dataease.auth.util.RsaUtil;
2021-02-26 11:27:33 +08:00
import io.dataease.commons.utils.BeanUtils;
2021-03-08 18:19:57 +08:00
import io.dataease.commons.utils.CodingUtil;
2021-02-26 11:27:33 +08:00
import io.dataease.commons.utils.ServletUtils;
2021-06-01 11:13:40 +08:00
/*import io.dataease.plugins.config.SpringContextUtil;
import io.dataease.plugins.xpack.display.dto.response.SysSettingDto;
2021-06-01 11:13:40 +08:00
import io.dataease.plugins.xpack.display.service.DisPlayXpackService;*/
2021-06-04 10:57:58 +08:00
import io.dataease.i18n.Translator;
2021-03-08 18:19:57 +08:00
import org.apache.commons.lang3.ObjectUtils;
2021-02-26 11:27:33 +08:00
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
2021-02-26 11:27:33 +08:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
2021-06-04 10:57:58 +08:00
2021-02-26 11:27:33 +08:00
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
public class AuthServer implements AuthApi {
@Autowired
private AuthUserService authUserService;
@Override
2021-03-08 18:19:57 +08:00
public Object login(@RequestBody LoginDto loginDto) throws Exception {
2021-02-26 11:27:33 +08:00
String username = loginDto.getUsername();
String password = loginDto.getPassword();
2021-03-08 18:19:57 +08:00
SysUserEntity user = authUserService.getUserByName(username);
2021-04-28 12:16:07 +08:00
2021-06-04 10:57:58 +08:00
if (ObjectUtils.isEmpty(user)) {
throw new RuntimeException(Translator.get("i18n_id_or_pwd_error"));
2021-02-26 11:27:33 +08:00
}
2021-06-04 10:57:58 +08:00
if (user.getEnabled() == 0) {
throw new RuntimeException(Translator.get("i18n_id_or_pwd_error"));
2021-04-28 12:16:07 +08:00
}
String realPwd = user.getPassword();
2021-03-08 18:19:57 +08:00
//私钥解密
String pwd = RsaUtil.decryptByPrivateKey(RsaProperties.privateKey, password);
//md5加密
pwd = CodingUtil.md5(pwd);
2021-06-04 10:57:58 +08:00
if (!StringUtils.equals(pwd, realPwd)) {
throw new RuntimeException(Translator.get("i18n_id_or_pwd_error"));
2021-02-26 11:27:33 +08:00
}
2021-06-04 10:57:58 +08:00
Map<String, Object> result = new HashMap<>();
2021-03-08 18:19:57 +08:00
TokenInfo tokenInfo = TokenInfo.builder().userId(user.getUserId()).username(username).lastLoginTime(System.currentTimeMillis()).build();
String token = JWTUtils.sign(tokenInfo, realPwd);
2021-03-10 11:49:18 +08:00
// 记录token操作时间
JWTUtils.addTokenExpire(token);
2021-03-03 14:44:01 +08:00
result.put("token", token);
2021-03-08 18:19:57 +08:00
ServletUtils.setToken(token);
2021-03-03 14:44:01 +08:00
return result;
2021-02-26 11:27:33 +08:00
}
@Override
public CurrentUserDto userInfo() {
2021-06-04 10:57:58 +08:00
CurrentUserDto userDto = (CurrentUserDto) SecurityUtils.getSubject().getPrincipal();
if (ObjectUtils.isEmpty(userDto)) {
String token = ServletUtils.getToken();
Long userId = JWTUtils.tokenInfoByToken(token).getUserId();
SysUserEntity user = authUserService.getUserById(userId);
CurrentUserDto currentUserDto = BeanUtils.copyBean(new CurrentUserDto(), user);
List<CurrentRoleDto> currentRoleDtos = authUserService.roleInfos(user.getUserId());
List<String> permissions = authUserService.permissions(user.getUserId());
currentUserDto.setRoles(currentRoleDtos);
currentUserDto.setPermissions(permissions);
return currentUserDto;
}
return userDto;
2021-02-26 11:27:33 +08:00
}
2021-03-08 18:19:57 +08:00
@Override
2021-06-04 10:57:58 +08:00
public String logout() {
String token = ServletUtils.getToken();
Long userId = JWTUtils.tokenInfoByToken(token).getUserId();
authUserService.clearCache(userId);
2021-02-26 11:27:33 +08:00
return "success";
}
@Override
public Boolean validateName(@RequestBody Map<String, String> nameDto) {
String userName = nameDto.get("userName");
if (StringUtils.isEmpty(userName)) return false;
SysUserEntity userEntity = authUserService.getUserByName(userName);
if (ObjectUtils.isEmpty(userEntity)) return false;
return true;
}
2021-02-26 11:27:33 +08:00
@Override
public Boolean isLogin() {
return null;
}
@Override
public String test() {
2021-03-10 11:49:18 +08:00
SysUserEntity userById = authUserService.getUserById(4L);
String nickName = userById.getNickName();
// System.out.println(nickName);
2021-06-01 11:13:40 +08:00
/* Map<String, DisPlayXpackService> beansOfType = SpringContextUtil.getApplicationContext().getBeansOfType(DisPlayXpackService.class);
2021-05-11 16:30:21 +08:00
for (Map.Entry entry : beansOfType.entrySet()) {
Object key = entry.getKey();
DisPlayXpackService value = (DisPlayXpackService)entry.getValue();
2021-05-11 16:30:21 +08:00
List<SysSettingDto> sysSettingDtos = value.systemSettings();
2021-05-11 16:30:21 +08:00
String name = entry.getValue().getClass().getName();
System.out.println("key: "+ key + ", value: "+ name);
2021-06-01 11:13:40 +08:00
}*/
2021-02-26 11:27:33 +08:00
return "apple";
}
}