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

我可以在 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);
    }
}

你能在你的主 spring boot 类上试试这个吗(我希望你的项目类路径以文件夹 COM 开头):-

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

实际上问题是因为存在 type 的 bean EmbeddedServletContainerCustomizer
我删除了它并解决了问题。

于 2015-09-22T07:27:09.107 回答