use of com.dtstack.taier.develop.dto.user.DTToken in project Taier by DTStack.
the class TokenService method decryption.
public DTToken decryption(String tokenText) {
Assert.notNull(tokenText, "JWT Token Text can't blank.");
try {
/**
* 验证
*/
DecodedJWT jwt = JWT.require(Algorithm.HMAC256(JWT_TOKEN)).build().verify(tokenText);
DTToken token = new DTToken();
token.setUserName(jwt.getClaim(DTToken.USER_NAME).asString());
token.setUserId(Long.parseLong(jwt.getClaim(DTToken.USER_ID).asString()));
if (!jwt.getClaim(DTToken.TENANT_ID).isNull()) {
token.setTenantId(Long.parseLong(jwt.getClaim(DTToken.TENANT_ID).asString()));
}
token.setExpireAt(jwt.getExpiresAt());
return token;
} catch (UnsupportedEncodingException e) {
if (log.isErrorEnabled()) {
log.error("JWT Token decode Error.", e);
}
throw new RdosDefineException("DT Token解码异常.");
} catch (TokenExpiredException e) {
if (log.isErrorEnabled()) {
log.error("JWT Token expire.", e);
}
throw new RdosDefineException("DT Token已过期");
}
}
use of com.dtstack.taier.develop.dto.user.DTToken in project Taier by DTStack.
the class TokenService method decryptionWithOutExpire.
public DTToken decryptionWithOutExpire(String tokenText) {
Assert.notNull(tokenText, "JWT Token Text can't blank.");
try {
DecodedJWT jwt = JWT.require(Algorithm.HMAC256(JWT_TOKEN)).build().verify(tokenText);
DTToken token = new DTToken();
token.setUserName(jwt.getClaim(DTToken.USER_NAME).asString());
token.setUserId(Long.parseLong(jwt.getClaim(DTToken.USER_ID).asString()));
if (!jwt.getClaim(DTToken.TENANT_ID).isNull()) {
token.setTenantId(Long.parseLong(jwt.getClaim(DTToken.TENANT_ID).asString()));
}
return token;
} catch (UnsupportedEncodingException e) {
throw new RdosDefineException("DT Token解码异常.");
}
}
use of com.dtstack.taier.develop.dto.user.DTToken in project Taier by DTStack.
the class UserController method switchTenant.
@PostMapping(value = "/switchTenant")
public R<String> switchTenant(@RequestParam(value = "tenantId") Long tenantId, HttpServletRequest request, HttpServletResponse response) {
String token = cookieService.token(request);
if (StringUtils.isBlank(token)) {
throw new RdosDefineException(ErrorCode.TOKEN_IS_NULL);
}
DTToken decryption = tokenService.decryption(token);
Long userId = decryption.getUserId();
User user = userService.getById(userId);
if (null == user) {
throw new RdosDefineException(ErrorCode.USER_IS_NULL);
}
Tenant tenant = tenantService.getTenantById(tenantId);
if (null == tenant) {
throw new RdosDefineException(ErrorCode.TENANT_IS_NULL);
}
DtUser dtUser = new DtUser();
dtUser.setUserId(user.getId());
dtUser.setUserName(user.getUserName());
dtUser.setEmail(user.getEmail());
dtUser.setPhone(user.getPhoneNumber());
dtUser.setTenantId(tenantId);
dtUser.setTenantName(tenant.getTenantName());
loginService.onAuthenticationSuccess(request, response, dtUser);
return R.ok(user.getUserName());
}
use of com.dtstack.taier.develop.dto.user.DTToken in project Taier by DTStack.
the class LoginService method onAuthenticationSuccess.
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, DtUser dtUser) {
String dtToken = cookieService.token(request);
// 若Token不存在,则生成Token
if (Objects.isNull(dtToken)) {
cookie(request, response, dtUser);
} else {
DTToken token = tokenService.decryption(dtToken);
boolean equalsUserId = dtUser.getUserId().equals(token.getUserId());
boolean nonNullTenantId = Objects.nonNull(dtUser.getTenantId());
if (nonNullTenantId && !dtUser.getTenantId().equals(token.getTenantId())) {
cookie(request, response, dtUser);
} else if (!equalsUserId) {
cookie(request, response, dtUser);
}
}
}
Aggregations