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的创建与解析的更多相关文章

  1. token的创建及解析

    <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifac ...

  2. 使用dom4j创建和解析xml文件

    使用dom4j创建和解析xml文件 在项目开发中,我们经常会遇到xml文件的创建和解析从别人接口得到的xml文件,而我们最常使用的组件是dom4j. 下面我就以代码来讲解一下如何使用dom4j来创建x ...

  3. python中文json串创建与解析

    下面代码,举例说明了json如何创建和解析含有中文的json串: #coding=gbk import os import sys reload(sys) sys.setdefaultencoding ...

  4. 使用dom4j创建和解析xml

    之前工作中用到了,相信写java的都会碰到xml,这里写了两个方法,创建和解析xml,废话不多说,直接上代码 package xml; import java.io.File; import java ...

  5. 使用Dom4j对XML文档创建与解析

    创建XML文件: public class Dom4jCreateXml { public void testCreatXml() { //创建文档对象 Document document = Doc ...

  6. (转)使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解

    使用 CJSON 在C语言中进行 JSON 的创建和解析的实例讲解   本文用代码简单介绍cjson的使用方法,1)创建json,从json中获取数据.2)创建json数组和解析json数组 1. 创 ...

  7. [转帖] k8s dashboard 的创建 升级 以及 admin token的创建和简单使用.

    Kubernetes Dashboard中的身份认证详解 https://jimmysong.io/posts/kubernetes-dashboard-upgrade/ Thu Nov 2, 201 ...

  8. Java创建和解析Json数据方法(三)——json-lib包的使用

    (三)json-lib包的使用         这篇笔记主要介绍json-lib包的创建和解析json数据的方式,主要是的JSONObject.JSONArray和Java对象:beans, maps ...

  9. Unity3D_(数据)LitJson创建和解析Json

    LitJson github: 传送门 JsonUtility创建和解析Json 传送门 LitJson.dll百度云盘 传送门 密码:p1py 加载LitJson.dll到Unity中 在Asset ...

随机推荐

  1. LeetCode 314. Binary Tree Vertical Order Traversal

    原题链接在这里:https://leetcode.com/problems/binary-tree-vertical-order-traversal/ 题目: Given a binary tree, ...

  2. swing之borderlayout

    import java.awt.BorderLayout; import javax.swing.JButton; import javax.swing.JFrame; //1.继承 JFrame类 ...

  3. CAS单点登录系统简介

    一.cas简介 全名:Central Authentication Service特点: 1.开源的.多协议的 SSO 解决方案: Protocols : Custom Protocol . CAS ...

  4. Sentry深入

    Sentry的架构 内部架构 核心就是规则引擎以及Metadata Store:记录格式有两种,一种policy file记录授权内容,另外一种是通过命令方式进行授权:前者记录在策略文件中,保存形式是 ...

  5. bzoj 1607 [Usaco2008 Dec]Patting Heads 轻拍牛头——枚举倍数

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1607 #include<iostream> #include<cstdio ...

  6. dockerfile http_php

    FROM centos6.6-php5.5:0.0.1 MAINTAINER syberos:wangmo RUN mv /etc/php.ini /etc/php.ini.bak COPY ./ph ...

  7. Python获取%appdata%路径的方法

    import osimport sys import winreg print(os.name)print(sys.getdefaultencoding())print(sys.version)pri ...

  8. 根据VM的TAG开停机

    公有云一个非常大的优势,就是可以根据需求进行开停机.由于计费是按时进行的,从而实现节省成本. Azure上用脚本按时开停机已经有很多部署的案例.本文将介绍采用VM Tag中规定的时间进行开停机的脚本. ...

  9. HDU5475(线段树)

    An easy problem Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  10. java代码。。重温JPassword,JLabel,JPanel

    package com.kk; //JPasswordField类的使用 import java.awt.Color; import java.awt.FlowLayout; import javax ...