1
13693261870
2022-09-16 fee60c3e25fac0982f3b8cb8feea7225c4ed22f8
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
158
159
160
package com.terra.proxy.util;
 
import com.alibaba.fastjson.JSONObject;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
 
import java.util.Date;
 
/**
 * @author lanbase
 * @Description: TODO(jwt工具类)
 * @date 2017-9-27 14:33
 */
@ConfigurationProperties(prefix = "sys.jwt")
@Component
public class JwtUtils {
 
    private Logger logger = LoggerFactory.getLogger(getClass());
 
    private String secret;
 
    private long expire;
 
    private String header;
 
    private String appidtokentime;
 
 
    /**
     * 生成jwt token
     *
     * @param userId
     * @return
     */
 
 
    public String generateToken(long userId) {
        Date nowDate = new Date();
        //过期时间
        Date expireDate = new Date(nowDate.getTime() + expire * 1000);
 
        return Jwts.builder()
                .setHeaderParam("type", "JWT")
                .setSubject(userId + "")
                .setIssuedAt(nowDate)
                .setExpiration(expireDate)
                .signWith(SignatureAlgorithm.HS512, secret)
                .compact();
    }
 
    public String generateToken(String msg, Long expireSeconds) {
        Date nowDate = new Date();
        //过期时间
        Date expireDate = new Date(nowDate.getTime() + expire * 1000);
        if (expireSeconds != null) {
            expireDate = new Date(nowDate.getTime() + expireSeconds.longValue() * 1000);
        }
 
        return Jwts.builder()
                .setHeaderParam("type", "JWT")
                .setSubject(msg)
                .setIssuedAt(nowDate)
                .setExpiration(expireDate)
                .signWith(SignatureAlgorithm.HS512, secret)
                .compact();
    }
 
 
    public String generateToken(Long expireSeconds, Integer appId) {
        if (expireSeconds == null) {
            //根据appid,获取对应的token有效持续时间
            JSONObject appmap = JSONObject.parseObject(appidtokentime);
            expireSeconds = appmap.getLongValue(appId.toString());
        }
        Date nowDate = new Date();
        //过期时间
        Date expireDate = new Date(nowDate.getTime() + expire * 1000);
        if (expireSeconds != null) {
            expireDate = new Date(nowDate.getTime() + expireSeconds.longValue() * 1000);
        }
 
        return Jwts.builder()
                .setHeaderParam("type", "JWT")
                .setSubject(appId.toString())
                .setIssuedAt(nowDate)
                .setExpiration(expireDate)
                .signWith(SignatureAlgorithm.HS512, secret)
                .compact();
    }
 
 
    public String generateToken(Integer userid, Long expireSeconds) {
        Date nowDate = new Date();
        //过期时间
        Date expireDate = new Date(nowDate.getTime() + expire * 1000);
        if (expireSeconds != null) {
            expireDate = new Date(nowDate.getTime() + expireSeconds.longValue() * 1000);
        }
 
        return Jwts.builder()
                .setHeaderParam("type", "JWT")
                .setSubject(userid.toString())
                .setIssuedAt(nowDate)
                .setExpiration(expireDate)
                .signWith(SignatureAlgorithm.HS512, secret)
                .compact();
    }
 
    public Claims getClaimByToken(String token) {
        try {
            return Jwts.parser()
                    .setSigningKey(secret)
                    .parseClaimsJws(token)
                    .getBody();
        } catch (Exception e) {
            return null;
        }
    }
 
 
    /**
     * token是否过期
     *
     * @param expiration
     * @return
     */
    public boolean isTokenExpired(Date expiration) {
        return expiration.before(new Date());
    }
 
    public String getSecret() {
        return secret;
    }
 
    public void setSecret(String secret) {
        this.secret = secret;
    }
 
    public long getExpire() {
        return expire;
    }
 
    public void setExpire(long expire) {
        this.expire = expire;
    }
 
    public String getHeader() {
        return header;
    }
 
    public void setHeader(String header) {
        this.header = header;
    }
 
}