springcloud安全控制token的创建与解析
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Service; import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.util.Base64;
import java.util.UUID; /**
* Create by liping on 2018/9/25
*/
@Service
public class JWTService {
private static final String encodekeys = "liping"; private static SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS512;
private static Key key; // @Value("${com.idoipo.jwt.encodedKey}")
// private String encodedKey; /**
* 初始化操作 @PostConstruct初始化完成后执行
*/
// @PostConstruct
// public void init() {
// signatureAlgorithm = SignatureAlgorithm.HS512;
// key = deserializeKey(encodedKey);
// } /**
* 生成加密解密key
* @return
*/
public static Key deserializeKey() {
byte[] decodedKey = Base64.getDecoder().decode(encodekeys); Key key = new SecretKeySpec(decodedKey, JWTService.signatureAlgorithm.getJcaName());
return key;
} /**
* 创建token 参数应该为自定义模型去传,用来设置token所携带字段
* @return
*/
public static String createToken(){
key = deserializeKey();
String token = Jwts.builder()
.setSubject(UUID.randomUUID().toString())
.claim("userName", "liping")
.setIssuer("lp")
.setAudience("xixi")
.signWith(signatureAlgorithm, key).compact();
return token;
} public static String parseToken(String token){
key = deserializeKey();
Claims claims = Jwts.parser().setSigningKey(key).parseClaimsJws(token).getBody();
return claims.get("userName").toString();
} public static void main(String[] args) {
String token = JWTService.createToken();
System.out.println(token);
String userName = JWTService.parseToken(token);
System.out.println(userName);
} }
springcloud安全控制token的创建与解析的更多相关文章
- token的创建及解析
<dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifac ...
- 使用dom4j创建和解析xml文件
使用dom4j创建和解析xml文件 在项目开发中,我们经常会遇到xml文件的创建和解析从别人接口得到的xml文件,而我们最常使用的组件是dom4j. 下面我就以代码来讲解一下如何使用dom4j来创建x ...
- python中文json串创建与解析
下面代码,举例说明了json如何创建和解析含有中文的json串: #coding=gbk import os import sys reload(sys) sys.setdefaultencoding ...
- 使用dom4j创建和解析xml
之前工作中用到了,相信写java的都会碰到xml,这里写了两个方法,创建和解析xml,废话不多说,直接上代码 package xml; import java.io.File; import java ...
- 使用Dom4j对XML文档创建与解析
创建XML文件: public class Dom4jCreateXml { public void testCreatXml() { //创建文档对象 Document document = Doc ...
- (转)使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解
使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解 本文用代码简单介绍cjson的使用方法,1)创建json,从json中获取数据.2)创建json数组和解析json数组 1. 创 ...
- [转帖] k8s dashboard 的创建 升级 以及 admin token的创建和简单使用.
Kubernetes Dashboard中的身份认证详解 https://jimmysong.io/posts/kubernetes-dashboard-upgrade/ Thu Nov 2, 201 ...
- Java创建和解析Json数据方法(三)——json-lib包的使用
(三)json-lib包的使用 这篇笔记主要介绍json-lib包的创建和解析json数据的方式,主要是的JSONObject.JSONArray和Java对象:beans, maps ...
- Unity3D_(数据)LitJson创建和解析Json
LitJson github: 传送门 JsonUtility创建和解析Json 传送门 LitJson.dll百度云盘 传送门 密码:p1py 加载LitJson.dll到Unity中 在Asset ...
随机推荐
- php处理redis
1,connect 描述:实例连接到一个Redis.参数:host: string,port: int返回值:BOOL 成功返回:TRUE;失败返回:FALSE 示例: 1 2 3 4 5 <? ...
- Unity5-ABSystem(五):AssetBundle内存
http://blog.csdn.net/lodypig/article/details/51879702 AssetBundle内存占用 建议 实测 www加载实测 LoadFromFile加载实测 ...
- h5的video标签支持的视频格式
关于<video>标签所支持的视频格式和编码: MP4 = MPEG 4文件使用 H264 视频编解码器和AAC音频编解码器 WebM = WebM 文件使用 VP8 视频编解码器和 Vo ...
- 如何在IJ中使用Jaxb2通过xml定义生成对应的Java Entity类的文件
#0. 准备要转换的xml文件,在Project视界中,右击这个xml文件,在弹出的菜单上选择“Generate XSD schema from XML File...”, 按默认设置生成xsd文件. ...
- POJ1159:动态规划
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 60290 Accepted: 20998 Desc ...
- postgresql 数据库,模式,表空间的关系
数据库与模式模式(schema)是对数据库(database)逻辑分割在数据库创建的同时,就已经默认为数据库创建了一个模式--public,这也是该数据库的默认模式.所有为此数据库创建的对象(表.函数 ...
- expected declaration specifiers or '...' before string constant
/work/platform_bus_dev_drv/led_dev.c:52: error: expected declaration specifiers or '...' before stri ...
- Rails的静态资源管理(一)——Asset Pipeline是什么
官方文档:http://guides.ruby-china.org/asset_pipeline.html http://guides.rubyonrails.org/asset_pipeline.h ...
- WINDOWS 7下的记事本程序目录
这是win7的目录 系统所在分区:\Windows\system32\notepad.exe
- 使用setTimeout 来实现setInterval的效果
直接上代码,实现的核心就是在延时的情况下递归的调用自己, x=0 y=-1 function countMin( ) { y=y+1 document.displayMin.displayBox.va ...