JWT登錄認(rèn)證Springboot詳解
SystemPara.SECRET 是自己定義的常量
依賴
<!-- token --> <dependency> <groupId>com.auth0</groupId> <artifactId>java-jwt</artifactId> <version>3.3.0</version> </dependency>
一、JWT工具類
package com.cn.util; import com.auth0.jwt.JWT; import com.auth0.jwt.JWTVerifier; import com.auth0.jwt.algorithms.Algorithm; import com.auth0.jwt.exceptions.JWTDecodeException; import com.auth0.jwt.interfaces.Claim; import com.auth0.jwt.interfaces.DecodedJWT; import com.cn.util.SystemPara; import com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithm; import java.io.UnsupportedEncodingException; import java.security.SignatureException; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * @Author 徐本錫 * @Date 2019/7/4 * @Param * @return **/ public class JWTUtil { private static final long EXPIRE_TIME = 5 * 60 * 1000; public static final String TOKEN_HEADER = "token"; public static final String TOKEN_PREFIX = "xbx_"; /** * @Author 徐本錫 * @Description 生成token * @Date 2019/7/4 * @Param * @return **/ public static String createToken (String logon_name) throws UnsupportedEncodingException { //簽名發(fā)布時(shí)間 Date createTime = new Date(); //設(shè)置簽名過期時(shí)間 5分鐘 Calendar nowTime=Calendar.getInstance(); nowTime.add(Calendar.MINUTE,5); Date expiresTime = nowTime.getTime(); //Date expiresTime = new Date(System.currentTimeMillis() + EXPIRE_TIME); Map<String,Object> map=new HashMap<String, Object>(); map.put("alg","HS256");//設(shè)置算法 為HS256 map.put("typ","JWT");//設(shè)置類型為JWT String token= JWT.create() .withHeader(map) .withClaim("logon_name",logon_name) //可以將基本不重要的對(duì)象信息放到claims中 .withIssuedAt(createTime)//設(shè)置簽發(fā)時(shí)間 .withExpiresAt(expiresTime)//設(shè)置過去時(shí)間 過期時(shí)間大于簽發(fā)時(shí)間 .sign(Algorithm.HMAC256(SystemPara.SECRET));//用公共密鑰加密 return token; } /** * 校驗(yàn)token是否正確 * * @param token 密鑰 * @param secret 用戶的密碼 * @return 是否正確 */ public static boolean verify(String token, String logon_name, String secret) { try { //根據(jù)密碼生成JWT效驗(yàn)器 Algorithm algorithm = Algorithm.HMAC256(SystemPara.SECRET); JWTVerifier verifier = JWT.require(algorithm) .withClaim("logon_name", logon_name) .build(); //效驗(yàn)TOKEN DecodedJWT jwt = verifier.verify(token); return true; } catch (Exception exception) { return false; } } /** * 獲得token中的信息無需secret解密也能獲得 * * @return token中包含的用戶名 */ public static String getLogonName(String token) { try { DecodedJWT jwt = JWT.decode(token); return jwt.getClaim("logon_name").asString(); } catch (JWTDecodeException e) { return null; } } }
二、登錄時(shí)生成token
service方法中生成token,然后放入返回結(jié)果
//service方法中生成token,然后放入返回結(jié)果 String token = JWTUtil.createToken(loginName); resultMap.put("token", token);
controller中 把token放入 response響應(yīng)中
package com.cn.controller; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.cn.domain.EmpinforVo; import com.cn.service.LogonService; import com.cn.util.JWTUtil; import com.cn.util.R; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author xbx * @date 2019-05-13 */ @RestController @RequestMapping("/Logon") public class LogonController { private final LogonService logonService; @Autowired public LogonController(LogonService logonService) { this.logonService = logonService; } @RequestMapping(value = "", method = RequestMethod.POST) public R logon(HttpServletResponse response, EmpinforVo entity) throws Exception{ Map resultMap = logonService.logon(entity); String token = (String) resultMap.get("token"); //放到響應(yīng)頭部 response.setHeader(JWTUtil.TOKEN_HEADER, JWTUtil.TOKEN_PREFIX + token); return R.success(resultMap); } }
三、創(chuàng)建攔截器
package com.cn.interceptor; import com.cn.util.JWTUtil; import com.cn.util.SystemPara; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * token驗(yàn)證攔截 */ public class JwtInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { // 取得token String tokenHeader = request.getHeader(JWTUtil.TOKEN_HEADER); if (tokenHeader == null || "".equals(tokenHeader)) { throw new Exception("token不存在"); } if (!tokenHeader.startsWith(JWTUtil.TOKEN_PREFIX)) { throw new Exception("這是你自己造的token吧"); } String token = tokenHeader.replace(JWTUtil.TOKEN_PREFIX, "");//真正的token String logonName = JWTUtil.getLogonName(token); // 驗(yàn)證token是否有效 if (!JWTUtil.verify(token, logonName, SystemPara.SECRET)){ throw new Exception("token已失效"); } return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // TODO Auto-generated method stub } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // TODO Auto-generated method stub } }
四、配置攔截器
package com.cn.config; import com.cn.interceptor.JwtInterceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * @Author 徐本錫 **/ @Configuration public class WebAppConfiguration implements WebMvcConfigurer { /** * 配置靜態(tài)資源 */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { String path= "/"; for(int i=1900; i<=2500; i++){ path = String.valueOf(i); registry.addResourceHandler("/"+path+"/**").addResourceLocations("file:C:/"+path+"/"); registry.addResourceHandler("/"+path+"/**").addResourceLocations("file:/"+path+"/"); } } /** * 跨域支持 */ @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**") .allowedOrigins("*") .allowCredentials(true) .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH") .maxAge(3600 * 24); } /** * 添加攔截器 */ @Override public void addInterceptors(InterceptorRegistry registry) { //攔截路徑可自行配置多個(gè) 可用 ,分隔開 registry.addInterceptor(new JwtInterceptor()).addPathPatterns("/**").excludePathPatterns("/Logon"); } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot+Mybatis-plus實(shí)現(xiàn)分頁查詢的示例代碼
本文主要介紹了SpringBoot+Mybatis-plus實(shí)現(xiàn)分頁查詢的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02Java實(shí)現(xiàn)Excel表單控件的添加與刪除
本文通過Java代碼示例介紹如何在Excel表格中添加表單控件,包括文本框、單選按鈕、復(fù)選框、組合框、微調(diào)按鈕等,以及如何刪除Excel中的指定表單控件,需要的可以參考一下2022-05-05Java后臺(tái)與微信小程序的數(shù)據(jù)交互實(shí)現(xiàn)
這篇文章主要介紹了Java后臺(tái)與微信小程序的數(shù)據(jù)交互實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12SpringMVC中Controller層獲取前端請(qǐng)求參數(shù)的方式匯總
這篇文章主要介紹了SpringMVC中Controller層獲取前端請(qǐng)求參數(shù)的幾種方式,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08java中json和對(duì)象之間相互轉(zhuǎn)換的運(yùn)用
本文主要介紹了java中json和對(duì)象之間相互轉(zhuǎn)換的運(yùn)用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07SpringBoot中讀取application.properties配置文件的方法
這篇文章主要介紹了SpringBoot中讀取application.properties配置文件的三種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-02-02Spring用AspectJ開發(fā)AOP(基于Annotation)
這篇文章主要介紹了Spring用AspectJ開發(fā)AOP(基于Annotation),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10Java基礎(chǔ)知識(shí)精通塊作用域與條件及switch語句
塊(block,即復(fù)合語句)是指由若干條 Java 語句組成的語句,并由一對(duì)大括號(hào)括起來。塊確定了變量的作用域。一個(gè)塊可以嵌套在另一個(gè)塊中;條件語句、switch語句是我們常見會(huì)用到的結(jié)構(gòu),感興趣的朋友來看看吧2022-04-04