JWT登錄認(rèn)證Springboot詳解
更新時間:2024年11月20日 15:04:08 作者:十&年
文章主要介紹了如何在Java項目中使用JWT進(jìn)行用戶認(rèn)證和授權(quán),通過定義一個常量,編寫JWT工具類來生成和解析token,登錄時在服務(wù)端生成token并返回給客戶端,客戶端使用攔截器攔截請求,驗證token的有效性,從而實現(xiàn)權(quán)限控制,文章旨在分享個人經(jīng)驗,為開發(fā)者提供參考
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ā)布時間
Date createTime = new Date();
//設(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) //可以將基本不重要的對象信息放到claims中
.withIssuedAt(createTime)//設(shè)置簽發(fā)時間
.withExpiresAt(expiresTime)//設(shè)置過去時間 過期時間大于簽發(fā)時間
.sign(Algorithm.HMAC256(SystemPara.SECRET));//用公共密鑰加密
return token;
}
/**
* 校驗token是否正確
*
* @param token 密鑰
* @param secret 用戶的密碼
* @return 是否正確
*/
public static boolean verify(String token, String logon_name, String secret) {
try {
//根據(jù)密碼生成JWT效驗器
Algorithm algorithm = Algorithm.HMAC256(SystemPara.SECRET);
JWTVerifier verifier = JWT.require(algorithm)
.withClaim("logon_name", logon_name)
.build();
//效驗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;
}
}
}二、登錄時生成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驗證攔截
*/
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);
// 驗證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) {
//攔截路徑可自行配置多個 可用 ,分隔開
registry.addInterceptor(new JwtInterceptor()).addPathPatterns("/**").excludePathPatterns("/Logon");
}
} 總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot RESTful API版本控制最佳方式
本文介紹了六種主流API版本控制策略,包括URI路徑版本控制、請求參數(shù)版本控制、自定義請求頭版本控制、內(nèi)容協(xié)商版本控制、媒體類型參數(shù)版本控制和域名或子域名版本控制,每種策略都有其優(yōu)缺點,并提供了最佳實踐和適用場景2025-12-12
java開發(fā)https請求ssl不受信任問題解決方法
這篇文章主要介紹了java開發(fā)https請求ssl不受信任問題解決方法,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
項目打包成jar后包無法讀取src/main/resources下文件的解決
本文主要介紹了項目打包成jar后包無法讀取src/main/resources下文件的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
如何自定義Jackson序列化?@JsonSerialize
這篇文章主要介紹了如何自定義Jackson序列化?@JsonSerialize,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
Curator實現(xiàn)zookeeper的節(jié)點監(jiān)聽詳解
這篇文章主要介紹了Curator實現(xiàn)zookeeper的節(jié)點監(jiān)聽詳解,Curtor框架中一共有三個實現(xiàn)監(jiān)聽的方式,一種是NodeCache監(jiān)聽指定節(jié)點,一種是pathChildrenCache監(jiān)聽子節(jié)點,一種是TreeCache可以監(jiān)控所有節(jié)點 相當(dāng)于以上兩種的合集,需要的朋友可以參考下2023-12-12
Windows環(huán)境使用bat腳本啟動Java服務(wù)的過程
Java項目一般會被打包成jar后啟動,在windows系統(tǒng)中可以通過終端窗口cmd啟動jar包,即在jar包所在的目錄中打開cmd,或在cmd中進(jìn)入到j(luò)ar包目錄,這篇文章主要介紹了Windows環(huán)境使用bat腳本啟動Java服務(wù),需要的朋友可以參考下2023-08-08

