tiaya01 2024-10-31 15:21 采纳率: 76.2%
浏览 6
已结题

为什么加上Interceptor之后就会出现无token问题?

为什么加上Interceptor之后就会出现这个问题?

img

JwtInterceptor:

package com.practice.springboot.config.interceptor;

import cn.hutool.core.util.StrUtil;
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.exceptions.JWTVerificationException;
import com.practice.springboot.entity.User;
import com.practice.springboot.exception.ServiceException;
import com.practice.springboot.result.Constants;
import com.practice.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class JwtInterceptor implements HandlerInterceptor {

    @Autowired
    private UserService userService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
        String token=request.getHeader("token");
        //如果不是映射方法直接通过
        if(!(handler instanceof HandlerMethod)){
            return true;
        }

        if(StrUtil.isBlank(token)){
            throw new ServiceException(Constants.CODE_401,"无token,请重新登录");
        }

        //获取token中的userId
        String userId;
        try{
            userId= JWT.decode(token).getAudience().get(0);
        }catch(JWTDecodeException j){
            throw new ServiceException(Constants.CODE_401,"token验证失败,请重新登陆");
        }
        //根据token中的userId查询数据库
        User user=userService.getById(userId);
        if(user==null){
            throw new ServiceException(Constants.CODE_401,"用户不存在,请重新登陆");
        }
        //用户密码加签验证token
        JWTVerifier jwtVerifier=JWT.require(Algorithm.HMAC256(user.getPassword())).build();
        try{
            jwtVerifier.verify(token);//验证token
        }catch(JWTVerificationException e){
            throw new ServiceException(Constants.CODE_401,"token验证失败,请重新登陆");
        }
        return true;
    }
}

Interceptor:
```java

package com.practice.springboot.config;

import com.practice.springboot.config.interceptor.JwtInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login/");//拦截所有请求,通过判断token是否合法决定是否需要登录
    }

    @Bean
    public JwtInterceptor jwtInterceptor() {
        return new JwtInterceptor();
    }

}


  • 写回答

1条回答 默认 最新

  • bingbingyihao 2024-10-31 20:01
    关注

    回答:代码看着没啥问题,但是你的网络请求截图截少了,很关键的负载和请求相关的信息都没截到,然后还需要注意,有一种请求叫做预检请求,preflight请求,它一般是不携带验证信息的,只会有host相关的信息,你看一下你是不是取的是预检请求的返回,然后需要将这种请求给过滤掉,即 OPTIONS类型的请求

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月17日
  • 已采纳回答 12月9日
  • 创建了问题 10月31日