我有一个带有几个 REST Controller 的 Spring Boot Web 应用程序。它使用 Spring Security 进行基于表单的身份验证(1)
此外,应用程序正在读取存储的请求并将它们分派(dispatch)给自身。为此,我有一个自定义 Spring Dispatcher servlet 和一个用于存储请求的过滤器链。调用时,它让 Spring DispatcherServlet 将 HTTP 请求分派(dispatch)到正确的 REST Controller (2)
我想设置 Spring 安全性,使其不需要对 (2) 情况进行身份验证 - 即 http 请求来自应用程序本身。
有人知道如何实现这一目标吗?
最佳答案
您可以创建自定义 RequestMatcher
:
Simple strategy to match an
HttpServletRequest
.
用你的逻辑来决定是否应该忽略该请求。
然后使用 AbstractRequestMatcherRegistry#requestMatchers
添加自定义 RequestMatcher
:
Associates a list of RequestMatcher instances with the
AbstractConfigAttributeRequestMatcherRegistry
到被忽略的 RequestMatcher
列表,其中 WebSecurity#ignoring
:
Allows adding
RequestMatcher
instances that Spring Security should ignore.
在你的WebSecurityConfigurerAdapter#configure
中方法:
Override this method to configure
WebSecurity
. For example, if you wish to ignore certain requests.
例如:
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.requestMatchers(new MyInternalRequestMatcher());
}
关于java - Spring 安全: do not require authentication for local request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43469736/