当前位置: 首页 > 知识库问答 >
问题:

来自邮递员的邮件请求的跨来源问题

鲁辉
2023-03-14

我正在编写一个具有交叉源配置的API

我的websecurity配置有

@Bean
    public FilterRegistrationBean corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(Boolean.TRUE);
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", config);
        FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
        bean.setOrder(0);
        return bean;
    }

    @Bean
    public FilterRegistrationBean someFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(someFilter());
        registration.addUrlPatterns("/abc/*");
        registration.setOrder(1);
        return registration;
    }

控制器类

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/gdpr/1.0")
public class AbcController {
 

    @PostMapping("/request")
    ReturnStatus saveRequest(HttpServletRequest request,
                             @RequestParam(value = "requestType", required = true) RequestType requestType,
                             @RequestParam(value = "Type", required = true) Type Type,
                             @RequestParam(value = "file", required = true)
                                     MultipartFile uploadedFile) throws InvalidInputException {
        
    }

身份验证过滤器检查

private static final Pattern HEALTH_CHECK = Pattern.compile("^.+/abc/[\\d]+\\.[\\d]+/?$");
        @Override
            protected void doFilterInternal(HttpServletRequest request,
                                            HttpServletResponse response, FilterChain filterChain)
                    throws ServletException, IOException {
        
                StringBuffer requestURL = request.getRequestURL();
        
                if (isHealthCheck(requestURL)) {
                    filterChain.doFilter(request, response);
                    return;
                }
        
                String source = UNKNOWN;
    
    boolean isHealthCheck(StringBuffer requestURL) {
            if (requestURL != null) {
                Matcher matcher = HEALTH_CHECK.matcher(requestURL.toString());
                return matcher.matches();
            }
            return false;
        }

匹配。匹配总是变假

我是不是漏掉了什么?

我添加了什么额外的参数吗?

期待着迅速的回应。

共有1个答案

孙钱青
2023-03-14

403响应与CORS无关。(此外,Postman不受CORS设置的影响。)

可能是CSRF保护自动配置造成的。

您可以尝试禁用它:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
    }
}
 类似资料:
  • 目前正在开发Spring boot后端API服务。这个API需要两个值,一个用于路径变量,另一个用于请求体。使用POSTMAN应用程序进行请求。 当我点击发送按钮时,它将如下所示 控制器类 如果我们向邮递员提出请求,十字架的来源是什么。我尝试了一些事情,但没有帮助。 请提出一个有效的解决办法。

  • 我在控制器[GET、POST和PUT]中编写了多个RESTendpoint。 接听和接听电话都很好。但当我试图点击来自Postman的PUT请求时,我的java控制器无法接收该请求。没有错误消息。响应主体为空。邮递员的回复代码是200。 以下是我的PUTendpoint,它无法从邮递员处获取请求: 下面是我的GETendpoint,它运行良好: 包含PUT请求标题和正文的邮递员URL为: 但是,系

  • 不管用。就像文件没有正确地附加到HTTP请求一样。 编辑:我终于明白了问题是POSTMAN可以访问我的文件系统,但我试图运行导出代码段的远程服务器没有--这是我的一个非常愚蠢的错误。

  • 有人能帮助我如何使用以下参数发送POST请求吗: 我试着在《邮递员的身体》中发送一个带有以下参数的: 然而,在发送上述请求时,我得到的位置字符串为null。

  • 如何在Postman中将附件添加到SOAP请求中? 我有以下设置,并且在“原始”文本框中有我的SOAP xml。 (我可以在SoapUI中做到这一点,但想使用Postman。)

  • 我创建了对在 AWS 服务器上运行的后端的邮递员 POST API 调用 我得到了适当的回应。我的测试人员(远程工作的人)做了同样的事情,得到了错误的API响应(不是我们所期望的)。 我试着在我们公司的另外4台电脑上做同样的测试用例,得到了同样的正确响应。我的测试人员的邮递员有什么问题?