Spring Boot 对 favicon.ico 的请求导致访问被拒绝异常

问题描述 投票:0回答:1

我有一个 Spring Boot 应用程序,它不是 Web 应用程序,它只是一个提供数据的 REST API 应用程序。当我从终端或邮递员请求本地主机 URL 时,它工作正常。但是,当我从浏览器请求相同的 URL 时,它会给出结果,但在

TRACE
日志记录模式下,它还会打印 Access Denied 异常。因为它还在后台向
/favicon.ico
发送请求。我想摆脱这个异常,但我找不到合适的解决方案。

spring.mvc.favicon.enabled
在 Spring Boot 2.7 版本中已弃用。所以,不能再用了。

2024-07-30T11:37:12.142+03:00 DEBUG 1963555 --- [nio-8089-exec-2] o.s.security.web.FilterChainProxy        : Securing GET /favicon.ico
2024-07-30T11:37:12.142+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.security.web.FilterChainProxy        : Invoking DisableEncodeUrlFilter (1/14)
2024-07-30T11:37:12.143+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.security.web.FilterChainProxy        : Invoking WebAsyncManagerIntegrationFilter (2/14)
2024-07-30T11:37:12.143+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.security.web.FilterChainProxy        : Invoking SecurityContextHolderFilter (3/14)
2024-07-30T11:37:12.143+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.security.web.FilterChainProxy        : Invoking HeaderWriterFilter (4/14)
2024-07-30T11:37:12.143+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.security.web.FilterChainProxy        : Invoking CorsFilter (5/14)
2024-07-30T11:37:12.143+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.security.web.FilterChainProxy        : Invoking LogoutFilter (6/14)
2024-07-30T11:37:12.143+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.s.w.a.logout.LogoutFilter            : Did not match request to Or [Ant [pattern='/logout', GET], Ant [pattern='/logout', POST], Ant [pattern='/logout', PUT], Ant [pattern='/logout', DELETE]]
2024-07-30T11:37:12.143+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.security.web.FilterChainProxy        : Invoking BearerTokenAuthenticationFilter (7/14)
2024-07-30T11:37:12.143+03:00 TRACE 1963555 --- [nio-8089-exec-2] .s.r.w.a.BearerTokenAuthenticationFilter : Did not process request since did not find bearer token
2024-07-30T11:37:12.143+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.security.web.FilterChainProxy        : Invoking BasicAuthenticationFilter (8/14)
2024-07-30T11:37:12.143+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.s.w.a.www.BasicAuthenticationFilter  : Did not process authentication request since failed to find username and password in Basic Authorization header
2024-07-30T11:37:12.143+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.security.web.FilterChainProxy        : Invoking RequestCacheAwareFilter (9/14)
2024-07-30T11:37:12.143+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.security.web.FilterChainProxy        : Invoking SecurityContextHolderAwareRequestFilter (10/14)
2024-07-30T11:37:12.144+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.security.web.FilterChainProxy        : Invoking AnonymousAuthenticationFilter (11/14)
2024-07-30T11:37:12.144+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.security.web.FilterChainProxy        : Invoking SessionManagementFilter (12/14)
2024-07-30T11:37:12.144+03:00 TRACE 1963555 --- [nio-8089-exec-2] .s.s.w.c.SupplierDeferredSecurityContext : Created SecurityContextImpl [Null authentication]
2024-07-30T11:37:12.144+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.s.w.a.AnonymousAuthenticationFilter  : Set SecurityContextHolder to AnonymousAuthenticationToken [Principal=anonymousUser, Credentials=[PROTECTED], Authenticated=true, Details=WebAuthenticationDetails [RemoteIpAddress=127.0.0.1, SessionId=null], Granted Authorities=[ROLE_ANONYMOUS]]
2024-07-30T11:37:12.144+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.security.web.FilterChainProxy        : Invoking ExceptionTranslationFilter (13/14)
2024-07-30T11:37:12.144+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.security.web.FilterChainProxy        : Invoking AuthorizationFilter (14/14)
2024-07-30T11:37:12.144+03:00 TRACE 1963555 --- [nio-8089-exec-2] estMatcherDelegatingAuthorizationManager : Authorizing GET /favicon.ico
2024-07-30T11:37:12.144+03:00 TRACE 1963555 --- [nio-8089-exec-2] estMatcherDelegatingAuthorizationManager : Denying request since did not find matching RequestMatcher
2024-07-30T11:37:12.148+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.b.f.s.DefaultListableBeanFactory     : Returning cached instance of singleton bean 'delegatingApplicationListener'
2024-07-30T11:37:12.148+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.b.f.s.DefaultListableBeanFactory     : Returning cached instance of singleton bean 'refreshEventListener'
2024-07-30T11:37:12.148+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.b.f.s.DefaultListableBeanFactory     : Returning cached instance of singleton bean 'startupTimeMetrics'
2024-07-30T11:37:12.148+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.b.f.s.DefaultListableBeanFactory     : Returning cached instance of singleton bean 'springApplicationAdminRegistrar'
2024-07-30T11:37:12.148+03:00 TRACE 1963555 --- [nio-8089-exec-2] o.s.s.w.a.ExceptionTranslationFilter     : Sending AnonymousAuthenticationToken [Principal=anonymousUser, Credentials=[PROTECTED], Authenticated=true, Details=WebAuthenticationDetails [RemoteIpAddress=127.0.0.1, SessionId=null], Granted Authorities=[ROLE_ANONYMOUS]] to authentication entry point since access is denied

org.springframework.security.access.AccessDeniedException: Access Denied
    at org.springframework.security.web.access.intercept.AuthorizationFilter.doFilter(AuthorizationFilter.java:98) ~[spring-security-web-6.3.1.jar:6.3.1]
    at org.springframework.security.web.ObservationFilterChainDecorator$ObservationFilter.wrapFilter(ObservationFilterChainDecorator.java:240) ~[spring-security-web-6.3.1.jar:6.3.1]
    at org.springframework.security.web.ObservationFilterChainDecorator$ObservationFilter.doFilter(ObservationFilterChainDecorator.java:227) ~[spring-security-web-6.3.1.jar:6.3.1]
    at org.springframework.security.web.ObservationFilterChainDecorator$VirtualFilterChain.doFilter(ObservationFilterChainDecorator.java:137) ~[spring-security-web-6.3.1.jar:6.3.1]
    ....

此外,当我对

/favicon.ico
端点使用 .permitAll() 时,它会转发到
/error
端点,因为找不到
/favicon.ico
。我可以创建一个虚拟
/favicon.ico
端点,以便在收到请求时不返回任何内容,但这似乎不是最佳实践。应该有更好的方法来解决这个问题。

我正在使用Spring Boot 3.3.2版本。

java spring-boot spring-mvc
1个回答
0
投票

将这一小段代码放入您网站的标题部分。浏览器会很高兴,不会再要求提供图标。

<link rel="icon" href="data:;base64,=">

© www.soinside.com 2019 - 2024. All rights reserved.