Token的解密和加密
生活随笔
收集整理的這篇文章主要介紹了
Token的解密和加密
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
首先引入pom依賴?
<dependency><groupId>com.auth0</groupId><artifactId>java-jwt</artifactId><version>3.4.0</version></dependency>加密Token
import java.util.Calendar; import java.util.Date;import com.auth0.jwt.JWT; import com.auth0.jwt.algorithms.Algorithm; import com.baidu.websocket.core.constans.SysCfg;public class encryToken {private static final String KEY= "qushen";public String token() {Calendar calendar = Calendar.getInstance();Date StartDate = calendar.getTime();System.out.println("calendar"+calendar);calendar.add(Calendar.SECOND,60); //特定時間的年后Date EndDate = calendar.getTime();System.out.println("StartDate"+StartDate);System.out.println("EndDate"+EndDate);String Token=JWT.create().withSubject(KEY)//.withIssuedAt(StartDate) // sign time.withExpiresAt(EndDate).sign(Algorithm.HMAC256(SysCfg.TOKEN_SALT));System.out.println(Token);return Token;} }解密的Token
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView;import com.auth0.jwt.JWT; import com.auth0.jwt.JWTVerifier; import com.auth0.jwt.algorithms.Algorithm; import com.baidu.websocket.core.constans.SysCfg;@Controller public class test {@RequestMapping("/index")@ResponseBodypublic String index(String Token) {System.out.println("進(jìn)入了controller");String token=token(Token);System.out.println("token:"+token);try {if(token.equals("qushen")) {return "token驗(yàn)證成功";}} catch (Exception e) {return "token驗(yàn)證失敗";}return "token驗(yàn)證失敗";}//解密Tokenpublic String token(String Token) {JWTVerifier jwtVerifier=JWT.require(Algorithm.HMAC256(SysCfg.TOKEN_SALT)).build();String verify = jwtVerifier.verify(Token).getSubject().toString();return verify;}}?
總結(jié)
以上是生活随笔為你收集整理的Token的解密和加密的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 王者荣耀软辅什么意思 2017年赵寅成
- 下一篇: 分布式定时任务