簡體   English   中英

在 spring boot 應用程序中禁用 HTTP OPTIONS 方法

[英]Disable HTTP OPTIONS method in spring boot application

我在 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 方法。

以前的答案僅適用於 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掃描的components。 如果沒有,您可以使用此處詳述的其他注冊方法。

試試這個; 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");

            }
        };
    }
}

我試過了,它奏效了。

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

如果你使用的是spring security,你可以使用下面的方法:

@Bean
public HttpFirewall configureFirewall() {
   StrictHttpFirewall strictHttpFirewall = new StrictHttpFirewall();
   strictHttpFirewall.setAllowedHttpMethods(Arrays.asList("GET","POST","OPTIONS"));
   return strictHttpFirewall;
}

我已經在 Spring Boot 應用程序上開發了 rest API。 API 僅接受 GET 和 POST ,但在使用 OPTIONS 方法請求時,API 會響應 200 狀態(而不是 405)。 我在 google 上搜索了這個問題,但沒有一個解決方案是基於 springboot 的。

回復:

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

需要禁用 OPTIONS 方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM