spring 如何在Sping Boot 应用中禁用DefaultSecurityFilterChain?

qhhrdooz  于 2022-12-26  发布在  Spring
关注(0)|答案(1)|浏览(163)

在我的Sping Boot 应用程序中,我有:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    ...
    @Override
    protected void configure(HttpSecurity httpSecurity)
        throws Exception 
    {
        httpSecurity
            .authorizeRequests()
            // various GET/POST path enable rules, none of which would enable access to default ones (see log below)
            ...
            // finally, deny everything else
            .antMatchers("/**").denyAll()
            ...
    }
}

启动时,日志显示:

2016-01-29 13:20:49.379  INFO 8044 --- [ost-startStop-1] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: OrRequestMatcher [requestMatchers=[Ant [pattern='/css/**'], Ant [pattern='/js/**'], Ant [pattern='/images/**'], Ant [pattern='/**/favicon.ico'], Ant [pattern='/error']]], []

我可以访问例如localhost:8080/blah/favicon.ico,即使我希望它被阻止。
我尝试遵循Security configuration with Spring-bootSpring Security exclude url patterns in security annotation configurartion中的建议。
根据http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-security上的文档,我还尝试将security.ignored设置为各种路径,并使用@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)注解上述类,但都无济于事。
是否有一种简单的方法可以禁用DefaultSecurityFilterChain,使其不会为公共静态资源位置添加这些被忽略(不安全)的路径?
如果没有,那么在Java或application.properties中禁用这些路径的正确配置是什么?
好的,有两种方法可以做到:
application.properties中,设置security.ignored=none
或者,创建以下类:

@Component
public class CustomSecurityProperties extends SecurityProperties {
    public CustomSecurityProperties() {
        // the default list is empty
        List<String> ignoredPaths = getIgnored();
        ignoredPaths.add("none");
    }
}

神奇的none的提示来自https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/SpringBootWebSecurityConfiguration.javaSpringBootWebSecurityConfiguration的第121-130行
无论哪种解决方案,日志中仍会保留以下内容:

2016-01-29 17:53:12.830  INFO 3008 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]

这表示创建了一个ResourceHttpRequestHandler来提供favicon.ico文件。但是,无法再访问/blah/favicon.ico

yv5phkfx

yv5phkfx1#

在你最后一个被拒绝的antmatcher中,有一个特定的url,我们有一个斜杠,它将阻止所有端点

相关问题