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 ...
随机推荐
- RabbitMQ集群 Docker一键部署
以下内容来自网络转载 步骤1. 安装docker 以centos7为例,https://docs.docker.com/engine/installation/linux/centos/ 步骤2. 创 ...
- centOS5.5 配置vnc,开启linux远程桌面
如何远程控制centOS桌面? 如何使用windows远程控制centOS桌面? 1.查看本机是否有安装vnc(centOS5默认有安装vnc) rpm -q vnc vnc-server 如果显示结 ...
- Qt Creator 中的段落 注释的 快捷方法【转载】
原文网址:http://jingyan.baidu.com/article/d5c4b52bc2bd1dda560dc5bb.html 作为一名合格的程序员,漂漂亮亮的注释是必须的!!怎么在Qt Cr ...
- poj 3539 Elevator——同余类bfs
题目:http://poj.org/problem?id=3539 考虑把层数分为模a剩余系.同类内可通过+若干个a走到. 不同类之间需要通过+b.+c来走到. 需要求出每一类中最小的能走到的.即最短 ...
- Docker资源
1.Docker入门教程 http://www.code123.cc/docs/docker-practice/repository/config.html 2.Docker入门教程 http://w ...
- 由于簇计数比预计的高,格式化操作无法完成——Allocation Unit Size Adjustments for Larger NTFS Volumes.
Allocation Unit Size Adjustments for Larger NTFS Volumes. Problem: When trying to format a new vol ...
- Spark Streaming之一:整体介绍
提到Spark Streaming,我们不得不说一下BDAS(Berkeley Data Analytics Stack),这个伯克利大学提出的关于数据分析的软件栈.从它的视角来看,目前的大数据处理可 ...
- 西安电子科技大学第16届程序设计竞赛 G-小国的复仇
西安电子科技大学第16届程序设计竞赛网络同步赛 G-小国的复仇 2 链接:https://www.nowcoder.com/acm/contest/107/G来源:牛客网 题目描述 众所周知,汀老师是 ...
- 隔行变色---bai
<!DOCTYPE html> <html> <style> .mousein { background-color:blue; cursor: pointer; ...
- web.config中namespace的配置(针对页面中引用)
1,在页面中使用强类型时: @model GZUAboutModel @using Nop.Admin.Models//命名空间(注意以下) 2,可以将命名空间提到web.config配置文件中去,此 ...