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-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.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-02-26 11:27:33 +08:00
|
|
|
String realPwd = user.getPassword();
|
2021-03-08 18:19:57 +08:00
|
|
|
if (ObjectUtils.isEmpty(user)){
|
2021-02-26 11:27:33 +08:00
|
|
|
throw new RuntimeException("没有该用户!");
|
|
|
|
}
|
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-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() {
|
|
|
|
String token = ServletUtils.getToken();
|
2021-03-08 18:19:57 +08:00
|
|
|
Long userId = JWTUtils.tokenInfoByToken(token).getUserId();
|
|
|
|
SysUserEntity user = authUserService.getUserById(userId);
|
2021-02-26 11:27:33 +08:00
|
|
|
CurrentUserDto currentUserDto = BeanUtils.copyBean(new CurrentUserDto(), user);
|
|
|
|
List<CurrentRoleDto> currentRoleDtos = authUserService.roleInfos(user.getUserId());
|
2021-03-05 16:07:44 +08:00
|
|
|
List<String> permissions = authUserService.permissions(user.getUserId());
|
2021-02-26 11:27:33 +08:00
|
|
|
currentUserDto.setRoles(currentRoleDtos);
|
2021-03-05 16:07:44 +08:00
|
|
|
currentUserDto.setPermissions(permissions);
|
2021-02-26 11:27:33 +08:00
|
|
|
return currentUserDto;
|
|
|
|
}
|
|
|
|
|
2021-03-08 18:19:57 +08:00
|
|
|
@Override
|
2021-02-26 11:27:33 +08:00
|
|
|
public String logout(){
|
|
|
|
return "success";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Boolean isLogin() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String test() {
|
|
|
|
return "apple";
|
|
|
|
}
|
|
|
|
}
|