Search in sources :

Example 1 with DTToken

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已过期");
    }
}
Also used : TokenExpiredException(com.auth0.jwt.exceptions.TokenExpiredException) RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DTToken(com.dtstack.taier.develop.dto.user.DTToken) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT)

Example 2 with DTToken

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解码异常.");
    }
}
Also used : RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DTToken(com.dtstack.taier.develop.dto.user.DTToken) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT)

Example 3 with DTToken

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());
}
Also used : DtUser(com.dtstack.taier.develop.dto.user.DtUser) User(com.dtstack.taier.dao.domain.User) Tenant(com.dtstack.taier.dao.domain.Tenant) RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) DTToken(com.dtstack.taier.develop.dto.user.DTToken) DtUser(com.dtstack.taier.develop.dto.user.DtUser) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 4 with DTToken

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);
        }
    }
}
Also used : DTToken(com.dtstack.taier.develop.dto.user.DTToken)

Aggregations

DTToken (com.dtstack.taier.develop.dto.user.DTToken)4 RdosDefineException (com.dtstack.taier.common.exception.RdosDefineException)3 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 TokenExpiredException (com.auth0.jwt.exceptions.TokenExpiredException)1 Tenant (com.dtstack.taier.dao.domain.Tenant)1 User (com.dtstack.taier.dao.domain.User)1 DtUser (com.dtstack.taier.develop.dto.user.DtUser)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1