SpringBoot登錄驗(yàn)證token攔截器的實(shí)現(xiàn)
用戶訪問(wèn)接口驗(yàn)證,如果用戶沒(méi)有登錄,則不讓他訪問(wèn)除登錄外的任何接口。
實(shí)現(xiàn)思路:
1.前端登錄,后端創(chuàng)建token(通過(guò)JWT這個(gè)依賴),返給前端
2.前端訪問(wèn)其他接口,傳遞token,后端判斷token存在以或失效
3.失效或不存在,則返回失效提示,前端根據(jù)接口返回的失效提示,讓其跳轉(zhuǎn)到登錄界面
注解定義
定義2個(gè)注解,1個(gè)用于任何接口都能訪問(wèn),另外一個(gè)用于需要登錄才能訪問(wèn)
調(diào)用都通過(guò)注解
package com.example.etf.story.tools;
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 PassToken {
boolean required() default true;
}登錄才能通過(guò)
package com.example.etf.story.tools;
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 UserLoginToken {
boolean required() default true;
}注解的作用說(shuō)明
@Target代表此注解,能@到哪些代碼上
@Target:注解的作用目標(biāo)
@Target(ElementType.TYPE)——接口、類(lèi)、枚舉、注解@Target(ElementType.FIELD)——字段、枚舉的常量@Target(ElementType.METHOD)——方法@Target(ElementType.PARAMETER)——方法參數(shù)@Target(ElementType.CONSTRUCTOR)——構(gòu)造函數(shù)@Target(ElementType.LOCAL_VARIABLE)——局部變量@Target(ElementType.ANNOTATION_TYPE)——注解@Target(ElementType.PACKAGE)——包
@Retention:注解的保留位置
RetentionPolicy.SOURCE:這種類(lèi)型的Annotations只在源代碼級(jí)別保留,編譯時(shí)就會(huì)被忽略,在class字節(jié)碼文件中不包含。RetentionPolicy.CLASS:這種類(lèi)型的Annotations編譯時(shí)被保留,默認(rèn)的保留策略,在class文件中存在,但JVM將會(huì)忽略,運(yùn)行時(shí)無(wú)法獲得。RetentionPolicy.RUNTIME:這種類(lèi)型的Annotations將被JVM保留,所以他們能在運(yùn)行時(shí)被JVM或其他使用反射機(jī)制的代碼所讀取和使用。@Document:說(shuō)明該注解將被包含在javadoc中@Inherited:說(shuō)明子類(lèi)可以繼承父類(lèi)中的該注解
token生成與驗(yàn)證
然后springBoot攔截器驗(yàn)證token
攔截器定義
攔截器配置定義
攔截器攔截,除了登錄和發(fā)送短信,不攔截,其他都攔截
package com.example.etf.story.tools;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.annotation.Resource;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Resource
private LoginInterceptor loginInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//注冊(cè)自己的攔截器,并設(shè)置攔截的請(qǐng)求路徑
//addPathPatterns為攔截此請(qǐng)求路徑的請(qǐng)求
//excludePathPatterns為不攔截此路徑的請(qǐng)求
registry.addInterceptor(loginInterceptor).addPathPatterns("/story/*").excludePathPatterns("/story/sendSMS")
.excludePathPatterns("/story/signOrRegister");
}
}攔截的時(shí)候,調(diào)用的方法,給誰(shuí)通過(guò)
其中service查詢數(shù)據(jù)庫(kù),有沒(méi)有用戶,的方法要自己寫(xiě)
攔截器的方法執(zhí)行類(lèi)
package com.example.etf.story.tools;
import com.auth0.jwt.JWT;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.example.etf.story.dao.R;
import com.example.etf.story.paramer.UserInfoParam;
import com.example.etf.story.service.TestClientService;
import com.example.etf.story.service.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
@Slf4j
@Component
public class LoginInterceptor extends R implements HandlerInterceptor {
/**
* 目標(biāo)方法執(zhí)行前
* 該方法在控制器處理請(qǐng)求方法前執(zhí)行,其返回值表示是否中斷后續(xù)操作
* 返回 true 表示繼續(xù)向下執(zhí)行,返回 false 表示中斷后續(xù)操作
*
* @return
*/
@Resource
private TestClientService testClientService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String token = request.getHeader("token");// 從 http 請(qǐng)求頭中取出 token
// 如果不是映射到方法直接通過(guò)
if (!(handler instanceof HandlerMethod)) {
return true;
}
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
//檢查方法是否有passtoken注解,有則跳過(guò)認(rèn)證,直接通過(guò)
if (method.isAnnotationPresent(PassToken.class)) {
PassToken passToken = method.getAnnotation(PassToken.class);
if (passToken.required()) {
return true;
}
}
//檢查有沒(méi)有需要用戶權(quán)限的注解
if (method.isAnnotationPresent(UserLoginToken.class)) {
UserLoginToken userLoginToken = method.getAnnotation(UserLoginToken.class);
if (userLoginToken.required()) {
// 執(zhí)行認(rèn)證
if (token == null) {
throw new RuntimeException("無(wú)token,請(qǐng)重新登錄");
}
// 獲取 token 中的 user id
String phone;
try {
phone = JWT.decode(token).getClaim("phone").asString();
} catch (JWTDecodeException j) {
throw new RuntimeException("token不正確,請(qǐng)不要通過(guò)非法手段創(chuàng)建token");
}
//查詢數(shù)據(jù)庫(kù),看看是否存在此用戶,方法要自己寫(xiě)
UserInfoParam userInfoParam = testClientService.selectUserByPhone(phone);
if (userInfoParam == null) {
throw new RuntimeException("用戶不存在,請(qǐng)重新登錄");
}
// 驗(yàn)證 token
if (TokenUtils.verify(token)) {
return true;
} else {
throw new RuntimeException("token過(guò)期或不正確,請(qǐng)重新登錄");
}
}
}
throw new RuntimeException("沒(méi)有權(quán)限注解一律不通過(guò)");
}
/**
* 目標(biāo)方法執(zhí)行后
* 該方法在控制器處理請(qǐng)求方法調(diào)用之后、解析視圖之前執(zhí)行
* 可以通過(guò)此方法對(duì)請(qǐng)求域中的模型和視圖做進(jìn)一步修改
*/
@Override
public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView
modelAndView) throws Exception {
System.out.println("postHandle執(zhí)行{}");
}
/**
* 頁(yè)面渲染后
* 該方法在視圖渲染結(jié)束后執(zhí)行
* 可以通過(guò)此方法實(shí)現(xiàn)資源清理、記錄日志信息等工作
*/
@Override
public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception
ex) throws Exception {
System.out.println("afterCompletion執(zhí)行異常");
}
}注解使用
在controller層加入注解進(jìn)行測(cè)試

返回值-全局異常類(lèi)定義
加入全局,異常類(lèi),這樣當(dāng)異常,會(huì)返回你所指定的異常
package com.example.etf.story.tools;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice
public class GloablExceptionHandler {
@ResponseBody
@ExceptionHandler(Exception.class)
public Object handleException(Exception e) {
String msg = e.getMessage();
if (msg == null || msg.equals("")) {
msg = "服務(wù)器出錯(cuò)";
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("message", msg);
jsonObject.put("status",500)
return jsonObject;
}
}各種測(cè)試
不傳token
成功

制造可行的假token
我們測(cè)試一下加token后的


因?yàn)閿?shù)據(jù)庫(kù)里,我沒(méi)有插入,所以不存在,我們?cè)陔S便寫(xiě)個(gè)token
偽造token測(cè)試

我們?cè)谠囋?/p>
程序員使用:方法不加注解,測(cè)試

程序員使用:加上,調(diào)用通過(guò),注解
我們?cè)囋嚕由贤ㄟ^(guò)注解


拓展:從請(qǐng)求中獲取token
我們?cè)谠囋噺闹蝎@取token


參考文章:
SpringBoot集成JWT實(shí)現(xiàn)token驗(yàn)證 - 簡(jiǎn)書(shū)
到此這篇關(guān)于SpringBoot登錄驗(yàn)證token攔截器的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot token攔截器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot實(shí)現(xiàn)token驗(yàn)證登陸狀態(tài)的示例代碼
- SpringBoot整合JWT(JSON?Web?Token)生成token與驗(yàn)證的流程及示例
- springboot+shiro+jwtsession和token進(jìn)行身份驗(yàn)證和授權(quán)
- SpringBoot集成JWT實(shí)現(xiàn)Token登錄驗(yàn)證的示例代碼
- 實(shí)戰(zhàn)SpringBoot集成JWT實(shí)現(xiàn)token驗(yàn)證
- Springboot 如何實(shí)現(xiàn)filter攔截token驗(yàn)證和跨域
- SpringBoot整合JWT框架,解決Token跨域驗(yàn)證問(wèn)題
- SpringBoot集成JWT實(shí)現(xiàn)token驗(yàn)證的流程
- SpringBoot下token短信驗(yàn)證登入登出權(quán)限操作(token存放redis,ali短信接口)
- Spring boot+VUE實(shí)現(xiàn)token驗(yàn)證的示例代碼
相關(guān)文章
SpringBoot集成企業(yè)微信開(kāi)發(fā)的實(shí)現(xiàn)
本文將詳細(xì)介紹如何使用?Spring?Boot?集成企業(yè)微信開(kāi)發(fā),通過(guò)企業(yè)微信?API?可以實(shí)現(xiàn)企業(yè)內(nèi)部的一些自動(dòng)化業(yè)務(wù)流程,提高工作效率,感興趣的可以了解一下2023-07-07
Maven多個(gè)項(xiàng)目實(shí)現(xiàn)聚合過(guò)程解析
這篇文章主要介紹了Maven多個(gè)項(xiàng)目實(shí)現(xiàn)聚合過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
基于JAVA的短信驗(yàn)證碼api調(diào)用代碼實(shí)例
這篇文章主要為大家詳細(xì)介紹了基于JAVA的短信驗(yàn)證碼api調(diào)用代碼實(shí)例,感興趣的小伙伴們可以參考一下2016-05-05
httpclient 請(qǐng)求http數(shù)據(jù),json轉(zhuǎn)map的實(shí)例
下面小編就為大家?guī)?lái)一篇httpclient 請(qǐng)求http數(shù)據(jù),json轉(zhuǎn)map的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
spring 定時(shí)任務(wù)@Scheduled詳解
這篇文章主要介紹了spring 定時(shí)任務(wù)@Scheduled的相關(guān)資料,文中通過(guò)示例代碼介紹的很詳細(xì),相信對(duì)大家的理解和學(xué)習(xí)具有一定的參考借鑒價(jià)值,有需要的朋友們下面來(lái)一起看看吧。2017-01-01
java Socket實(shí)現(xiàn)多人群聊與私聊功能
這篇文章主要為大家詳細(xì)介紹了java Socket實(shí)現(xiàn)多人群聊與私聊功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07

