【问题标题】:Expose an unsecure Restful endpoint in a Springboot Secured application在 Springboot 安全应用程序中公开一个不安全的 Restful 端点
【发布时间】:2014-09-16 21:55:36
【问题描述】:

我想在 Springboot 安全应用程序中公开一个不安全的 Restful 端点。对端点 /api/notify 的 GET 请求有效,但 POST 请求会导致 403。如何配置以便远程客户端可以从那里的服务器 POST 到 /api/notify?

我的扩展 WebSecurityConfigurerAdapter 如下所示:

protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
        .antMatchers("/").permitAll()
        .antMatchers("/signup").permitAll()
        .antMatchers("/reset").permitAll()
        .antMatchers("/api/notify").permitAll()
        .anyRequest().authenticated();
    http
        .formLogin()
            .defaultSuccessUrl("/home")
        .failureUrl("/login?error")
        .loginPage("/login")
        .permitAll()
        .and()
        .logout()
        .permitAll();
}

【问题讨论】:

  • 你能告诉我们 /api/notify 映射到的控制器方法吗?
  • 我怀疑 csfr 保护阻碍了你。当发布 Spring Security 检查一个 csfr 令牌是否是表单提交的一部分时,它是不允许访问的。使用 java-config 时,默认启用 http.csfr().disable(); 禁用。

标签: spring-security spring-boot


【解决方案1】:

我想这就是你要找的:

@Configuration
@EnableWebMvcSecurity
public class SecurityCtxConfig extends WebSecurityConfigurerAdapter {
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
                .antMatchers(HttpMethod.POST,
                        "<YOUR URL>");
    }

【讨论】:

    【解决方案2】:

    我建议你尝试以下配置:

    protected void configure(HttpSecurity http) throws Exception {
        //This reads, invoke this configuration only when
        //the pattern "/api/notify" is found.
        //When this configuration is invoked, restrict access,
        //disable CSRF, and if the path matches "/api/notify" permit all.
        http.antMatcher("/api/notify")
            .csrf().disable()
            .authorizeRequests()
            .antMatchers("/api/notify")
            .permitAll();
    
        //Your other configuration...
        http
            .authorizeRequests()
            .antMatchers("/").permitAll()
            .antMatchers("/signup").permitAll()
            .antMatchers("/reset").permitAll()
            .anyRequest().authenticated();
        http
            .formLogin()
                .defaultSuccessUrl("/home")
            .failureUrl("/login?error")
            .loginPage("/login")
            .permitAll()
            .and()
            .logout()
            .permitAll();
    }
    

    【讨论】:

    • GET 被接受,POST 仍然被禁止,我在上面添加了我的控制器方法。
    • 我的控制器被@RestController注释
    • @darrenrose,我已经更新了我的答案。也就是说,此答案旨在让您了解如何实施最终解决方案。
    猜你喜欢
    • 2011-11-03
    • 2021-04-04
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 2017-03-26
    • 2011-05-11
    • 1970-01-01
    • 2016-04-02
    相关资源
    最近更新 更多