package com.dk.common.util.oauth; import com.auth0.jwt.JWT; import com.auth0.jwt.JWTVerifier; import com.auth0.jwt.algorithms.Algorithm; import com.auth0.jwt.exceptions.JWTDecodeException; import com.auth0.jwt.interfaces.DecodedJWT; import com.dk.common.infrastructure.config.ConfigStatic; import lombok.extern.slf4j.Slf4j; import org.apache.oltu.oauth2.rs.request.OAuthAccessResourceRequest; import javax.servlet.http.HttpServletRequest; @Slf4j public class JwtUtil { public static final String SHIRO_CP_ID = "ftyId"; public static final String SHIRO_CP_CODE = "ftyCode"; public static final String SHIRO_STAFF_NAME = "username"; public static final String SHIRO_STAFF_ID = "userId"; public static final String SHIRO_APP_CODE = "appCode"; public static final String SHIRO_CLIENT_ID = "clientId"; public static final String SHIRO_USER_SALT = "salt"; public static final String SHIRO_ISSUER = "Issuer"; public static final String SHIRO_SUBJECT = "long_token"; public static final String SHIRO_GRANT_TYPE = "grantType"; public static final String SHIRO_APP_LANG = "lang"; /** * 校验token是否正确 * * @param token 密钥 * @param salt 盐值 * @return 是否正确 */ public static boolean verify(String token, String salt) { try { Algorithm algorithm = Algorithm.HMAC256(salt); JWTVerifier verifier = JWT.require(algorithm) // 签发人 .withIssuer(SHIRO_ISSUER) // 主题 .withSubject(SHIRO_SUBJECT) // 签发的目标 //.withAudience(jwtProperties.getAudience()) .build(); DecodedJWT jwt = verifier.verify(token); if (jwt != null) { return true; } } catch (Exception e) { log.error("The token is invalid{}", e.getMessage()); } return false; } /** * 获取AES解密token * * @param request * @return */ public static String getDecryptToken(HttpServletRequest request, String AESKey) { String token = ""; try { // 构建 OAuth2 资源请求 OAuthAccessResourceRequest oauthRequest = new OAuthAccessResourceRequest(request); // 获取Access Token String accessToken = oauthRequest.getAccessToken(); token = AESSecurityUtil.decrypt(AESKey, accessToken); return token; } catch (Exception e) { throw new RuntimeException(e); } } /** * @desc : 通过已解密的token获取staffName * @author : 周兴 * @date : 2023/2/26 16:07 */ public static String getStaffName(String token) { try { DecodedJWT jwt = JWT.decode(token); return jwt.getClaim(SHIRO_STAFF_NAME).asString(); } catch (JWTDecodeException e) { log.error("error:{}", e.getMessage()); return null; } } /** * @desc : 通过已解密的token获取staffId * @author : 周兴 * @date : 2023/2/26 16:07 */ public static String getStaffId(String token) { try { DecodedJWT jwt = JWT.decode(token); return jwt.getClaim(SHIRO_STAFF_ID).asString(); } catch (JWTDecodeException e) { log.error("error:{}", e.getMessage()); return null; } } /** * @desc : 通过已解密的token获取cpId * @author : 周兴 * @date : 2023/2/26 16:07 */ public static String getCpId(String token) { try { DecodedJWT jwt = JWT.decode(token); return jwt.getClaim(SHIRO_CP_ID).asString(); } catch (JWTDecodeException e) { log.error("error:{}", e.getMessage()); return null; } } /** * @desc : 通过已解密的token获取cpCode * @author : 周兴 * @date : 2023/2/26 16:07 */ public static String getCpCode(String token) { try { DecodedJWT jwt = JWT.decode(token); return jwt.getClaim(SHIRO_CP_CODE).asString(); } catch (JWTDecodeException e) { log.error("error:{}", e.getMessage()); return null; } } /** * @desc : 通过已解密的token获取appCode * @author : 张潇木 * @date : 2022-7-15 17:34 */ public static String getAppCode(String token) { try { DecodedJWT jwt = JWT.decode(token); return jwt.getClaim(SHIRO_APP_CODE).asString(); } catch (JWTDecodeException e) { log.error("error:{}", e.getMessage()); return null; } } /** * @desc : 通过已解密的token获取Lang * @author : 周兴 * @date : 2022-7-15 17:34 */ public static String getLang(String token) { try { DecodedJWT jwt = JWT.decode(token); return jwt.getClaim(SHIRO_APP_LANG).asString(); } catch (JWTDecodeException e) { log.error("error:{}", e.getMessage()); return null; } } /** * @desc : 通过未解密的token获取appCode * @author : 张潇木 * @date : 2022-7-15 17:34 */ public static String getAppCode(HttpServletRequest httpServletRequest) { try { String authorization = httpServletRequest.getHeader("Authorization"); String[] tokens = authorization.split(" "); String token = AESSecurityUtil.decrypt(ConfigStatic.getAESKey(), tokens[1]); return JwtUtil.getAppCode(token); } catch (Exception e) { log.error("error:{}", e.getMessage()); return null; } } public static String getGrantType(String token) { try { DecodedJWT jwt = JWT.decode(token); return jwt.getClaim(SHIRO_GRANT_TYPE).asString(); } catch (JWTDecodeException e) { log.error("error:{}", e.getMessage()); return null; } } /** * 解析token,获取token数据 * * @param token * @return */ public static DecodedJWT getJwtInfo(String token) { return JWT.decode(token); } public static String uuid32() { return java.util.UUID.randomUUID().toString().replace("-", ""); } }