我有以下 Spring Security 配置:
httpSecurity
.csrf().disable()
.exceptionHandling()
.authenticationEntryPoint(unauthorizedHandler)
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/api/**").fullyAuthenticated()
.and()
.addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);
authenticationTokenFilterBean()
甚至应用于与 /api/**
表达式不匹配的端点。我还尝试添加以下配置代码:
@Override
public void configure(WebSecurity webSecurity) {
webSecurity.ignoring().antMatchers("/some_endpoint");
}
但这仍然没有解决我的问题。我如何告诉 Spring Security 仅在与安全 URI 表达式匹配的端点上应用过滤器?
原文由 Bravo 发布,翻译遵循 CC BY-SA 4.0 许可协议
我有一个具有相同要求的应用程序,为了解决它,我基本上将 Spring Security 限制为给定的 ant 匹配模式(使用
antMatcher
),如下所示:You can read it as follows: for
http
only invoke these configurations on requests matching the ant pattern/api/**
authorizingany request
toauthenticated
usersand
add filter
authenticationTokenFilterBean()
before
UsernamePasswordAuthenticationFilter
.对于所有其他请求,此配置无效。