• JWT登录认证(3拦截器)


    Jwt登录认证(拦截器):

            使用拦截器统一验证令牌

            登录和注册接口需要放行

    interceptors.LoginInterceptor:(注册一个拦截器)

    1. package com.lin.springboot01.interceptors;
    2. import com.lin.springboot01.pojo.Result;
    3. import com.lin.springboot01.utils.JwtUtil;
    4. import jakarta.servlet.http.HttpServletRequest;
    5. import jakarta.servlet.http.HttpServletResponse;
    6. import org.springframework.stereotype.Component;
    7. import org.springframework.web.servlet.HandlerInterceptor;
    8. import java.util.Map;
    9. @Component
    10. public class LoginInterceptor implements HandlerInterceptor {
    11. @Override
    12. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    13. String token = request.getHeader("Authorization");
    14. try {
    15. //能否解析成功
    16. Map claims = JwtUtil.parseToken(token);
    17. //放行
    18. return true;
    19. } catch (Exception e) {
    20. //解析失败,httpServletResponse响应码设置为401
    21. response.setStatus(401);
    22. return false;
    23. }
    24. }
    25. }

    config.WebConfig:(在config配置项中配置拦截器)

    1. package com.lin.springboot01.config;
    2. import com.lin.springboot01.interceptors.LoginInterceptor;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.context.annotation.Configuration;
    5. import org.springframework.validation.annotation.Validated;
    6. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    7. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    8. @Configuration
    9. public class WebConfig implements WebMvcConfigurer {
    10. @Autowired
    11. private LoginInterceptor loginInterceptor;
    12. @Override
    13. public void addInterceptors(InterceptorRegistry registry) {
    14. //登录接口和注册接口不拦截
    15. registry.addInterceptor(loginInterceptor).excludePathPatterns("/user/login","/user/register");
    16. }
    17. }

    controller.ArticleController

    1. package com.lin.springboot01.controller;
    2. import com.lin.springboot01.pojo.Result;
    3. import com.lin.springboot01.utils.JwtUtil;
    4. import jakarta.servlet.http.HttpServletResponse;
    5. import org.springframework.web.bind.annotation.GetMapping;
    6. import org.springframework.web.bind.annotation.RequestHeader;
    7. import org.springframework.web.bind.annotation.RequestMapping;
    8. import org.springframework.web.bind.annotation.RestController;
    9. import java.util.Map;
    10. @RestController
    11. @RequestMapping("/article")
    12. public class ArticleController {
    13. @GetMapping("/list")
    14. public Result list(/*@RequestHeader(name = "Authorization") String token, HttpServletResponse response*/){
    15. //验证token
    16. /* try {
    17. Map claims = JwtUtil.parseToken(token); //parseToken是JwtUtil工具类里定义的解析token的方法
    18. return Result.success("可以展示数据");
    19. } catch (Exception e) {
    20. response.setStatus(401);
    21. return Result.error("未登录");
    22. }*/
    23. return Result.success("可以展示数据");
    24. }
    25. }

  • 相关阅读:
    基于大模型的单轮文档问答
    windows 11 install windows subsystem for android
    APIMapper 源码解析
    Invalid access token: Invalid header string: ‘utf-8‘ codec can‘t decode byte
    Linux常用命令
    ADSP-21489的图形化编程详解(1:硬件的准备和软件环境的搭建)
    使用 Alice inspector 和 Dio 进行 Flutter API 日志记录
    我的OpenAI库发布了!!!
    MYSQL第五章节有关约束操作详解(附代码,例题详解)这一篇就够了
    【web-渗透测试方法】(15.5)测试访问控件
  • 原文地址:https://blog.csdn.net/m0_71088505/article/details/134454829