初识JWT 简单的使用

一 . JWT基本概念

全称是JSON Web Tokens ,

与之前接触到的 用户认证方式不同的是,JWT是无状态的。

当系统中认证用户的那个模块对用户进行认证以后(比如用户名密码),将给用户发送一个Token (就是一串字符,里面会携带一些必要的信息,比如用户名,角色信息)可以放到cookie 里或者别的什么地方,用户后面请求别的模块时会带着这个Token,

验证签名以后,再根据用户角色进行鉴权,进行相应的操作。

用JWT的原因是要和python项目对接起,用JWT进行用户认证比较方便,可以实现分布式情形下的单点登录,而且对Token进行了签名,可以防止用户伪造Token信息。 



二. 引入库

这是java JWT源码的地址,里面有详细的介绍。

https://github.com/auth0/java-jwt 

Installation

Maven

<dependency>
    <groupId>com.auth0</groupId>
    <artifactId>java-jwt</artifactId>
    <version>3.3.0</version>
</dependency>

Gradle

compile 'com.auth0:java-jwt:3.3.0'

三. 实现一个简单的JWT工具类 


package com.example.jwtdemo.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTCreationException;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.io.UnsupportedEncodingException;

/**
 * @program: jwtdemo
 * @description: about jwt
 * @author: boombaozi.com
 * @create: 2018-04
 **/
public class MyJwtUtil {

/**
* @description: 生成token  如果成功 返回Token ,失败则返回null;
**/
    public static String createToken(Long user_id,String user_name,Integer user_auth) {
        try {
            //指定一个加密方式
            Algorithm algorithm = Algorithm.HMAC256("secret");
            //给payload里放入自己需要的信息
   String token = JWT.create()
                    .withClaim("user_id",user_id)
                    .withClaim("user_name",user_name)
                    .withClaim("user_auth",user_auth)
                    .sign(algorithm);
            return token;
        } catch (UnsupportedEncodingException exception) {
            System.out.println("UTF-8 encoding not supported");
            return null;
            //UTF-8 encoding not supported
        } catch (JWTCreationException exception) {
            System.out.println("Invalid Signing configuration / Couldn't convert Claims.");
            return null;
            //Invalid Signing configuration / Couldn't convert Claims.
        }

    }

    /**
    * @description:  验证token签名是否合法,合法返回true ,非法返回false
    **/
public static boolean verifyToken(String token){
    try {
        Algorithm algorithm = Algorithm.HMAC256("secret");

        JWTVerifier verifier = JWT.require(algorithm)
                .build(); //Reusable verifier instance
        DecodedJWT jwt = verifier.verify(token);
        return true;
    } catch (UnsupportedEncodingException exception){
        System.out.println("UTF-8 encoding not supported");
        return false;
        //UTF-8 encoding not supported
    } catch (JWTVerificationException exception){
        //Invalid signature/claims
        System.out.println("Invalid signature/claims");
        return false;
    }
}
/**
* @description:
**/

public static String decodeToken(String token) {

    try {

        DecodedJWT jwt = JWT.decode(token);

        String payload = jwt.getPayload();

        return payload;

    } catch (JWTDecodeException exception) {
        //Invalid token
        System.out.println("Invalid signature/claims");
        return null;
    }

}

}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值