405不允许的方法为POST

问题描述:

我有一个非常简单的弹簧启动的应用程序,这是由下面的代码安全:405不允许的方法为POST

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。 问题是所有的职位返回

405不允许的方法

如果我从应用程序中删除安全启动,它的工作原理。这让我相信安全配置是个问题。但我无法找出如何。

+0

您的登录处理程序如何? –

+0

auth.jdbcAuthentication()。数据源(数据源) \t \t .passwordEncoder(的PasswordEncoder()) \t \t .usersByUsernameQuery( \t \t \t “选择用户名,密码,从用户其中username =?启用”) \t \t。 authoritiesByUsernameQuery( \t \t \t“select username,role from user_roles where username =?”);但这应该不重要,因为此方法在管理员部分之外。 – Arash

这应该很容易。

如果启用了CSRF,则不允许POST和PUT请求,并且默认情况下,spring引导启用这些请求。

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

.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

这可以如果使用Spring Security SAML扩展和您的也会发生返回UsernameNotFoundException。看起来很不直观,但这就是默认情况下(按照我的经验)。