继之前更新对登录授权和令牌的博客之后,今天如期带来Java的集成

推荐阅读:

新建一个maven项目

依赖

<dependencies>
    <!-- JWT -->
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt</artifactId>
        <version>0.7.0</version>
    </dependency>
    <!--lombok-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.6</version>
    </dependency>
    <!--junit-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>

创建 Member

@Data
public class Member {

    private String id;
    private String nickname;
    private String avatar;
}

创建 JwtUtils

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;

/**
 * @author 乐心湖
 * @date 2020/7/31 20:08
 **/
public class JwtUtils {

    public static final String SUBJECT = "lxh-user";

    //秘钥
    public static final String APP_SECRET = "79e7c69681b8270162386e6daa53d1dc";

    //过期时间,毫秒,30分钟
    public static final long EXPIRE = 1000 * 60 * 30;

    /**
     * 生成Jwt令牌
     * @return
     */
    public static String generateJwt(Member member){


        String token = Jwts.builder()
                .setHeaderParam("typ", "JWT") //令牌类型
                .setHeaderParam("alg", "HS256") //签名算法
                .setSubject(SUBJECT) //令牌主题
                .setIssuedAt(new Date()) //签发时间
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE)) //过期时间
                .claim("id", member.getId())
                .claim("nickname", member.getNickname())
                .claim("avatar", member.getAvatar())
                .signWith(SignatureAlgorithm.HS256, APP_SECRET).compact();

        return token;
    }


    /**
     * 校验jwt
     * @param jwtToken
     * @return
     */
    public static Claims checkJwt(String jwtToken){

        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);

        return claimsJws.getBody();
    }
}

JwtTest 测试

生成一个JWT

@Test
public void testGenerateJwt(){
    Member member = new Member();
    member.setId("10000");
    member.setNickname("xn2001");
    member.setAvatar("1.png");
    String jwt = JwtUtils.generateJwt(member);
    System.out.println(jwt);
}

输出的就是我们生成的 JWT Token 了

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJseGgtdXNlciIsImlhdCI6MTU5NjE5ODIxOCwiZXhwIjoxNTk2MjAwMDE4LCJpZCI6IjEwMDAwIiwibmlja25hbWUiOiJ4bjIwMDEiLCJhdmF0YXIiOiIxLnBuZyJ9.vnjit4PGJWOnh7TKIExiF2lw2p0OQpdtCIe55Ponjfs

验证 token

@Test
public void testCheckJwt(){

    Claims claims = JwtUtils.checkJwt(
   
        //这里是你生成的JWT         "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJseGgtdXNlciIsImlhdCI6MTU5NjE5ODIxOCwiZXhwIjoxNTk2MjAwMDE4LCJpZCI6IjEwMDAwIiwibmlja25hbWUiOiJ4bjIwMDEiLCJhdmF0YXIiOiIxLnBuZyJ9.vnjit4PGJWOnh7TKIExiF2lw2p0OQpdtCIe55Ponjfs"
    );

    String id = (String)claims.get("id");
    String nickname = (String)claims.get("nickname");
    String avatar = (String)claims.get("avatar");

    System.out.println(id);
    System.out.println(nickname);
    System.out.println(avatar);
}

10000
xn2001
1.png


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!


腾讯云社区邀请各位技术博主加入,福利多多噢!
Last modification:August 11th, 2020 at 01:01 am
如果觉得我的文章对你有用,请随意赞赏