2017-10-03 25 views
0

私はこの問題に悩まされています...私はSpringブートアプリケーションをS2S通信と併用しています。私は@RestController POST要求を受け入れる必要がありますメソッドがあります。スプリングブート - POSTメソッドが許可されていません

これは私がリクエストが有効になっているCSFRフィルターによってブロックされたことがわかった私は405エラーを取得し、このリンクをヒットした場合、前記第一の時点で

を許可されていないコントローラ

@RestController 
public class PaymentRestController { 

@PostMapping("/util/paymentResponse") 
    public void savePaymentResponse(@RequestParam boolean transaction_status, @RequestParam String usedToken, 
      @RequestParam String transaction_message, @RequestParam String authCode, 
      @RequestParam String transactionCode, @RequestParam String orderId, HttpServletRequest request) { 
//business logic 
} 

} 

ですWebアプリケーション上で、私はこのように

@Configuration 
@ComponentScan("it.besmart") 
@EnableWebSecurity 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{ 

    @Autowired 
    @Qualifier("customUserDetailsService") 
    UserDetailsService userDetailsService; 

    @Autowired 
    CustomSuccessHandler customSuccessHandler; 

    @Autowired 
    CustomAuthenticationFailureHandler customAuthenticationFailureHandler; 

    @Autowired 
    DataSource dataSource; 

    private final static Logger logger = LoggerFactory.getLogger(SecurityConfiguration.class); 

    @Autowired 
    public void configureGlobalService(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); 

    } 

    @Bean 
    public PasswordEncoder passwordEncoder() { 
     return new BCryptPasswordEncoder(); 
    } 

    @Bean 
    public SwitchUserFilter switchUserFilter() { 
     SwitchUserFilter filter = new SwitchUserFilter(); 
     filter.setUserDetailsService(userDetailsService); 
     filter.setSuccessHandler(customSuccessHandler); 
     filter.setFailureHandler(customAuthenticationFailureHandler); 
     return filter; 
    } 

     protected void configure(HttpSecurity http) throws Exception { 
      logger.debug("Webapp security configured"); 


      http 

      .authorizeRequests() 
        .antMatchers("/", "/home", "/contacts", "/faq", "/privacy", "/register", "/registrationConfirm", "/util/**", "/resendRegistrationToken","/park**", "/oauth/authorize", "/error") 
        .permitAll() 
        .antMatchers("/profile**", "/edit**","/payment**", "/plate**","/notification**", "/addPaymentMethod**", "/logout/impersonate**") 
        .access("hasRole('USER') or hasRole('NOPAYMENT')") 
        .antMatchers("/book**", "/manage**") 
        .access("hasRole('USER')") 
        .antMatchers("/admin**", "/login/impersonate**").access("hasRole('ADMIN')") 
        .antMatchers("/updatePassword").hasAuthority("CHANGE_PASSWORD_PRIVILEGE") 

        .and().formLogin().loginPage("/?login=login").loginProcessingUrl("/")     .successHandler(customSuccessHandler).failureHandler(customAuthenticationFailureHandler).usernameParameter("email").passwordParameter("password").and().rememberMe().rememberMeParameter("remember-me").tokenRepository(persistentTokenRepository()).tokenValiditySeconds(86400).and().exceptionHandling().accessDeniedPage("/accessDenied") 

        .and().csrf().ignoringAntMatchers("/util**") 
        .and().logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")) 
        .logoutSuccessUrl("/?logout=true").permitAll() 


        .and().addFilterAfter(switchUserFilter(), FilterSecurityInterceptor.class); 

     } 

このように私のセキュリティを設定している私はCSRFトークン例外を取得し、それでも405エラーを取得していませんよ。 これはPOSTの問題でもないので、私はGETにリクエストとマッピングを変更すると、まだ405エラーが発生します...そして、POSTを送信しようとすると、ヘッダ応答にAllowedメソッドがPOST 、私はGETでそれを送信する場合、私は問題は、パラメータの一つがnullだったということでした

+0

セキュリティに関連する問題を除外するには、application.propertiesにmanagement.security.enabled = falseプロパティを追加して、そのように機能するかどうかを試してみてください。 – marco

+0

何も変更されていません...問題はまだそこにあります... – besmart

+0

セキュリティ上の問題はありません...どのようにPOSTリクエストしますか?また、すべてのRequestParamにrequired = falseを追加し、アプリケーションをデバッグし、コントローラの最初の行にブレークポイントを設定してみてください。 – marco

答えて

0

...私は場所を確認する方法がわからないメソッドPOST ...奇妙な

許さ参照してください。 そのように、リクエストパラメータの注釈で必要=ヌルを追加する解決されています

@RequestParam(value = "yourParamName", required = false) 

はこの405を引き起こし、ここで定義されている:

6.5.5. 405 Method Not Allowed 

The 405 (Method Not Allowed) status code indicates that the method 
received in the request-line is known by the origin server but not 
supported by the target resource. The origin server MUST generate an 
Allow header field in a 405 response containing a list of the target 
resource's currently supported methods. 

A 405 response is cacheable by default; i.e., unless otherwise 
indicated by the method definition or explicit cache controls (see 
Section 4.2.2 of [RFC7234]). 

「ターゲット・リソースが」here:を定義しているとき

関連する問題