package TestToken;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.junit.Test; import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map; public class TokenTest { //公共密钥客户端不会知道
public static String SECRET="FreeMaNong"; public static String createToken() throws UnsupportedEncodingException {
//签名发布时间
Date iatDate=new Date();
System.out.println(iatDate);//英文时间 //设置签名过期时间 1分钟
Calendar nowTime=Calendar.getInstance();
nowTime.add(Calendar.MINUTE,1);
Date expiresDate=nowTime.getTime();
//System.out.println(expiresDate); Map<String,Object> map=new HashMap<String, Object>();
map.put("alg","HS256");//设置算法 为HS256
map.put("typ","JWT");//设置类型为JWT
String token=JWT.create().withHeader(map)
.withClaim("name","Free码农")
.withClaim("age","28")
.withClaim("org","今日头条")
.withClaim("username","chenyu")
.withIssuedAt(iatDate)//设置签发时间
.withExpiresAt(expiresDate)//设置过去时间 过期时间大于签发时间
.sign(Algorithm.HMAC256(SECRET));//用公共密钥加密
//System.out.println(token);
return token;
} public static Map<String,Claim> verifyToken(String token) throws UnsupportedEncodingException {
JWTVerifier verifier =JWT.require(Algorithm.HMAC256(SECRET)).build();//用公共密钥解密验证
DecodedJWT jwt=null;
try{
jwt=verifier.verify(token);
}catch (Exception e)
{
throw new RuntimeException("登录凭证已过去,请重新登录");
}
return jwt.getClaims();
} @Test
public void TestToken() throws UnsupportedEncodingException {
String token=createToken();
System.out.println("Token:"+token);
Map<String,Claim> claims=verifyToken(token);
System.out.println(claims.get("name").asString());
System.out.println(claims.get("age").asString());
System.out.println(claims.get("username").asString());
System.out.println(claims.get("org")==null?null:claims.get("org").asString()); //测试过期token
// String GuoQiToken="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhdXRoMCJ9.izVguZPRsBQ5Rqw6dhMvcIwy8_9lQnrO3vpxGwPCuzs";
// Map<String,Claim> claims2=verifyToken(GuoQiToken);
} @Test
public void Test() throws UnsupportedEncodingException {
Algorithm algorithm = Algorithm.HMAC256("secret");
String token = JWT.create().withIssuer("auth0") .sign(algorithm);
System.out.println(token);
}
}

  

Token的生成和检验的更多相关文章

  1. ASP.NET OAuth:access token的加密解密,client secret与refresh token的生成

    在 ASP.NET OWIN OAuth(Microsoft.Owin.Security.OAuth)中,access token 的默认加密方法是: 1) System.Security.Crypt ...

  2. token的生成和应用

    token的生成和应用 接口特点汇总: 1.因为是非开放性的,所以所有的接口都是封闭的,只对公司内部的产品有效: 2.因为是非开放性的,所以OAuth那套协议是行不通的,因为没有中间用户的授权过程: ...

  3. JWT实现token的生成和认证demo

    上篇写到对JWT的理解,这篇写一个小的demo来实践下 Github:https://github.com/wuhen152033/token/tree/dev 简介 本次的demo是基于Spring ...

  4. JSON Web Token (JWT)生成Token及解密实战。

    昨天讲解了JWT的介绍.应用场景.优点及注意事项等,今天来个JWT具体的使用实践吧. 从JWT官网支持的类库来看,jjwt是Java支持的算法中最全的,推荐使用,网址如下. https://githu ...

  5. php token的生成

    转载自:http://blog.snsgou.com/post-766.html --->非开放性平台 --->公司内部产品 接口特点汇总: 1.因为是非开放性的,所以所有的接口都是封闭的 ...

  6. token的生成规则

    1.token生成规则要添加时间戳,timestamp,以便解析token时,可以根据判断时间超过30分钟不予处理.像session过期时间一样.

  7. [Python]token的生成及验证

    hmac模块(仅在python3中可以使用) 简介: HMAC是密钥相关的哈希运算消息认证码,HMAC运算利用哈希算法,以一个密钥和一个消息为输入,生成一个消息摘要作为输出. 典型应用: HMAC的一 ...

  8. php token的生成和使用

    原文连接:http://ukagaka.github.io/php/2018/05/08/JWT.html 1. 为什么要使用tokent进行登录 前后端分离或者为了支持多个web应用,那么原来的co ...

  9. Java实现token的生成与验证-登录功能

    一.token与cookie相比较的优势1.支持跨域访问,将token置于请求头中,而cookie是不支持跨域访问的: 2.无状态化,服务端无需存储token,只需要验证token信息是否正确即可,而 ...

随机推荐

  1. [转载]How To Install Nginx And PHP-FPM On CentOS 6 Via Yum

    http://www.lifelinux.com/how-to-install-nginx-and-php-fpm-on-centos-6-via-yum/ http://blog.csdn.net/ ...

  2. 阻塞式简易http服务器

    说明         使用java.net包的ServerSocket也是阻塞的,所以下面的实例把ServerSocketChannel换成ServerSocket效果一样. 后台代码 package ...

  3. SQL将完整时间字段截取到年月日

    在SQL查询语句中使用convert(char(10),日期字段,120)方法 1.char(10)指'yyyy-mm-dd'正好10个字符 2.120的是日期的格式 3.使用语句:select * ...

  4. JS代码判断浏览器类型以及版本

    browserVersion:function(){ var explorer = window.navigator.userAgent; if (explorer.indexOf("MSI ...

  5. mysql里几个超时配置参数wait_timeout,net_read_timeout等

    以下这些配置项单位都是秒,在mysql命令行中可以使用show global variables like '变量名';可查询配置值. connect_timeout:连接响应超时时间.服务器端在这个 ...

  6. 811. Subdomain Visit Count

    这题主要难在构建关联容器,方法很多,但是核心都是把原字符串一截一截减下来处理,先把前面用空格隔开的次数转化为整数,然后处理后面的多层子域. 方法一,查找标志字符,用标志字符把字符串分成几段 stati ...

  7. FMS是什么?

  8. 2019.02.06 bzoj4503: 两个串(fft)

    传送门 题意简述:给两个字符串s,ts,ts,t,ttt中可能有通配符,问ttt在sss出现的次数和所有位置. 思路:一道很熟悉的题,跟bzoj4259bzoj4259bzoj4259差不多的. 然后 ...

  9. hdu-1033(格式)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1033 参考文章:https://blog.csdn.net/curson_/article/detai ...

  10. error C4430: error 2141

    c:\evan\workspace\1\1\netwowkippack.h(50) : error C2146: 语法错误 : 缺少“;”(在标识符“nSourPort”的前面) c:\evan\wo ...