forked from gzzfw/backEnd/gz-dyh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package cn.huge.base.common.utils;
 
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import java.util.Date;
 
/**
 * @title: JWT工具类
 * @description: JWT工具类
 * @company: hugeinfo
 * @author: liyj
 * @time: 2021-11-05 16:51:48
 * @version: 1.0.0
 */
@Component
public class JwtUtils {
 
    /**
     * jwt生成密钥
     */
    public static String secretKey;
 
    /**
     * token过期时间:4小时
     */
    public static long tokenExpireTime;
 
    /**
     * refreshToken过期时间:比token时间长一点
     */
    public static long refreshTokenExpireTime;
 
    /**
     * refreshToken 存储key
     */
    public static String jwtRefreshTokenKeyFormat;
 
    /**
     * token黑名单 存储key
     */
    public static String jwtBlacklistKeyFormat;
 
    @Value("${jwt.secret-key}")
    public void setSecretKey(String secret_Key) {
        secretKey = secret_Key;
    }
 
    @Value("${jwt.token.expire-time}")
    public void setTokenExpireTime(long token_expire_time) {
        tokenExpireTime = token_expire_time;
    }
 
    @Value("${jwt.refresh-token.expire-time}")
    public void setRefreshTokenExpireTime(long refresh_token_expire_time) {
        refreshTokenExpireTime = refresh_token_expire_time;
    }
 
    @Value("${jwt.refresh-token-key.format}")
    public void setJwtRefreshTokenKeyFormat(String jwt_refresh_token_key_format) {
        jwtRefreshTokenKeyFormat = jwt_refresh_token_key_format;
    }
 
    @Value("${jwt.blacklist-key.format}")
    public void setJwtBlacklistKeyFormat(String jwt_blacklist_key_format) {
        jwtBlacklistKeyFormat = jwt_blacklist_key_format;
    }
 
    /**
     * jwt用户
     */
    private static String ISSUSER = "HUGEINFO";
 
    /**
     * 生成token
     * @param userId 登录用户标识
     * @return String
     */
    public static String buildJWT(String userId){
        //生成jwt
        Date now = new Date();
        Algorithm algo = Algorithm.HMAC256(secretKey);
        String token = JWT.create()
                .withIssuer(ISSUSER)
                .withIssuedAt(now)
                .withExpiresAt(new Date(now.getTime() + tokenExpireTime))
                .withClaim("userId", userId)
                .sign(algo);
        return token;
    }
 
    /**
     * 生成token
     * @param custId 平台客户标识
     * @param userId 登录用户标识
     * @return String
     */
    public static String buildJWT(String custId, String userId){
        //生成jwt
        Date now = new Date();
        Algorithm algo = Algorithm.HMAC256(secretKey);
        String token = JWT.create()
                .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);
    }
}
/**
 * -------------------_ooOoo_-------------------
 * ------------------o8888888o------------------
 * ------------------88" . "88------------------
 * ------------------(| -_- |)------------------
 * ------------------O\  =  /O------------------
 * ---------------____/`---'\____---------------
 * -------------.'  \\|     |//  `.-------------
 * ------------/  \\|||  :  |||//  \------------
 * -----------/  _||||| -:- |||||-  \-----------
 * -----------|   | \\\  -  /// |   |-----------
 * -----------| \_|  ''\---/''  |   |-----------
 * -----------\  .-\__  `-`  ___/-. /-----------
 * ---------___`. .'  /--.--\  `. . __----------
 * ------."" '<  `.___\_<|>_/___.'  >'"".-------
 * -----| | :  `- \`.;`\ _ /`;.`/ - ` : | |-----
 * -----\  \ `-.   \_ __\ /__ _/   .-` /  /-----
 * ======`-.____`-.___\_____/___.-`____.-'======
 * -------------------`=---='
 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 * ---------佛祖保佑---hugeinfo---永无BUG----------
 */