Token的生成和检验
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的生成和检验的更多相关文章
- ASP.NET OAuth:access token的加密解密,client secret与refresh token的生成
在 ASP.NET OWIN OAuth(Microsoft.Owin.Security.OAuth)中,access token 的默认加密方法是: 1) System.Security.Crypt ...
- token的生成和应用
token的生成和应用 接口特点汇总: 1.因为是非开放性的,所以所有的接口都是封闭的,只对公司内部的产品有效: 2.因为是非开放性的,所以OAuth那套协议是行不通的,因为没有中间用户的授权过程: ...
- JWT实现token的生成和认证demo
上篇写到对JWT的理解,这篇写一个小的demo来实践下 Github:https://github.com/wuhen152033/token/tree/dev 简介 本次的demo是基于Spring ...
- JSON Web Token (JWT)生成Token及解密实战。
昨天讲解了JWT的介绍.应用场景.优点及注意事项等,今天来个JWT具体的使用实践吧. 从JWT官网支持的类库来看,jjwt是Java支持的算法中最全的,推荐使用,网址如下. https://githu ...
- php token的生成
转载自:http://blog.snsgou.com/post-766.html --->非开放性平台 --->公司内部产品 接口特点汇总: 1.因为是非开放性的,所以所有的接口都是封闭的 ...
- token的生成规则
1.token生成规则要添加时间戳,timestamp,以便解析token时,可以根据判断时间超过30分钟不予处理.像session过期时间一样.
- [Python]token的生成及验证
hmac模块(仅在python3中可以使用) 简介: HMAC是密钥相关的哈希运算消息认证码,HMAC运算利用哈希算法,以一个密钥和一个消息为输入,生成一个消息摘要作为输出. 典型应用: HMAC的一 ...
- php token的生成和使用
原文连接:http://ukagaka.github.io/php/2018/05/08/JWT.html 1. 为什么要使用tokent进行登录 前后端分离或者为了支持多个web应用,那么原来的co ...
- Java实现token的生成与验证-登录功能
一.token与cookie相比较的优势1.支持跨域访问,将token置于请求头中,而cookie是不支持跨域访问的: 2.无状态化,服务端无需存储token,只需要验证token信息是否正确即可,而 ...
随机推荐
- activiti5.22整合modeler时出错TypeError: Cannot read property 'split' of undefined
activiti5.22.0整合modeler时,打开的流程页面不显示工具栏和左边的控件栏,产生如下的错误: TypeError: Cannot read property 'split' of un ...
- 【C++】c++中栈 队列 的应用
C++中提供了STL模板statck 在使用的时候更为方便 除了一般的队列外 还有STL更有双向队列可以使用 #include<deque> 声明:deque <type > ...
- 【Apache】Apache服务的基本概念(二)
Apache服务的基本概念 Apache安装请参照:[Apache]Apache服务的安装(一) 1.端口 apache默认监听TCP协议端口80端口 2.apache服务 apache服务默认会启动 ...
- 买铅笔(NOIP2016)
先给题目链接:买铅笔 这题非常水,没啥可分析的,先给代码: #include<bits/stdc++.h> //1 using namespace std; int main(){ int ...
- centos7 sqoop 1 搭建笔记
1.require : java环境,hadoop,hive ,mysql2.下载解压sqoop13.设置环境变量 export SQOOP_HOME=/data/spark/bin/sqoop ex ...
- my.ini优化mysql数据库性能的十个参数(推荐)
(1).max_connections:允许的同时客户的数量.增加该值增加 mysqld 要求的文件描述符的数量.这个数字应该增加,否则,你将经常看到 too many connections 错误. ...
- 微信小程序组件的使用
1.在page同级目录下新建components文件夹,然后新建目录test,新建组件test 2.新建在page目录下新建目录,然后新建page页面.注意:每新建一个页面,都要修改app.json文 ...
- redis知识积累
redis 安装包:redis-3.0.6.tar.gz 打开 redis: cd /media/deepl/文档/ubuntutools/redis-3.0.6/src/ ./redis-serve ...
- oracle在exp导出时报错PLS-00201: identifier 'EXFSYS.DBMS_EXPFIL_DEPASEXP' must be declared
报错如下信息: EXP-00008: ORACLE error 6550 encounteredORA-06550: line 1, column 14:PLS-00201: identifier ' ...
- MFC 消息框
窗口类能够使用messagebox int ret = MessageBox(_T("内容"), _T("标题"), MB_OKCANCLE| //MB_OB ...