2016-08-12 14 views
1

私はPOSTリクエストを送信するとき、私はWebSecurityConfigurerAdapterを拡張するクラスを使用して、春のセキュリティを有効にすると、私はこのエラーを受け取るを有効にして、「405がサポートされていないGET」と言いますGETは私がPOSTを送信しているときに許可されていません...私は何時間もこれを把握することができませんでした。POSTリクエストは、春のセキュリティは

下図のように私は私のメインクラスとWebSecurityConfigrerAdapterクラスで@EnableWebSecurityをコメントアウト場合、それは完全に正常に動作します:必要に応じて

@Configuration 
@EnableResourceServer 
@Order(-20) 
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter { 

    private final AuthenticationManager authenticationManager; 

    @Autowired 
    public CustomWebSecurityConfigurerAdapter(AuthenticationManager authenticationManager) { 
     this.authenticationManager = authenticationManager; 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.parentAuthenticationManager(authenticationManager); 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
      .antMatchers("/css/**", "/js/**", "/images/**", "/fonts/**", "/videos/**") 
      .permitAll() 
     .and() 
      .formLogin().loginPage("/login").permitAll() 
     .and() 
      .authorizeRequests()      
      .antMatchers("/", "/join", "/login", "/about", "/contact", "/index.html") 
      .permitAll() 
     .and().authorizeRequests() 
      .anyRequest().authenticated()        
     .and().exceptionHandling() 
      .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/")) 
     .and() 
      .logout().logoutSuccessUrl("/index.html").permitAll() 
     .and() 
      .csrf().disable(); 
    } 

} 

ここに私の春のコントローラは、方法によってです:

@RequestMapping(value = "/login", method = RequestMethod.POST) 
    public ResponseEntity<BusinessUser> login(@RequestBody BusinessUser inputUser) { 
     BusinessUser requestedUser = businessUserService.findByUsername(inputUser.getUsername()); 
     if(requestedUser != null) 
      if(BCrypt.checkpw(inputUser.getPassword(), requestedUser.getPassword())) 
       return new ResponseEntity<>(requestedUser, HttpStatus.OK); 
     return new ResponseEntity<>(HttpStatus.BAD_REQUEST); 
    } 

このコントローラも正常に動作し、POST要求を受け取ることができ、WebSecurityConfigurerAdapterクラス(OAuth2.0とCSRF保護を後で有効にする必要がある)を除いてJSON形式のユーザー情報を返すことができます。

+1

POSTリクエストを送信しているURLは? – 11thdimension

+0

@ 11thdimension http:// localhost:8090/login –

+0

@JakeMiller '/ login'にはクラスレベルで定義された別のルートが前置されていませんか?とにかくもちろん、ブラウザのバーにURLを書き込んでアクセスしようとすると、GETリクエストを行います。 POSTを行うにはAJAXを使用する必要があります。 – nbro

答えて

1

私は後世のためにもう少しドキュメントを追加します。ジェイクが言ったように、ラインは

.formLogin().loginPage("/login").permitAll() 

実際には問題でした。 documentationによるとloginPageのためのいくつかの要件があります。

  • それはHTTP POSTでなければなりませんが、
  • それは
  • それは として名を含める必要があり AbstractAuthenticationFilterConfigurer.loginProcessingUrl(文字列)に提出しなければならない
  • HTTPパラメータ usernameパラメータ(文字列)
  • パスワードには、パスワードのパスワードとして、パスワードパラメータ名(文字列)
  • のパスワードを含める必要があります

メソッド定義にloginPage()メソッドが必要とする必須のhttpパラメータがありません。

+0

私は説明を感謝します!私は試行錯誤を通して問題が何であるかを理解しましたが、なぜそのラインがエラーを引き起こしているのか全く分かりませんでした。ありがとうございました –

0

私はそれを修正しました。

この行は、エラーの原因となった...

.formLogin().loginPage("/login").permitAll() 

は、それを削除し、今ではすべてが正常に動作しています。

関連する問題