Spring Security配置错误

我最近看到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 {
    
    @Override
    public 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 {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/", "/home").permitAll()
                .anyRequest().authenticated();
        http
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }
}

就像所有Spring指南一样,很好并且具有解释性。 第一种“配置”方法将“ /”和“家庭”注册为公共,并指定其他所有内容均应进行身份验证。 它还注册登录URL。 第二种“配置”方法指定角色“ USER”的身份验证方法。 当然,您不想在生产中像这样使用它!

现在,我将略微修改此代码。

@Override
    protected 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值