【问题标题】:405 Method Not Allowed for POSTPOST 不允许使用 405 方法
【发布时间】:2016-02-15 10:43:20
【问题描述】:

我有一个非常简单的 Spring Boot 应用程序,它由以下代码保护:

http.authorizeRequests()
        .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
        .and()
          .formLogin().loginPage("/login").failureUrl("/login?error")
          .usernameParameter("username").passwordParameter("password")
        .and()
          .logout().logoutSuccessUrl("/login?logout")
        .and()
          .exceptionHandling().accessDeniedPage("/403");

这个想法是保护“管理”部分。它公开了一个 REST API。 问题是所有的POSTS返回

405 方法不允许

如果我从应用程序中删除安全启动器,它会起作用。这让我相信安全配置是问题所在。但我不知道怎么做。

【问题讨论】:

  • 您的登录处理程序怎么样?
  • auth.jdbcAuthentication().dataSource(dataSource) .passwordEncoder(passwordEncoder()) .usersByUsernameQuery("选择用户名,密码,从username=的用户启用?") .authoritiesByUsernameQuery("选择用户名,来自 user_roles 的角色,其中 username=?");不过没关系,因为这个方法不在管理部分。

标签: spring spring-security spring-boot


【解决方案1】:

这应该很容易。

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

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

.csrf().disable()

那就是:

http.
.csrf().disable().
authorizeRequests()
        .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
        .and()
          .formLogin().loginPage("/login").failureUrl("/login?error")
          .usernameParameter("username").passwordParameter("password")
        .and()
          .logout().logoutSuccessUrl("/login?logout")
        .and()
          .exceptionHandling().accessDeniedPage("/403");

如果需要启用 CSRF,请参考文档:

http://docs.spring.io/spring-security/site/docs/4.0.x/reference/htmlsingle/#csrf-configure

【讨论】:

    【解决方案2】:

    如果使用 Spring Security SAML 扩展并且您的 SAMLUserDetailsService 返回 UsernameNotFoundException,也会发生这种情况。看起来很违反直觉,但默认情况下就是这样做的(根据我的经验)。

    【讨论】:

      猜你喜欢
      • 2014-04-18
      • 2014-05-23
      • 1970-01-01
      • 2018-12-20
      • 2015-11-16
      • 2016-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多