我们面临着SpringSecurity忽略一个方法的问题。我们试图跳过一些urls (acutator/health)和资源的身份验证。身份验证是在外部进行的,我们有一个自定义过滤器来提取授权原则。
我们覆盖配置的方法,如下所示:
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/**", "/actuator/health");
}
protected void configure(HttpSecurity http) throws Exception {
http.addFilter(cutstomFilter).authorizeRequests()
.antMatchers("/add","/update","/upload").hasAuthority("ADMIN").anyRequest().authenticated()
.and().logout().logoutSuccessUrl("/logoutUser").and()
.exceptionHandling().accessDeniedPage("/accessDenied").and().csrf().disable();
}
在给定的实现中,我们的customFilter被要求提供资源和健康url。这是由于原则的改变而导致的重新认证。
我们尝试添加这段代码,但是customFilter也会被调用为health。
http.authorizeRequests().antMatchers("/actuator/health").permitAll()
注意:检查了@Rob的答案,但不明白如果我们将这些url放在忽略列表中,为什么需要一个自定义的文件处理程序。https://stackoverflow.com/a/19985323/2138633
发布于 2020-12-24 12:13:19
更新:请参阅@dur中的评论,它可能会解决问题,而不会有重大变化。
To make it clear, your first security configuration is correct. Your problem
is that your filter is used as a servlet filter not only as a security chain
filter. Spring Boot does this autmatically, if you expose your filter.
https://stackoverflow.com/a/39314867/14072498
OP提到的是执行器的端点。让我们看看doc:https://spring.io/guides/topicals/spring-security-architecture
医生说:
If you want your application security rules to apply to the actuator
endpoints, you can add a filter chain that is ordered earlier than the
actuator one and that has a request matcher that includes all actuator
endpoints.
Doc建议将配置划分为多个WebSecurityConfigurerAdapter
实现。
在下面的示例配置中,您应该将您所指的自定义筛选器应用于MainAppConfigurerAdapter
。
“多弹簧启动安全配置”示例:https://medium.com/@igor.bonny/multiple-spring-boot-security-configuration-c876f1b6061e
为了跳过其他端点的身份验证,请添加
.and()
.authorizeRequests().anyRequest().permitAll();
以结束应用程序链,如下所示。
若要验证安全设置,请为所有端点添加集成测试。
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration {
@Configuration
@Order(ManagementServerProperties.BASIC_AUTH_ORDER - 1)
public class ActuatorConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) {
http.antMatcher("/actuator/**")
...
}
}
@Configuration
@Order(SecurityProperties.DEFAULT_FILTER_ORDER)
public class MainAppConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) {
http.antMatcher("/api/**")
...
}
}
}
发布于 2020-12-24 13:54:34
management.endpoints.web.exposure.include=*
@Configuration(proxyBeanMethods = false)公共类ActuatorSecurity扩展WebSecurityConfigurerAdapter {@覆盖受保护的无效配置(HttpSecurity http)抛出异常{HttpSecurity -> requests.anyRequest().permitAll();}
发布于 2020-12-24 14:14:27
一种方法是在安全配置和自定义筛选器中使用模式,从身份验证中提取主体。你可以这样做:
静态String[] IGNORE_PATTERNS =新的String[] {“**/*..js”,“**/*..css”,String[]
静态String[] PERMIT_ALL_PATTERNS =新String[] { "/login“、"/logout”、"/health"};
WebSecurity
中使用被忽略的模式:web.ignoring().antMatchers(IGNORE_PATTERNS);
HttpSecurity
中的所有模式:http.authorizeRequests().antMatchers(PERMIT_ALL_PATTERNS).permitAll().anyRequest().authenticated().and() .
RequestMatcher
:列表匹配器=新的ArrayList<>();for (字符串模式: IGNORE_PATTERNS) {matchers.add(新的AntPathRequestMatcher(模式));} for (字符串模式: PERMIT_ALL_PATTERNS) {matchers.add(新的模式);} RequestMatcher ignoreRequestMatcher =新的OrRequestMatcher(匹配器);
doFilter
方法中使用请求匹配器:如果(ignoreRequestMatcher.matches((HttpServletRequest)请求){ /*跳过此过滤器*/ chain.doFilter(请求,响应);返回;}在*/下面的过滤器代码的/*其余部分
https://stackoverflow.com/questions/65437818
复制相似问题