Search in sources :

Example 31 with BusinessException

use of com.jun.plugin.system.common.exception.BusinessException in project jun_springboot_api_service by wujun728.

the class JwtUtil method sign.

/**
 * 生成签名
 * @param account 帐号
 * @return java.lang.String 返回加密的Token
 * @author Wang926454
 * @date 2018/8/31 9:07
 */
public static String sign(String account) {
    try {
        // 帐号加JWT私钥加密
        String secret = account + Base64ConvertUtil.decode(encryptJWTKey);
        // 此处过期时间是以毫秒为单位,所以乘以1000
        Date date = new Date(System.currentTimeMillis() + Long.parseLong(accessTokenExpireTime) * 1000);
        Algorithm algorithm = Algorithm.HMAC256(secret);
        // 附带account帐号信息
        return JWT.create().withClaim(Constant.ACCOUNT, account).withClaim("currentTimeMillis", String.valueOf(System.currentTimeMillis())).withExpiresAt(date).sign(algorithm);
    } catch (UnsupportedEncodingException e) {
        logger.error("JWTToken加密出现UnsupportedEncodingException异常:{}", e.getMessage());
        throw new BusinessException("JWTToken加密出现UnsupportedEncodingException异常:" + e.getMessage());
    }
}
Also used : BusinessException(com.jun.plugin.system.common.exception.BusinessException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Algorithm(com.auth0.jwt.algorithms.Algorithm) Date(java.util.Date)

Example 32 with BusinessException

use of com.jun.plugin.system.common.exception.BusinessException in project jun_springboot_api_service by wujun728.

the class JwtUtil method verify.

/**
 * 校验token是否正确
 * @param token Token
 * @return boolean 是否正确
 * @author Wang926454
 * @date 2018/8/31 9:05
 */
public static boolean verify(String token) {
    try {
        // 帐号加JWT私钥解密
        String secret = getClaim(token, Constant.ACCOUNT) + Base64ConvertUtil.decode(encryptJWTKey);
        Algorithm algorithm = Algorithm.HMAC256(secret);
        JWTVerifier verifier = JWT.require(algorithm).build();
        verifier.verify(token);
        return true;
    } catch (UnsupportedEncodingException e) {
        logger.error("JWTToken认证解密出现UnsupportedEncodingException异常:{}", e.getMessage());
        throw new BusinessException("JWTToken认证解密出现UnsupportedEncodingException异常:" + e.getMessage());
    }
}
Also used : BusinessException(com.jun.plugin.system.common.exception.BusinessException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Algorithm(com.auth0.jwt.algorithms.Algorithm) JWTVerifier(com.auth0.jwt.JWTVerifier)

Aggregations

BusinessException (com.jun.plugin.system.common.exception.BusinessException)32 SysUser (com.jun.plugin.system.entity.SysUser)7 SysDept (com.jun.plugin.system.entity.SysDept)5 IOException (java.io.IOException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 SysPermission (com.jun.plugin.system.entity.SysPermission)3 SysRolePermission (com.jun.plugin.system.entity.SysRolePermission)3 Transactional (org.springframework.transaction.annotation.Transactional)3 Algorithm (com.auth0.jwt.algorithms.Algorithm)2 SysRole (com.jun.plugin.system.entity.SysRole)2 DeptRespNodeVO (com.jun.plugin.system.vo.resp.DeptRespNodeVO)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 SecureRandom (java.security.SecureRandom)2 BadPaddingException (javax.crypto.BadPaddingException)2 Cipher (javax.crypto.Cipher)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 KeyGenerator (javax.crypto.KeyGenerator)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 SecretKey (javax.crypto.SecretKey)2