【问题标题】:Spring Security session/xsrf configuration by pathSpring Security session/xsrf 按路径配置
【发布时间】:2017-01-19 01:01:11
【问题描述】:

我有一个使用 Spring Security 进行身份验证的现有 Web 应用程序。它还使用会话管理来允许用户在预定义的时间段内登录,并使用 XSRF 令牌来防止 XSS 攻击。

@Override
protected void configure(HttpSecurity http) throws Exception {
    // @formatter:off
    http
    .exceptionHandling()
        .authenticationEntryPoint(restEntryPoint())
    .and()
    .headers().addHeaderWriter(new StaticHeadersWriter("Server",""))
    .and()
    .httpBasic()
        .authenticationEntryPoint(restEntryPoint())
    .and()  
    .logout().addLogoutHandler(myLogoutHandler())
    .logoutSuccessHandler(logoutSuccessHandler())
    .and()
    .authorizeRequests()
        .antMatchers("/index.html", "/login", "/").permitAll()
        .antMatchers(HttpMethod.OPTIONS).denyAll()
        .antMatchers(HttpMethod.HEAD).denyAll()  
        .anyRequest().authenticated()
    .and()
    .authenticationProvider(myAuthenticationProvider)
        .csrf()
            .csrfTokenRepository(csrfTokenRepository())
    .and()
    .addFilterAfter(csrfHeaderFilter(), SessionManagementFilter.class);
    // @formatter:on
}

这非常适合 Web 应用程序。但是,现在我被要求添加一个配置,允许第三方客户端应用程序通过纯 REST 调用调用我的服务,即它们应该是完全无状态的并使用 http 基本身份验证 - 不应该创建会话并且应该禁用 xsrf(我想想……)。

我可以为所有这些客户端 API 调用定义一个共享 URL 路径。但是如何利用我现有的安全配置和服务器来支持这两个要求?

【问题讨论】:

    标签: java spring security session spring-security


    【解决方案1】:

    回答我自己的问题...

    Spring 安全性允许您根据顺序使用多种配置。在文档中,它给出了以下示例:

    @EnableWebSecurity
    public class MultiHttpSecurityConfig {
        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) { 1
            auth
                .inMemoryAuthentication()
                    .withUser("user").password("password").roles("USER").and()
                    .withUser("admin").password("password").roles("USER", "ADMIN");
        }
    
        @Configuration
        @Order(1)                                                        2
        public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
            protected void configure(HttpSecurity http) throws Exception {
                http
                    .antMatcher("/api/**")                               3
                    .authorizeRequests()
                        .anyRequest().hasRole("ADMIN")
                        .and()
                    .httpBasic();
            }
        }
    
        @Configuration                                                   4
        public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http
                    .authorizeRequests()
                        .anyRequest().authenticated()
                        .and()
                    .formLogin();
            }
        }
    }
    

    在上面的示例中,/api 仅允许用于 ADMIN 角色,而其他路径将使用默认的 FormLoginWebSecurityConfigurerAdapter 进行配置。

    this URL查看更多信息:

    【讨论】:

      猜你喜欢
      • 2015-06-11
      • 2019-10-15
      • 1970-01-01
      • 2018-02-23
      • 2015-04-19
      • 2018-02-23
      • 2012-01-23
      • 2011-01-08
      • 1970-01-01
      相关资源
      最近更新 更多