CORS:对飞行前请求的响应未通过访问控制检查:飞行前请求不允许重定向

时间:2019-06-13 13:26:09

标签: java spring-boot cors openshift same-origin-policy

我正在尝试将angualar js应用程序与后端spring boot集成在一起,在该应用程序中,我面临着针对预检请求的重定向

这部署在openshift上,我已配置为通过在controller方法中添加一些注释来启用cors,这有助于解决以下问题:  请求在传入请求中没有“ Access-Control-Allow-Origin”标头:CORS政策问题。

    @CrossOrigin(allowedHeaders = "*", origins = "*", exposedHeaders = 
        "Access-Control-Allow-Origin", methods = {
          RequestMethod.POST, RequestMethod.GET, RequestMethod.PUT, 
    RequestMethod.DELETE, RequestMethod.HEAD,
          RequestMethod.OPTIONS, RequestMethod.PATCH, RequestMethod.TRACE })
    @RestController
    public class Controller {

   @Autowired
   Service botService;

   @Autowired
   Environment env;

   @CrossOrigin()
   @RequestMapping(value = "/jwtToken", method = {
                 RequestMethod.POST }, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
   @ResponseStatus(HttpStatus.OK)
   public ResponseEntity<UnifiedService> botConntor(                     
                 @RequestBody UnifiedInput input, HttpServletRequest request) {
          UnifiedBPMService output = botService.processBotRequest(input, request);
          return new ResponseEntity<UnifiedService>(output, HttpStatus.OK);
   }

我在实际的角度应用中遇到的错误是:

  

从以下位置访问“ http:/// chatbot / api / jwtToken”处的XMLHttpRequest           来源“ http://”已被CORS政策阻止:响应           进行预检请求未通过访问控制检查:重定向不是           允许进行预检请求。

options调用返回了以下条件:

    Request URL: http://<domain>/chatbot/api/jwtToken
    Request Method: OPTIONS
    Status Code: 302 Found
    Remote Address: 10.235.222.220:80
    Referrer Policy: no-referrer-when-downgrade

1 个答案:

答案 0 :(得分:0)

您的后端正在重定向(302),而不是向OPTIONS / preflight请求发送适当的响应(200)。检查您的后端日志以查看其重定向的原因。可能类似于Spring安全拒绝OPTIONS请求并重定向到您的登录页面。