forked from kidgrow-microservices-platform

zhaoxiaohao
2021-01-26 f7c5db77d404397bf9c35ab1ddc7e03639d131a3
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
package com.kidgrow.jwt.server.utils;
 
import com.kidgrow.common.exception.BizException;
import com.kidgrow.jwt.server.properties.AuthServerProperties;
import com.kidgrow.jwt.utils.JwtHelper;
import com.kidgrow.jwt.utils.JwtUserInfo;
import com.kidgrow.jwt.utils.Token;
import lombok.AllArgsConstructor;
 
/**
 * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
 *
 * @Description: jwt token工具<br>
 * @Project: <br>
 * @CreateDate: Created in 2020/2/11 13:55 <br>
 * @Author: <a href="4345453@kidgrow.com">liuke</a>
 */
@AllArgsConstructor
public class JwtTokenServerUtils {
    /**
     * 认证服务端使用,如 authority-server
     * 生成和 解析token
     */
    private AuthServerProperties authServerProperties;
 
    /**
     * 生成token
     *
     * @param jwtInfo
     * @param expire
     * @return
     * @throws BizException
     */
    public Token generateUserToken(JwtUserInfo jwtInfo, Integer expire) throws BizException {
        AuthServerProperties.TokenInfo userTokenInfo = authServerProperties.getUser();
        if (expire == null || expire <= 0) {
            expire = userTokenInfo.getExpire();
        }
        return JwtHelper.generateUserToken(jwtInfo, userTokenInfo.getPriKey(), expire);
    }
 
    /**
     * 解析token
     *
     * @param token
     * @return
     * @throws BizException
     */
    public JwtUserInfo getUserInfo(String token) throws BizException {
        AuthServerProperties.TokenInfo userTokenInfo = authServerProperties.getUser();
        return JwtHelper.getJwtFromToken(token, userTokenInfo.getPubKey());
    }
}