Disable HTTP OPTIONS method in spring boot application

后端 未结 3 482
悲&欢浪女
悲&欢浪女 2020-12-16 20:15

I had developed rest API on spring boot application. The APIs accept only GET , and POST , but on requesting using OPTIONS method , API responding 200 status (instead of 405

相关标签:
3条回答
  • 2020-12-16 20:38

    Previous answer is for tomcat only, so adding mine as well. You can disable the method cross-container by, for example, using a standard servlet filter:

    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); 
            } 
        }
    } 
    

    Note: it is assumed that this class is componentscanned by Spring. If not, you can use other registration methods as detailed in here.

    0 讨论(0)
  • 2020-12-16 20:44

    Try this; in allowedMethods you can filter methods which are required:

    @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");
    
                }
            };
        }
    }
    
    0 讨论(0)
  • 2020-12-16 21:00

    I tried this and it worked.

    @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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题