java Sping Boot ,Spring Security返回状态401,而不是404,因为“没有找到HTTP请求的Map”

ecbunoof  于 2023-09-29  发布在  Java
关注(0)|答案(3)|浏览(101)

我正在使用Sping Boot 和Spring Security进行项目。Spring Security使用每个请求的session id验证header。如果会话ID无效或已过期,则将返回错误代码401。会话ID在到达控制器之前进行验证。
现在,我面临一个问题,如果用户输入一个没有有效会话ID的无效URL,响应代码仍然是401,因为会话ID首先被验证。我的预期是,如果URL无效,将返回错误代码404(未找到HTTP请求的Map)。换句话说,我想在验证会话ID之前验证URL。
有没有办法这样做,因为头中的会话id在到达控制器之前在GenericFilterBean中进行了验证?
任何帮助都是感激不尽的。谢谢

webghufk

webghufk1#

您可以尝试在WebSecurityConfigurerAdapter类中配置访问设置。

  1. @Override
  2. protected void configure(HttpSecurity http) throws Exception {
  3. http
  4. .authorizeRequests()
  5. .antMatchers("/secure/**").authenticated()
  6. .and()
  7. .authorizeRequests().anyRequest().permitAll();
  8. }

因此,过滤器不会为任何不匹配“/secure/**”模式的请求返回HTTP 401。

iibxawm4

iibxawm42#

将此过滤器作为Spring Security中的第一个过滤器:

  1. public class NoHandlerFoundFilter extends OncePerRequestFilter {
  2. private final DispatcherServlet dispatcherServlet;
  3. public NoHandlerFoundFilter(DispatcherServlet dispatcherServlet) {
  4. this.dispatcherServlet = dispatcherServlet;
  5. }
  6. @Override
  7. protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
  8. if (null == getHandler(request)) {
  9. throw new NoHandlerFoundException(request.getMethod(), getRequestUri(request),
  10. new ServletServerHttpRequest(request).getHeaders());
  11. }
  12. filterChain.doFilter(request, response);
  13. }
  14. private static String getRequestUri(HttpServletRequest request) {
  15. String uri = (String) request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE);
  16. if (uri == null) {
  17. uri = request.getRequestURI();
  18. }
  19. return uri;
  20. }
  21. protected HandlerExecutionChain getHandler(HttpServletRequest request) {
  22. if (dispatcherServlet.getHandlerMappings() != null) {
  23. for (HandlerMapping mapping : dispatcherServlet.getHandlerMappings()) {
  24. try {
  25. HandlerExecutionChain handler = mapping.getHandler(request);
  26. if (handler != null) {
  27. return handler;
  28. }
  29. } catch (Exception ex) {
  30. // Ignore
  31. }
  32. }
  33. }
  34. return null;
  35. }
  36. }
展开查看全部
oknwwptz

oknwwptz3#

  1. @Order(Ordered.HIGHEST_PRECEDENCE)
  2. @Component
  3. @Slf4j
  4. @WebFilter
  5. public class NoHandlerFoundFilter extends OncePerRequestFilter {
  6. private final DispatcherServlet dispatcherServlet;
  7. public NoHandlerFoundFilter(DispatcherServlet dispatcherServlet) {
  8. this.dispatcherServlet = dispatcherServlet;
  9. }
  10. @Override
  11. protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
  12. try{
  13. boolean flag = validateUrl(request, response);
  14. if(flag)
  15. filterChain.doFilter(request, response);
  16. }catch (Exception ex){
  17. log.error("URL Filter Chain Exception:", ex);
  18. //throw new UrlNotFoundException(request, response, getRequestUri(request), ex);
  19. }
  20. }
  21. private boolean validateUrl(HttpServletRequest request, HttpServletResponse response) throws NoHandlerFoundException, IOException {
  22. if (null == getHandler(request)) {
  23. Status errorStatus = this.getErrorStatus(HttpStatus.NOT_FOUND.toString(), "URL not found", null);
  24. PlatformResponse errorResponse = new PlatformResponse();
  25. errorResponse.setStatus(errorStatus);
  26. response.setStatus(HttpStatus.NOT_FOUND.value());
  27. response.getWriter().write(objectMapper.writeValueAsString(errorResponse));
  28. return false;
  29. //throw new UrlNotFoundException(request, response, "test", null);
  30. }
  31. return true;
  32. }
  33. private static String getRequestUri(HttpServletRequest request) {
  34. String uri = (String) request.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE);
  35. if (uri == null) {
  36. uri = request.getRequestURI();
  37. }
  38. return uri;
  39. }
  40. protected HandlerExecutionChain getHandler(HttpServletRequest request) {
  41. if (dispatcherServlet.getHandlerMappings() != null) {
  42. for (HandlerMapping mapping : dispatcherServlet.getHandlerMappings()) {
  43. try {
  44. HandlerExecutionChain handler = mapping.getHandler(request);
  45. if (handler != null) {
  46. return handler;
  47. }
  48. } catch (Exception ex) {
  49. // Ignore
  50. }
  51. }
  52. }
  53. return null;
  54. }
  55. }

稍微修改一下答案。
适用于springBootVersion: 3

展开查看全部

相关问题