java - Spring 安全: do not require authentication for local request

标签 java spring spring-security

我有一个带有几个 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/

相关文章:

java - 如何将jdk 1.8安装到jdeveloper 12.1.3.0.0中

spring - 使用 Spring Security 的在线用户

spring - org.springframework.web.filter.HiddenHttpMethodFilter 不能转换为 javax.servlet.Filter

java - 如何访问 Spring Soap 端点中的 SOAP header ?

java - Spring MVC - 上传文件被spring security阻止

java - 无静态方法元工厂(OptaPlanner)

java - 如何在 ibm i 服务器上运行 java 程序

java - 一段时间后杀死java中的线程

java - 如何在JHipster中为FeignClient启用Autowired?

java - 是否可以在 Mule Flow 中添加安全 url,例如 Spring Security?