【问题标题】:405: Method Not Allowed after update to Spring Boot 1.3.0405:更新到 Spring Boot 1.3.0 后方法不允许
【发布时间】:2016-03-02 17:01:00
【问题描述】:

我已经设置了一个授权和资源服务器,它们都在同一个应用程序中运行。使用 spring boot 1.2.6.RELEASE 和 spring security oauth2 2.0.7.RELEASE 设置运行良好。在更新到 spring boot 1.3.0.RELEASE(无论是 spring security oauth2 2.0.7.RELEASE 还是 2.0.8.RELEASE)后,/login 端点用于 POST请求被破坏。我得到了响应 405“方法不允许”。

我花了一天多没有成功。我还设置了一个额外的、直接的授权和资源服务器,它们都在同一个应用程序中运行。然而,这个轻量级的设置也有同样的问题。一旦我启用资源服务器,登录端点就不再工作。

在我的原始服务器的配置 sn-ps 下方。

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    // ... some code

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .csrf()
                .requireCsrfProtectionMatcher(new AntPathRequestMatcher("/oauth/authorize"))
                .disable()
                .logout()
                .logoutUrl(logoutUrl)
                .logoutSuccessHandler(logoutSuccessHandler)
                .invalidateHttpSession(true)
                .and()
                .formLogin()
                .permitAll()
                .loginProcessingUrl(loginProcessingUrl)
                .failureUrl(loginAuthenticationFailureUrl)
                .loginPage(loginPage);
    }

}

@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

    // ... some code

    private static final String RESOURCE_SERVER_MAPPING_REGEX = "^(?!(\\/?login|\\/?logout|\\/?oauth)).*$";

    // ...some code

    @Override
    public void configure(HttpSecurity http) throws Exception {

        http
            // Configure the endpoints wherefore the resource server is responsible or rather should bound to.
            .requestMatchers().regexMatchers(RESOURCE_SERVER_MAPPING_REGEX)
            .and()
            .authorizeRequests().anyRequest().permitAll()
            .and()
            .headers().frameOptions().disable();
    }
}

【问题讨论】:

    标签: spring spring-security spring-boot spring-security-oauth2


    【解决方案1】:

    解决方案

    看起来我面临@EnableResourceServer creates a FilterChain that matches possible unwanted endpoints with Spring Security 4.0.3中提到的问题,Spring Security 是 spring-boot-starter-security 的依赖项。由于 spring-boot-starter-parent 的更新,我从 Spring Security 3.2.8 切换到了 4.0.3。可以在here 找到有关此问题的更多有趣评论。

    我更改了 ResourceServer 配置,如下面的代码 sn-ps 解决了问题。

    通过使用否定现有代码来匹配登录和注销表单或匹配器(如 spring NotOAuthRequestMatcher)来替换 RegexRequestMatcher 定义会很棒,它目前是 ResourceServerConfiguration 的私有内部类。请不要犹豫发表建议。 ;-)

    @Configuration
    @EnableResourceServer
    public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
    
        // ... some code
    
        private static final String RESOURCE_SERVER_MAPPING_REGEX = "^(?!(\\/?login|\\/?logout|\\/?oauth)).*$";
    
        // ...some code
    
        @Override
        public void configure(HttpSecurity http) throws Exception {
    
            http.requestMatcher(
                    new AndRequestMatcher(
                            new RegexRequestMatcher(RESOURCE_SERVER_MAPPING_REGEX, null)
                    )
            ).authorizeRequests().anyRequest().permitAll()
            // ...some code
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      这应该很简单。

      如果启用 CSRF,则不允许 POST 和 PUT 请求,Spring Boot 默认启用这些请求。

      只需将此添加到您的 ResourceServerConfig 配置代码中:

      .csrf().disable()
      

      这也作为示例问题 here

      【讨论】:

      • 你的意思是即使对于resourceserverconfig?如上所示,它没有那个..
      • 对造成的混乱感到抱歉。你是对的,ResourceServer 配置不包含配置。但是我尝试了它但没有成功。我不太确定顺序或更确切地说是多个 HttpSecurity 配置的使用。可能与 CSRF 配置的 HttpSecurity 对象无关。
      猜你喜欢
      • 2016-09-23
      • 2015-07-22
      • 2023-02-15
      • 2022-01-07
      • 2019-05-10
      • 2018-03-27
      • 1970-01-01
      • 2014-09-19
      • 1970-01-01
      相关资源
      最近更新 更多