File was renamed from dyh-service/dyh-base/src/main/java/cn/huge/base/common/utils/JwtUtils.java |
| | |
| | | package cn.huge.base.common.utils; |
| | | package cn.huge.module.utils; |
| | | |
| | | import com.auth0.jwt.JWT; |
| | | import com.auth0.jwt.algorithms.Algorithm; |
| | |
| | | * jwt生成密钥 |
| | | */ |
| | | public static String secretKey; |
| | | |
| | | /** |
| | | * jwt生成密钥 |
| | | */ |
| | | public static String issUser; |
| | | |
| | | /** |
| | | * token过期时间:4小时 |
| | |
| | | secretKey = secret_Key; |
| | | } |
| | | |
| | | @Value("${jwt.iss_user}") |
| | | public void setIssUser(String iss_user) { |
| | | issUser = iss_user; |
| | | } |
| | | |
| | | @Value("${jwt.token.expire-time}") |
| | | public void setTokenExpireTime(long token_expire_time) { |
| | | tokenExpireTime = token_expire_time; |
| | |
| | | } |
| | | |
| | | /** |
| | | * jwt用户 |
| | | */ |
| | | private static String ISSUSER = "HUGEINFO"; |
| | | |
| | | /** |
| | | * 生成token |
| | | * @param userId 登录用户标识 |
| | | * @return String |
| | |
| | | Date now = new Date(); |
| | | Algorithm algo = Algorithm.HMAC256(secretKey); |
| | | String token = JWT.create() |
| | | .withIssuer(ISSUSER) |
| | | .withIssuer(issUser) |
| | | .withIssuedAt(now) |
| | | .withExpiresAt(new Date(now.getTime() + tokenExpireTime)) |
| | | .withClaim("userId", userId) |
| | |
| | | Date now = new Date(); |
| | | Algorithm algo = Algorithm.HMAC256(secretKey); |
| | | String token = JWT.create() |
| | | .withIssuer(ISSUSER) |
| | | .withIssuer(issUser) |
| | | .withIssuedAt(now) |
| | | .withExpiresAt(new Date(now.getTime() + tokenExpireTime)) |
| | | .withClaim("custId", custId) |
| | | .withClaim("userId", userId) |
| | | .sign(algo); |
| | | return token; |
| | | } |
| | | |
| | | /** |
| | | * 生成讯飞token |
| | | * @return |
| | | */ |
| | | public static String buildXfJWT(){ |
| | | //生成jwt |
| | | Date time = new Date(); |
| | | String secret = "D34F84C9963548A9BA4A70AD6D61A99E"; |
| | | String appId = "1000000001"; |
| | | Algorithm algorithm = Algorithm.HMAC256(secret); |
| | | String auth = JWT.create() |
| | | .withIssuer(appId) |
| | | .withClaim("time", time+"") |
| | | .sign(algorithm); |
| | | return "Bearer "+auth; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | String auth = buildXfJWT(); |
| | | System.out.println(auth); |
| | | } |
| | | } |
| | | /** |