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-05-30 20:24:54 +08:00
|
|
|
|
|
|
|
import io.dataease.plugins.config.SpringContextUtil;
|
|
|
|
|
|
|
|
import io.dataease.plugins.xpack.display.dto.response.SysSettingDto;
|
|
|
|
import io.dataease.plugins.xpack.display.service.DisPlayXpackService;
|
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;
|
2021-03-19 13:05:14 +08:00
|
|
|
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;
|
|
|
|
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-03-08 18:19:57 +08:00
|
|
|
if (ObjectUtils.isEmpty(user)){
|
2021-02-26 11:27:33 +08:00
|
|
|
throw new RuntimeException("没有该用户!");
|
|
|
|
}
|
2021-04-28 12:16:07 +08:00
|
|
|
if (user.getEnabled()==0){
|
|
|
|
throw new RuntimeException("用户已经失效!");
|
|
|
|
}
|
|
|
|
String realPwd = user.getPassword();
|
2021-03-08 18:19:57 +08:00
|
|
|
//私钥解密
|
|
|
|
String pwd = RsaUtil.decryptByPrivateKey(RsaProperties.privateKey, password);
|
|
|
|
//md5加密
|
|
|
|
pwd = CodingUtil.md5(pwd);
|
|
|
|
|
|
|
|
if (!StringUtils.equals(pwd, realPwd)){
|
2021-02-26 11:27:33 +08:00
|
|
|
throw new RuntimeException("密码错误!");
|
|
|
|
}
|
2021-03-03 14:44:01 +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-03-19 13:05:14 +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-02-26 11:27:33 +08:00
|
|
|
public String logout(){
|
2021-03-19 13:05:14 +08:00
|
|
|
String token = ServletUtils.getToken();
|
|
|
|
Long userId = JWTUtils.tokenInfoByToken(token).getUserId();
|
|
|
|
authUserService.clearCache(userId);
|
2021-02-26 11:27:33 +08:00
|
|
|
return "success";
|
|
|
|
}
|
|
|
|
|
2021-04-22 14:53:23 +08:00
|
|
|
@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();
|
2021-05-21 12:26:25 +08:00
|
|
|
// System.out.println(nickName);
|
2021-05-30 20:24:54 +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();
|
2021-05-30 20:24:54 +08:00
|
|
|
DisPlayXpackService value = (DisPlayXpackService)entry.getValue();
|
2021-05-11 16:30:21 +08:00
|
|
|
List<SysSettingDto> sysSettingDtos = value.systemSettings();
|
2021-05-30 20:24:54 +08:00
|
|
|
|
2021-05-11 16:30:21 +08:00
|
|
|
String name = entry.getValue().getClass().getName();
|
|
|
|
System.out.println("key: "+ key + ", value: "+ name);
|
2021-05-30 20:24:54 +08:00
|
|
|
}
|
2021-02-26 11:27:33 +08:00
|
|
|
return "apple";
|
|
|
|
}
|
|
|
|
}
|