Blame view

src/main/java/com/huaheng/framework/token/TokenService.java 2.92 KB
mahuandong authored
1
2
package com.huaheng.framework.token;
3
4
mahuandong authored
5
6
import com.huaheng.framework.redis.RedisCache;
import com.huaheng.pc.system.user.domain.User;
7
import io.jsonwebtoken.*;
8
9
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
mahuandong authored
10
11
12
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
13
14
import java.time.LocalDateTime;
import java.time.ZoneId;
mahuandong authored
15
16
17
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
18
mahuandong authored
19
20
21
22
23
24
25
26
27
28
29
30

/**
 * token生成与解析
 * @author Enzo Cotter
 * @date 2020/6/11
 */
@Service
public class TokenService {

    @Resource
    private RedisCache redisCache;
31
32
33

    private static final Logger log = LoggerFactory.getLogger(TokenService.class);
mahuandong authored
34
35
36
37
38
39
40
    /**
     * 有效期7
     */
    private static final int EXPIRE_TIME = 7;
    /**
     * 
     */
41
    public static final String signingKey = "OldSuperMan";
mahuandong authored
42
43
mahuandong authored
44
45
46
47
48
    /**
     * 创建token
     * @param user 用户
     * @return
     */
49
    public String createToken(User user, String password){
mahuandong authored
50
        Claims claims = Jwts.claims();
51
52
53
54
55
56
57
        user.setPassword(password);
        claims.put("user", user);
        JwtBuilder builder = Jwts.builder().setClaims(claims)
                //签发时间
                .setIssuedAt(new Date())
                //签名算法以及密匙
                .signWith(SignatureAlgorithm.HS512,signingKey);
mahuandong authored
58
59
60
61
62
63
64
65
66
//        if (redisCache.setCacheObject(token, user.toString(), EXPIRE_TIME, TimeUnit.DAYS) == null) {
//            throw new ServiceException("redis异常");
//        }
        if (EXPIRE_TIME >= 0) {
            LocalDateTime expDate = LocalDateTime.now();
            expDate = expDate.plusDays(EXPIRE_TIME);
            // 过期时间
            builder.setExpiration(Date.from(expDate.atZone(ZoneId.systemDefault()).toInstant()));
mahuandong authored
67
        }
68
        return builder.compact();
mahuandong authored
69
70
71
    }

    /**
游杰 authored
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
     * 创建token
     * @param user 用户
     * @return
     */
    public String createTokenForMobile(User user){
        //签发时间
        Date iatTime = new Date();
        //expire time
        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.DATE, EXPIRE_TIME);

        Claims claims = Jwts.claims();
        claims.put("user", user);
        claims.setIssuedAt(iatTime);
        String token = Jwts.builder().setClaims(claims)
                .signWith(SignatureAlgorithm.HS512,signingKey).compact();
88
        //Date expireTime = nowTime.getTime();
游杰 authored
89
90
91
92
93
94
95
//        if (redisCache.setCacheObject(token, user.toString(), EXPIRE_TIME, TimeUnit.DAYS) == null) {
//            throw new ServiceException("redis异常");
//        }
        return token;
    }

    /**
mahuandong authored
96
97
98
99
100
101
102
     * 解析token
     * @param token
     */
    public void parseToken(String token){
        Jws<Claims> jws = Jwts.parser().setSigningKey(signingKey).parseClaimsJws(token);
        Claims claims = jws.getBody();
        Map<String,String> header = jws.getHeader();
103
104
        System.out.println("parse" + header);
        log.warn("Token:"+ header);
mahuandong authored
105
106
    }
}