gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:49:00 24 4
gpt4 key购买 nike

我有一个带有几个 REST Controller 的 Spring Boot Web 应用程序。它使用 Spring Security 进行基于表单的身份验证(一)

此外,应用程序正在读取存储的请求并将它们分派(dispatch)给自己。为此,我有一个自定义的 Spring Dispatcher servlet 和一个用于存储请求的过滤器链。调用时,它让 Spring DispatcherServlet 将 HTTP 请求分派(dispatch)到正确的 REST Controller (2)

我想以一种不需要对 (2) 情况进行身份验证的方式设置 Spring 安全性 - 即当 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com