最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot登錄驗證token攔截器的實現(xiàn)

 更新時間:2022年07月07日 11:07:31   作者:我要用代碼向我喜歡的女孩表白  
本文主要介紹了SpringBoot登錄驗證token攔截器的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

用戶訪問接口驗證,如果用戶沒有登錄,則不讓他訪問除登錄外的任何接口。

實現(xiàn)思路:

1.前端登錄,后端創(chuàng)建token(通過JWT這個依賴),返給前端

2.前端訪問其他接口,傳遞token,后端判斷token存在以或失效

3.失效或不存在,則返回失效提示,前端根據(jù)接口返回的失效提示,讓其跳轉(zhuǎn)到登錄界面

注解定義

定義2個注解,1個用于任何接口都能訪問,另外一個用于需要登錄才能訪問

調(diào)用都通過注解

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;
}

登錄才能通過

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;
}

注解的作用說明

@Target代表此注解,能@到哪些代碼上

@Target:注解的作用目標(biāo)

  • @Target(ElementType.TYPE)——接口、類、枚舉、注解
  • @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:這種類型的Annotations只在源代碼級別保留,編譯時就會被忽略,在class字節(jié)碼文件中不包含。
  • RetentionPolicy.CLASS:這種類型的Annotations編譯時被保留,默認(rèn)的保留策略,在class文件中存在,但JVM將會忽略,運行時無法獲得。
  • RetentionPolicy.RUNTIME:這種類型的Annotations將被JVM保留,所以他們能在運行時被JVM或其他使用反射機(jī)制的代碼所讀取和使用。
  • @Document:說明該注解將被包含在javadoc
  • @Inherited:說明子類可以繼承父類中的該注解

token生成與驗證

傳送門

然后springBoot攔截器驗證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) {
        //注冊自己的攔截器,并設(shè)置攔截的請求路徑
        //addPathPatterns為攔截此請求路徑的請求
        //excludePathPatterns為不攔截此路徑的請求
        registry.addInterceptor(loginInterceptor).addPathPatterns("/story/*").excludePathPatterns("/story/sendSMS")
                .excludePathPatterns("/story/signOrRegister");
    }
}

攔截的時候,調(diào)用的方法,給誰通過

其中service查詢數(shù)據(jù)庫,有沒有用戶,的方法要自己寫

攔截器的方法執(zhí)行類

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í)行前
     * 該方法在控制器處理請求方法前執(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 請求頭中取出 token
        // 如果不是映射到方法直接通過
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
 
        //檢查方法是否有passtoken注解,有則跳過認(rèn)證,直接通過
        if (method.isAnnotationPresent(PassToken.class)) {
            PassToken passToken = method.getAnnotation(PassToken.class);
            if (passToken.required()) {
                return true;
            }
        }
        //檢查有沒有需要用戶權(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("無token,請重新登錄");
                }
                // 獲取 token 中的 user id
                String phone;
                try {
                    phone = JWT.decode(token).getClaim("phone").asString();
                } catch (JWTDecodeException j) {
                    throw new RuntimeException("token不正確,請不要通過非法手段創(chuàng)建token");
                }
                //查詢數(shù)據(jù)庫,看看是否存在此用戶,方法要自己寫
                UserInfoParam userInfoParam = testClientService.selectUserByPhone(phone);
                if (userInfoParam == null) {
                    throw new RuntimeException("用戶不存在,請重新登錄");
                }
 
                // 驗證 token
                if (TokenUtils.verify(token)) {
                    return true;
                } else {
                    throw new RuntimeException("token過期或不正確,請重新登錄");
                }
 
            }
        }
          throw new RuntimeException("沒有權(quán)限注解一律不通過");
    }
 
 
 
 
        /**
         * 目標(biāo)方法執(zhí)行后
         * 該方法在控制器處理請求方法調(diào)用之后、解析視圖之前執(zhí)行
         * 可以通過此方法對請求域中的模型和視圖做進(jìn)一步修改
         */
        @Override
        public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView
        modelAndView) throws Exception {
            System.out.println("postHandle執(zhí)行{}");
 
        }
        /**
         * 頁面渲染后
         * 該方法在視圖渲染結(jié)束后執(zhí)行
         * 可以通過此方法實現(xiàn)資源清理、記錄日志信息等工作
         */
        @Override
        public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception
        ex) throws Exception {
            System.out.println("afterCompletion執(zhí)行異常");
 
        }
 
}

注解使用

在controller層加入注解進(jìn)行測試

返回值-全局異常類定義

加入全局,異常類,這樣當(dāng)異常,會返回你所指定的異常

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ù)器出錯";
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("message", msg);
        jsonObject.put("status",500)
        return jsonObject;
    }
}

各種測試

不傳token

成功

制造可行的假token

我們測試一下加token后的

 因為數(shù)據(jù)庫里,我沒有插入,所以不存在,我們在隨便寫個token

偽造token測試

我們在試試

程序員使用:方法不加注解,測試

程序員使用:加上,調(diào)用通過,注解

 我們試試,加上通過注解

拓展:從請求中獲取token

我們在試試從中獲取token

參考文章:

SpringBoot集成JWT實現(xiàn)token驗證 - 簡書

springboot對請求的接口實現(xiàn)token攔截以及參數(shù)校驗_kotomeli的博客-CSDN博客_springboot攔截請求參數(shù)

到此這篇關(guān)于SpringBoot登錄驗證token攔截器的實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot token攔截器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java實現(xiàn)飛機(jī)游戲代碼

    java實現(xiàn)飛機(jī)游戲代碼

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)飛機(jī)游戲代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • java ArrayList中的remove方法介紹

    java ArrayList中的remove方法介紹

    大家好,本篇文章主要講的是java ArrayList中的remove方法介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • mybatis-plus版本不兼容問題的解決

    mybatis-plus版本不兼容問題的解決

    本文主要介紹了mybatis-plus與spring-boot3版本不兼容導(dǎo)致的BeanDefinitionStoreException問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-12-12
  • java集合類arraylist循環(huán)中刪除特定元素的方法

    java集合類arraylist循環(huán)中刪除特定元素的方法

    下面小編就為大家?guī)硪黄狫ava集合類ArrayList循環(huán)中刪除特定元素的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • IDEA中啟動多個SpringBoot服務(wù)的實現(xiàn)示例

    IDEA中啟動多個SpringBoot服務(wù)的實現(xiàn)示例

    本文主要介紹了IDEA中啟動多個SpringBoot服務(wù)的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08
  • SpringBoot2中使用@RequestHeader獲取請求頭的方法

    SpringBoot2中使用@RequestHeader獲取請求頭的方法

    springMVC/SpringBoot中提供了@RequestHeader注解用來獲取請求頭。本文就詳細(xì)的來介紹一下如何使用,感興趣的可以了解下
    2021-10-10
  • Dubbo無法訪問遠(yuǎn)程Zookeeper已注冊服務(wù)的問題解決方案

    Dubbo無法訪問遠(yuǎn)程Zookeeper已注冊服務(wù)的問題解決方案

    今天小編就為大家分享一篇關(guān)于Dubbo無法訪問遠(yuǎn)程Zookeeper已注冊服務(wù)的問題解決方案,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • SpringBoot中的@Configuration注解詳解

    SpringBoot中的@Configuration注解詳解

    這篇文章主要介紹了SpringBoot中的@Configuration注解詳解,Spring Boot推薦使用JAVA配置來完全代替XML 配置,JAVA配置就是通過 @Configuration和 @Bean兩個注解實現(xiàn)的,需要的朋友可以參考下
    2023-08-08
  • java通過實例了解值傳遞和引用傳遞

    java通過實例了解值傳遞和引用傳遞

    這篇文章主要介紹了java通過實例了解值傳遞和引用傳遞,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-11-11
  • 線程池ThreadPoolExecutor并行處理實現(xiàn)代碼

    線程池ThreadPoolExecutor并行處理實現(xiàn)代碼

    這篇文章主要介紹了線程池ThreadPoolExecutor并行處理實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11

最新評論

呼伦贝尔市| 屯留县| 翼城县| 神木县| 五大连池市| 班玛县| 永寿县| 榕江县| 南部县| 溆浦县| 卫辉市| 东山县| 汾西县| 杭锦旗| 平安县| 阿克苏市| 康马县| 丹凤县| 滨州市| 洪江市| 方正县| 巴楚县| 天门市| 棋牌| 库车县| 栾川县| 石河子市| 郓城县| 白水县| 尚志市| 德阳市| 肃宁县| 邹平县| 名山县| 镇安县| 通化市| 台前县| 忻城县| 子长县| 余姚市| 青岛市|