javax.servlet.http.HttpServletRequest;和jakarta.servlet.http.HttpServletRequest request的问题吧
1.注册拦截器成功了,但是却一直没有进入到拦截器里面。
我自己的问题:我的拦截器里面的代码一直是复制网上的,然后拦截器一直未能生效。
package com.example.library.interceptor;
import com.example.library.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
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;
import java.util.Map;
/**
* jwt令牌校验的拦截器
*/
@Component
@Slf4j
public class JwtTokenInterceptor implements HandlerInterceptor {
/**
* 校验jwt
*
* @return
* @throws Exception
*/
//目标资源方法执行前执行,返回true则放行;放回false,不放行
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws Exception {
log.info("=======进入拦截器========");
// 如果不是映射到方法直接通过,可以访问资源.
if (!(object instanceof HandlerMethod)) {
return true;
}
//为空就返回错误
String token = httpServletRequest.getHeader("token");
if (null == token || "".equals(token.trim())) {
return false;
}
//校验令牌
if (JwtUtil.checkToken(token)) {
log.info("==============token:" + token);
Map<String, Object> map = JwtUtil.parseToken(token);
return true;
} else {
throw new RuntimeException("token无效");
}
}
}
使用ctel+O自动重写里面的方法,然后自己填入对应的逻辑,就可以了。
package com.example.library.interceptor;
import com.example.library.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
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.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
/**
* jwt令牌校验的拦截器
*/
@Component
public class JwtTokenInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Object handler) throws Exception {
System.out.println("=======进入拦截器========");
// 如果不是映射到方法直接通过,可以访问资源.
if (!(handler instanceof HandlerMethod)) {
return true;
}
//为空就返回错误
String token = request.getHeader("token");
if (token == null || "".equals(token.trim())) {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "缺少令牌");
return false;
}
//校验令牌
if (JwtUtil.checkToken(token)) {
System.out.println("==============token:" + token);
return true;
} else {
throw new RuntimeException("token无效");
}
}
@Override
public void postHandle(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Object handler, Exception ex) throws Exception {
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}