spring-security 在Sping Boot 应用程序中禁用HTTP OPTIONS方法

m0rkklqb  于 2022-11-11  发布在  Spring
关注(0)|答案(4)|浏览(257)

我开发了一个关于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方法。

vpfxa7rd

vpfxa7rd1#

Previous answer只适用于tomcat,所以也可以添加mine。您可以禁用跨容器方法,例如,使用标准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

lokaqttq

lokaqttq2#

试试这个;在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");

            }
        };
    }
}
x33g5p2x

x33g5p2x3#

我试过这个,它起作用了。

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

piok6c0g4#

如果您使用的是Spring Security,可以使用以下方法:

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

相关问题