詳解基于JWT的springboot權(quán)限驗(yàn)證技術(shù)實(shí)現(xiàn)
JWT簡介
Json Web Token(JWT):JSON網(wǎng)絡(luò)令牌,是為了在網(wǎng)絡(luò)應(yīng)用環(huán)境間傳遞聲明而制定的一種基于JSON的開放標(biāo)準(zhǔn)((RFC 7519)。JWT是一個(gè)輕便的安全跨平臺傳輸格式,定義了一個(gè)緊湊的自包含的方式用于通信雙方之間以 JSON 對象行使安全的傳遞信息。因?yàn)閿?shù)字簽名的存在,這些信息是可信的。
實(shí)現(xiàn)步驟:
環(huán)境spring boot
1、添加jwt依賴
<dependency> <groupId>com.auth0</groupId> <artifactId>java-jwt</artifactId> <version>3.8.1</version> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>0.9.1</version> </dependency>
2、在src下創(chuàng)建annotation包
新建自定義注解類 JwtToken
package com.qf.tyleryue_one.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * 自定義注解:方法前 表示方法需要攔截 */@Target({ElementType.METHOD,ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)public @interface JwtToken {}
3、在src下創(chuàng)建utils包
新建自定義JwtUtils工具類
package com.qf.tyleryue_one.utils;import com.auth0.jwt.JWT;import com.auth0.jwt.JWTCreator;import com.auth0.jwt.JWTVerifier;import com.auth0.jwt.algorithms.Algorithm;import jdk.internal.org.objectweb.asm.TypeReference;import java.util.Date;/** * 用來生成簽名,校驗(yàn)簽名,通過簽名 */public class JwtUtils { //令牌有效時(shí)間 private final static long EXPIRE_TIME=5*60*1000; //密鑰 private final static String SECRECT='Tyler_Yue_key'; /** * 創(chuàng)建令牌 */ public static String sign(String userId){ //構(gòu)建失效時(shí)鐘 Date exipre_date = new Date(System.currentTimeMillis() + EXPIRE_TIME); //創(chuàng)建令牌 JWTCreator.Builder builder = JWT.create(); //給jwt令牌playload中放入發(fā)令牌放的用戶 //給userid用戶發(fā)令牌 builder.withAudience(userId); //設(shè)置令牌失效時(shí)間 builder.withExpiresAt(exipre_date); //對令牌密鑰進(jìn)行加密 Algorithm algorithm = Algorithm.HMAC256(SECRECT); String sign = builder.sign(algorithm); return sign;//返回令牌 } /** * 驗(yàn)證令牌 */ public static boolean verifyToken(String token){ try { //生成校驗(yàn)器 Algorithm algorithm = Algorithm.HMAC256(SECRECT); //校驗(yàn) JWTVerifier build = JWT.require(algorithm).build(); //無異常則校驗(yàn)成功 return true; } catch (Exception e) { throw new RuntimeException('令牌過期'); } }}
4、在src下新建vo包
封裝一個(gè)返回用戶帶令牌的 對象
package com.qf.tyleryue_one.vo;import com.alibaba.druid.filter.AutoLoad;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;/** * 封裝一個(gè)返回 含令牌的用戶對象 */@Data@AllArgsConstructor@NoArgsConstructorpublic class TokenVo { //用戶名 private String usernaem; //令牌名 private String token;}
5、舉例controller層用戶登錄業(yè)務(wù)登錄帶令牌
package com.qf.tyleryue_one.controller;import com.qf.tyleryue_one.entity.VueUser;import com.qf.tyleryue_one.service.VueUserService;import com.qf.tyleryue_one.utils.JwtUtils;import com.qf.tyleryue_one.vo.Msg;import com.qf.tyleryue_one.vo.TokenVo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.*;import java.util.UUID;/** * 登錄業(yè)務(wù) */@Controllerpublic class VueUserController { @Autowired private VueUserService vueUserService; @RequestMapping(value = '/dealLogin',method = RequestMethod.POST) @CrossOrigin @ResponseBody public Msg login(@RequestBody VueUser vueUser){ VueUser vueUser1 = vueUserService.selectByUsername(vueUser.getUsername()); if (vueUser1!=null){ if (vueUser1.getPassword().equals(vueUser.getPassword())){//密碼匹配,發(fā)放令牌///隨機(jī)生成字符串未useridString userid = UUID.randomUUID().toString();String token = JwtUtils.sign(userid);//封裝令牌對象TokenVo tokenVo = new TokenVo(vueUser.getUsername(), token);return new Msg(200,'登錄成功,令牌已發(fā)放',tokenVo); }else {return new Msg(403,'密碼錯(cuò)誤',null); } }else { return new Msg(403,'用戶不存在',null); } }}
到此這篇關(guān)于詳解基于JWT的springboot權(quán)限驗(yàn)證技術(shù)實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)springboot JWT權(quán)限驗(yàn)證內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. JS繪圖Flot如何實(shí)現(xiàn)動(dòng)態(tài)可刷新曲線圖2. 詳解CSS不定寬溢出文本適配滾動(dòng)3. PHP與已存在的Java應(yīng)用程序集成4. python中if嵌套命令實(shí)例講解5. 詳解Python中openpyxl模塊基本用法6. 使用css實(shí)現(xiàn)全兼容tooltip提示框7. CSS自定義滾動(dòng)條樣式案例詳解8. IDEA項(xiàng)目的依賴(pom.xml文件)導(dǎo)入問題及解決9. Java之JSP教程九大內(nèi)置對象詳解(中篇)10. 使用ProcessBuilder調(diào)用外部命令,并返回大量結(jié)果
