2

コントローラーメソッド内でスローされた例外は500 internal server errorに変換されると予想していますが、コントローラーメソッド内でどのような例外が発生しても、クライアント側で405 method not allowed例外が発生します。

ノート:

デバッグ時に期待されるメソッドに移動するため、リクエストマッピングに問題はありません。しかし、コントローラーメソッドから例外がスローされると、クライアントで「405メソッドは許可されていません」と表示されます

注 2:

例外をキャッチしてHttpServerErrorException、適切なエラー コードとメッセージをスローしようとしましたが、何も変わりませんでした。

編集:

私は春のセキュリティを使用していますが、ここに設定があります:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

/*

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }
*/

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/session/**").permitAll()
                .antMatchers("/app/**").permitAll()
                .antMatchers("/lib/**").permitAll()
                .antMatchers("/templates/**").permitAll()
                .antMatchers("/dial/**").permitAll()
                .antMatchers("/names/**").permitAll()
//                    .antMatchers("/workflows/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/#/login").permitAll();

        http.csrf().disable();
        http.exceptionHandling().defaultAuthenticationEntryPointFor(
                new UnauthorizedLoginUrlAuthenticationEntryPoint("/login.html"),
                new ELRequestMatcher("hasHeader('X-Requested-With','XMLHttpRequest')"));
               /* .logout()
                    .permitAll();*/
    }


}
4

2 に答える 2

0

実際には、タイプ の Bean が存在することが問題でしたEmbeddedServletContainerCustomizer
私はそれを削除し、問題は解決しました。

于 2015-09-22T07:27:09.107 に答える
0

Spring Data RESTコントローラーの例外ハンドラーでこれを見つけることができました:-

@ControllerAdvice(basePackageClasses = RepositoryRestExceptionHandler.class)
public class RepositoryRestExceptionHandler {

    /**
     * Send {@code 405 Method Not Allowed} and include the supported {@link org.springframework.http.HttpMethod}s in the
     * {@code Allow} header.
     *
     * @param o_O the exception to handle.
     * @return
     */
    @ExceptionHandler
    ResponseEntity<Void> handle(HttpRequestMethodNotSupportedException o_O) {

        HttpHeaders headers = new HttpHeaders();
        headers.setAllow(o_O.getSupportedHttpMethods());

        return response(HttpStatus.METHOD_NOT_ALLOWED, headers);
    }
}

メインのスプリング ブート クラスでこれを試すことができますか (プロジェクトのクラスパスがフォルダー COM で始まることを願っています):-

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan({"com"})
于 2015-09-20T15:26:33.157 に答える