在我的 Spring 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
,即使我希望它被阻止。
我尝试遵循 Spring-boot 的安全配置 和 Spring Security 在安全注释配置中排除 url 模式中的建议。
根据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
来自SpringBootWebSecurityConfiguration
的第121-130行,位于https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main /java/org/springframework/boot/autoconfigure/security/SpringBootWebSecurityConfiguration.java
两种解决方案仍会在日志中留下以下内容:
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
。
在最后一个被拒绝的 antmatcher 中,有特定的 url,但没有单斜杠,这会阻止所有端点