Search in sources :

Example 1 with UtilClassException

use of org.neusoft.neubbs.exception.UtilClassException in project neubbs by nuitcoder.

the class SecretUtil method encryptMd5.

/**
 * MD5 加密
 *      - MD5 消息摘要算法
 *
 * @param plainText 明文
 * @return String MD5 密文
 */
public static String encryptMd5(String plainText) {
    checkParamNotNull(plainText);
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(plainText.getBytes());
        return new BigInteger(1, md.digest()).toString(SetConst.REDIX_SIXTEEN);
    } catch (NoSuchAlgorithmException noe) {
        throw new UtilClassException(ApiMessage.UNKNOWN_ERROR).log(LogWarnEnum.UC1);
    }
}
Also used : UtilClassException(org.neusoft.neubbs.exception.UtilClassException) BigInteger(java.math.BigInteger) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 2 with UtilClassException

use of org.neusoft.neubbs.exception.UtilClassException in project neubbs by nuitcoder.

the class SecretUtil method generateUserInfoToken.

/**
 * 生成用户信息 Token
 *      - 使用 com.auth0.jwt 库(JSON Web Token),加密生成 token
 *      - 只加密用户信息的(id,name,rank,state),使用 HS256 对称加密算法
 *
 * @param user 用户信息对象(至少 id,name,rank,state 属性不能为空)
 * @return String 密文 token
 */
public static String generateUserInfoToken(UserDO user) {
    checkParamNotNull(user);
    if (user.getId() == null || user.getName() == null || user.getRank() == null || user.getState() == null) {
        throw new UtilClassException(ApiMessage.UNKNOWN_ERROR).log(LogWarnEnum.UC11);
    }
    Map<String, Object> headerMap = new HashMap<>(SetConst.SIZE_TWO);
    headerMap.put("alg", "HS256");
    headerMap.put("typ", "JWT");
    // set payload, use HS256 to encrypt, generate token, not set expire time
    try {
        return JWT.create().withHeader(headerMap).withIssuer("Neubbs Admin").withSubject("Neubbs User Information").withAudience("Neubbs User").withIssuedAt(new Date()).withClaim(ParamConst.ID, user.getId()).withClaim(ParamConst.NAME, user.getName()).withClaim(ParamConst.RANK, user.getRank()).withClaim(ParamConst.STATE, user.getState()).sign(Algorithm.HMAC256(SetConst.JWT_TOKEN_SECRET_KEY));
    } catch (UnsupportedEncodingException e) {
        throw new UtilClassException(ApiMessage.INVALID_TOKEN).log(LogWarnEnum.US13);
    }
}
Also used : UtilClassException(org.neusoft.neubbs.exception.UtilClassException) HashMap(java.util.HashMap) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Date(java.util.Date)

Example 3 with UtilClassException

use of org.neusoft.neubbs.exception.UtilClassException in project neubbs by nuitcoder.

the class SecretUtil method decryptUserInfoToken.

/**
 * 解密用户信息 Token
 *      - 解密 JWT 生成的用户信息 Token, 获取 UserDO 对象
 *
 * @param token 密文 token
 * @return UserDO 用户信息对象(包含 id,name,rank,state 属性)
 */
public static UserDO decryptUserInfoToken(String token) {
    checkParamNotNull(token);
    DecodedJWT decodedJWT;
    try {
        // decrypt HS256
        JWTVerifier verifier = JWT.require(Algorithm.HMAC256(SetConst.JWT_TOKEN_SECRET_KEY)).build();
        // decoding Base64
        decodedJWT = verifier.verify(token);
    } catch (UnsupportedEncodingException | TokenExpiredException e) {
        throw new UtilClassException(ApiMessage.UNKNOWN_ERROR).log(LogWarnEnum.UC10);
    }
    // Get User information(id, name, rank ,state)
    UserDO user = new UserDO();
    user.setId(decodedJWT.getClaim(ParamConst.ID).asInt());
    user.setName(decodedJWT.getClaim(ParamConst.NAME).asString());
    user.setRank(decodedJWT.getClaim(ParamConst.RANK).asString());
    user.setState(decodedJWT.getClaim(ParamConst.STATE).asInt());
    return user;
}
Also used : UtilClassException(org.neusoft.neubbs.exception.UtilClassException) TokenExpiredException(com.auth0.jwt.exceptions.TokenExpiredException) UserDO(org.neusoft.neubbs.entity.UserDO) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) JWTVerifier(com.auth0.jwt.JWTVerifier)

Aggregations

UtilClassException (org.neusoft.neubbs.exception.UtilClassException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 JWTVerifier (com.auth0.jwt.JWTVerifier)1 TokenExpiredException (com.auth0.jwt.exceptions.TokenExpiredException)1 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)1 BigInteger (java.math.BigInteger)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 UserDO (org.neusoft.neubbs.entity.UserDO)1