【问题标题】:Disable HTTP OPTIONS method in spring boot application在 Spring Boot 应用程序中禁用 HTTP OPTIONS 方法
【发布时间】:2017-07-11 02:44:40
【问题描述】:

我在 Spring Boot 应用程序上开发了 Rest API。 API 仅接受 GET 和 POST ,但在使用 OPTIONS 方法请求时,API 响应 200 状态(而不是 405)。我用谷歌搜索了这个问题,但没有一个解决方案是基于 springboot 的。

回复:

Allow: OPTIONS, TRACE, GET, HEAD, POST
Public: OPTIONS, TRACE, GET, HEAD, POST

需要禁用 OPTIONS 方法。

【问题讨论】:

  • @dur 请忽略服务器部分。实际上,我无法将完整的响应显示为分类。所以我只是添加了类似类型的响应。希望你能理解

标签: java spring-boot spring-security


【解决方案1】:

我试过了,效果很好。

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
    return new EmbeddedServletContainerCustomizer() {
        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            if (container.getClass().isAssignableFrom(TomcatEmbeddedServletContainerFactory.class)) {
                TomcatEmbeddedServletContainerFactory tomcatContainer = (TomcatEmbeddedServletContainerFactory) container;
                tomcatContainer.addContextCustomizers(new ContextSecurityCustomizer());
            }
        }
    };
}

private static class ContextSecurityCustomizer implements TomcatContextCustomizer {
    @Override
    public void customize(Context context) {
        SecurityConstraint constraint = new SecurityConstraint();
        SecurityCollection securityCollection = new SecurityCollection();
        securityCollection.setName("restricted_methods");
        securityCollection.addPattern("/*");
        securityCollection.addMethod(HttpMethod.OPTIONS.toString());
        constraint.addCollection(securityCollection);
        constraint.setAuthConstraint(true);
        context.addConstraint(constraint);
    }
}

【讨论】:

    【解决方案2】:

    Previous answer 仅适用于 tomcat,所以也添加我的。您可以禁用方法跨容器,例如,使用标准 servlet 过滤器:

    import java.io.IOException;
    import javax.servlet.FilterChain;
    import javax.servlet.ServletException;
    import javax.servlet.ServletResponse;
    import javax.servlet.http.HttpServletRequest; 
    import javax.servlet.http.HttpServletResponse; 
    
    import org.springframework.stereotype.Component;     
    import org.springframework.web.filter.OncePerRequestFilter; 
    
    @Component
    public class MethodFilter extends OncePerRequestFilter { 
    
        @Override 
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) 
                        throws ServletException, IOException { 
            if (request.getMethod().equals("OPTIONS")) {
                response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
            } else { 
                filterChain.doFilter(request, response); 
            } 
        }
    } 
    

    注意:假设这个类是Spring扫描的组件。如果没有,可以使用in here详述的其他注册方式。

    【讨论】:

    • 如果您想禁用 TRACE 方法,此过滤器似乎不起作用
    • @SpartanX1 取决于您禁用的含义。它当然应该阻止它的使用,但是 TRACE 在 OPTIONS 输出中仍然可见
    【解决方案3】:

    试试这个;在allowedMethods 你可以过滤需要的方法:

    @Configuration
    public class CorsConfiguration {
    
        @Bean
        public WebMvcConfigurer corsConfigurer() {
            return new WebMvcConfigurerAdapter() {
                @Override
                public void addCorsMappings(CorsRegistry registry) {
                    registry.addMapping("/**")
                            .allowedOrigins(origins u want to allow)
                            .allowCredentials(false).allowedMethods("POST", "GET", "PUT");
    
                }
            };
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-03-08
      • 2016-05-10
      • 2016-06-23
      • 2017-11-06
      • 2016-07-16
      • 2018-09-29
      • 1970-01-01
      • 2019-05-27
      • 2016-01-21
      相关资源
      最近更新 更多