spring—如何在angular 8中调用RESTAPI post方法获取cors错误

ny6fqffe  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(127)

我试图在angular 8中调用restapi post方法。

'Access-Control-Allow-Origin' :'*','Access-Control-Allow-Methods' :'POST, GET, PUT, OPTIONS, DELETE, PATCH','Access-Control-Max-Age' :'3600','Access-Control-Allow-Headers' :'Origin, X-Requested-With, Content-Type, Accept',
  'Content-Type': 'application/json',
  'username':'dexadmin','password':'dexcenter'}

  this.http.post('http://localhost:8080/DEXRESTAPI/queues/getMaxJob',{},{​​'headers':headers}).

  subscribe((data:any) => {​​

    console.log('Data', +data);

    console.log('Was my name');
  }​​);
}

Rest API is as below:

@RequestMapping(value = "/getMaxJob", method = RequestMethod.POST)
   public Integer getMaxJob() throws JmsException {

      try {
         return queueDelegate.getMaxJob();
      }
      catch (Exception cause) {
         logger.error("Error getting MaxJob" + cause.getMessage(), cause);
         throw new JmsException(cause.getLocalizedMessage());
      }
   }

我有个错误。我在下面的控制台上访问xmlhttprequest时出错http://localhost:8080/dexrestapi/queues/getmaxjob/“从原点”http://localhost:4200“已被cors策略阻止:对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许来源”标头。
我已经在securityconfig.java文件中设置了

http
                .httpBasic().disable()
                .csrf().disable()
                .anonymous().disable()
                .authorizeRequests()
                .antMatchers("/oauth/token").permitAll()
                .antMatchers(HttpMethod.OPTIONS).permitAll()
                .antMatchers(HttpMethod.GET).permitAll()
                .antMatchers(HttpMethod.POST).permitAll()
                .anyRequest().fullyAuthenticated();
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题