Spring boot 集成jjwt


    io.jsonwebtoken
    jjwt
    0.9.1

时间线

package com.ys.vo;



public class TokenVo {
    private String token;//: "string",

    private long expTime;//: int,
    private long genTime;//: long

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public long getExpTime() {
        return expTime;
    }

    public void setExpTime(long expTime) {
        this.expTime = expTime;
    }

    public long getGenTime() {
        return genTime;
    }

    public void setGenTime(long genTime) {
        this.genTime = genTime;
    }
}

yml文件

#jwt:
#  config:
#    key: ${random.uuid}
#    key: axju
#    expTime: 360000

jjwt

package com.ys.Utils;
/**
 * 使用Token密码进行加密
 */

import com.ys.utils.Constants;
import com.ys.vo.UserVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Date;

@ConfigurationProperties("jwt.config")
public class JwtUtils {
    @Value("${jwt.config.key}")
    private String key;//秘钥
    @Value("${jwt.config.expTime}")
    private long expTime;//持续时间
    private long genTime;//: long

    public long getGenTime() {
        return genTime;
    }

    public void setGenTime(long genTime) {
        this.genTime = genTime;
    }

    public String createToken(当前用户obj userVo) {
        long genTime = System.currentTimeMillis();
        JwtBuilder builder = Jwts.builder().setId(当前用户id + "")
                .setSubject(当前用户名称)
                .setIssuedAt(new Date(genTime))
                .signWith(SignatureAlgorithm.HS256, key)
                .claim("currentUser", userVo);
        if (expTime > 0) {
            builder.setExpiration(new Date(genTime + expTime));
        }
        return builder.compact();
    }

    public Claims parseJWT(String jwtStr) {
        return Jwts.parser()
                .setSigningKey(key)//秘钥加密
                .parseClaimsJws(jwtStr)//载荷
                .getBody();//进行签证
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public long getExpTime() {
        return expTime;
    }

    public void setExpTime(long expTime) {
        this.expTime = expTime;
    }
}

接口登录

Object[] searchPhoneAndPasswd(String phone, String password) throws AuthException;

实现

@Resource
    private JwtUtils jwtUtils;

String jwtStr = jwtUtils.createToken(用户对象);
Claims claims = jwtUtils.parseJWT(jwtStr);

Object[] objects = new Object[2];
TokenVo tokenVo = new TokenVo();
tokenVo.setExpTime(claims.getExpiration().getTime());
tokenVo.setGenTime(claims.getIssuedAt().getTime());
tokenVo.setToken(jwtStr);
objects[0] = userVo;
objects[1] = tokenVo;

token

@Override
public UserVo loadCurrentUserByToken(String token) {
    Claims claims = jwtUtils.parseJWT(token);
    Map map = (Map) claims.get("currentUser");
    UserVo userVo = new UserVo();
    //map转对象
    map.forEach((k, v) -> {
        try {
            List props = Arrays.asList("birthday", "createdTime", "updatedTime");
            if (props.contains(k) == false) {
                Ognl.setValue(k, userVo, v);
            } else {
                if (v != null) {
                    HashMap d1 = (HashMap) v;
                    LocalDateTime d2 = LocalDateTime.of((Integer) (d1.get("year")),
                            (Integer) (d1.get("monthValue")),
                            (Integer) (d1.get("dayOfMonth")),
                            (Integer) (d1.get("hour")),
                            (Integer) (d1.get("minute")));
                    Ognl.setValue(k, userVo, d2);
                }
            }
        } catch (OgnlException e) {
            e.printStackTrace();
        }
    });
    return userVo;
}

你可能感兴趣的:(java)