我最近看到Mike Wienser的SpringOne2GX谈论了Application Security Pitfalls 。 如果您在Servlet容器上使用Spring的堆栈,这将非常有用,值得一看。
它使我想起了我曾经面临的一个严重的Spring Security Misconfiguration。 在Spring的指导项目Securing a Web Application上进行解释。 该项目使用Spring Boot,Spring Integration和Spring MVC。
项目使用以下视图:
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/home").setViewName("home");registry.addViewController("/").setViewName("home");registry.addViewController("/hello").setViewName("hello");registry.addViewController("/login").setViewName("login");}}
应公开访问“ / home”,“ /”和“ / login” URL,而“ / hello”仅应由经过身份验证的用户访问。 这是《指南》中的原始Spring Security配置:
@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/", "/home").permitAll().anyRequest().authenticated();http.formLogin().loginPage("/login").permitAll().and().logout().permitAll();}@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");}
}
就像所有Spring指南一样,很好并且具有解释性。 第一种“配置”方法将“ /”和“家庭”注册为公共,并指定其他所有内容均应进行身份验证。 它还注册登录URL。 第二种“配置”方法指定角色“ USER”的身份验证方法。 当然,您不想在生产中像这样使用它!
现在,我将略微修改此代码。
@Overrideprotected void configure(HttpSecurity http) throws Exception {//!!! Don't use this example !!!http.authorizeRequests() .antMatchers("/hello").hasRole("USER");//... same as above ...}
一切都是公共和私有端点,必须列出。 您可以看到我修改后的代码具有与原始代码相同的行为。 实际上,它节省了一行代码。
但是,这有一个严重的问题。 如果我需要引入新的专用端点怎么办? 假设我不知道它需要在Spring Security配置中注册的事实。 我的新端点将是公共的。 这样的配置错误确实很难发现,并且可能导致URL暴露。
因此得出的结论是: 默认情况下,始终对所有端点进行身份验证。
翻译自: https://www.javacodegeeks.com/2014/06/spring-security-misconfiguration.html