错误:-对预检请求的响应未通过访问控制检查:所请求的资源上不存在“ Access-Control-Allow-Origin”标头

时间:2019-10-18 10:28:03

标签: angular spring-boot

我收到以下错误消息:-

”通过“ http://localhost:8080/api/nemis-访问XMLHttpRequest 来自来源“ http://localhost:4200”的ops / claims / v1.0 / reporting / twsScheduleReport'已被阻止 CORS政策:对预检请求的响应未通过访问控制检查:否'Access-Control- Allow-Origin标头出现在所请求的资源上。”

请帮助我,我不知道问题出在角度代码还是我的java spring-boot。 当我按下搜索按钮时,会出现上述错误,指出“访问控制-允许-来源”的CORS错误。

我的控制器:-

**import statments**
@CrossOrigin
@RestController
@RequestMapping("/api/nemis/claims/v1.0/")

public class TwsScheduleController {
private static final Logger logger = LogManager.getLogger(TwsScheduleController.class);
@Autowired
private TwsScheduleService TwsScheduleService;  
/**
 * TwsSchedule Report Rest end point for search functionality
 * 
 * @param claimFilterModel
 * @return List<TwsScheduleModel>
 * @throws ParseException
 */
@PostMapping("/reporting/twsScheduleReport/")
@CrossOrigin
public ResponseEntity<List<TwsScheduleModel>> getTwsScheduleReport(@RequestBody ClaimFilterModel claimFilterModel)
        throws ParseException {
    logger.info("Fetching Data for TwsSchedule Report Begin");
    String employeeMsId = "pshar25";
    List<TwsScheduleModel> list = TwsScheduleService.getTwsScheduleReport(claimFilterModel,
        employeeMsId);
    logger.info("Fetching Data for TwsSchedule Report End");
    return new ResponseEntity<List<TwsScheduleModel>>(list, HttpStatus.OK);
  }

}

我的服务:-

**import statments**

@ConfigurationProperties
@Service
public class TwsScheduleServiceImpl implements TwsScheduleService {

private static final Logger logger = LogManager.getLogger(RecurrenceServiceImpl.class);
@Autowired
private TwsScheduleDao twsScheduleDao;

/**
 * Get the TwsSchedule report for the date range and state
 * @param claimFilterModel
 * @return List<TwsScheduleModel>
 */
@Override
public List<TwsScheduleModel> getTwsScheduleReport(ClaimFilterModel claimFilterModel, String 
employeeMsId) {

    return twsScheduleDao.getTwsScheduleReport(claimFilterModel, employeeMsId);


}

我的DAO:-

**import statments**

@Transactional
@Repository
@PropertySource("classpath:sql/dailyOps-sql.properties")

public class TwsScheduleDaoImpl implements TwsScheduleDao {
private static final Logger logger = LogManager.getLogger(TwsScheduleDaoImpl.class);

@Value("${fetchAll_TwsSchedule}")
private String fetchAll_TwsSchedule_SQL;

@Value("${fetch_TwsSchedule_byState}")
private String fetch_TwsSchedule_byState_SQL;

@Autowired
Utility utility;
@Autowired
@Qualifier("enc1NamedJdbcTemplate")
private NamedParameterJdbcTemplate namedParameterJdbcTemplateEnc1;
@Autowired
@Qualifier("enc2NamedJdbcTemplate")
private NamedParameterJdbcTemplate namedParameterJdbcTemplateEnc2;
@Autowired
Messenger messenger;

/**
 * get the TwsSchedule report for the date range and state
 * @param geoState
 * @return List<TwsScheduleModel>
 */
@Override
public List<TwsScheduleModel> getTwsScheduleReport(ClaimFilterModel claimFilterModel, String 
employeeMsId) {
    Map<String, Object> paramMap = new HashMap<String, Object>();
    paramMap.put("geo_state", claimFilterModel.getState());
    paramMap.put(Constants.EMPLOYEEMSID, employeeMsId);
    RowMapper<TwsScheduleModel> rowMapper = new BeanPropertyRowMapper<TwsScheduleModel> 
 (TwsScheduleModel.class);

    List<TwsScheduleModel> tws_model = new ArrayList<TwsScheduleModel>();

    // If geoState is set to ALL fetch from both database instances
    if (claimFilterModel.getState().equals("ALL"))
    {
        tws_model = utility.getNamedParameterJdbcTemplate(1)
                .query(fetchAll_TwsSchedule_SQL, paramMap, rowMapper);

        tws_model.addAll(utility.getNamedParameterJdbcTemplate(2)
                .query(fetchAll_TwsSchedule_SQL, paramMap, rowMapper));
    }
    /* If there is more than one state provided, split the state input and delimit by "," then fetch 
  from corresponding
     * database for each state */
    else if(claimFilterModel.getState().length() > 3)
    {
        String [] my_split = claimFilterModel.getState().split(",");
        for(int i = 0; i < my_split.length; i++)
        {
            paramMap.put("geo_state", my_split[i]);
            if (utility.getNamedParameterJdbcTemplate(my_split[i]) == null) {

  tws_model.addAll(namedParameterJdbcTemplateEnc1.query(fetch_TwsSchedule_byState_SQL,paramMap, 
  rowMapper));

  tws_model.addAll(namedParameterJdbcTemplateEnc2.query(fetch_TwsSchedule_byState_SQL,paramMap, 
  rowMapper));      
            }
            else{
                tws_model.addAll(utility.getNamedParameterJdbcTemplate(my_split[i])
                .query(fetch_TwsSchedule_byState_SQL,paramMap, rowMapper)); 
            }
        }
    }
    // If there is only one state provided, fetch from the corresponding database
    else
    {
        if (utility.getNamedParameterJdbcTemplate(claimFilterModel.getState()) == null) {

    tws_model.addAll(namedParameterJdbcTemplateEnc1.query(fetch_TwsSchedule_byState_SQL,paramMap, 
    rowMapper));

    tws_model.addAll(namedParameterJdbcTemplateEnc2.query(fetch_TwsSchedule_byState_SQL,paramMap, 
    rowMapper));        
        }
        else{
            tws_model = utility.getNamedParameterJdbcTemplate(claimFilterModel.getState())
                    .query(fetch_TwsSchedule_byState_SQL,paramMap, rowMapper);
        }

    }
    return tws_model;
  }


}

1 个答案:

答案 0 :(得分:0)

By default spring boot don't allow cross origin requests you can add the below configuration(CorsConfiguration) so that it will allow the requests

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and()
            //other config
    }

    @Bean
    CorsConfigurationSource corsConfigurationSource()
    {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("https://example.com"));
        configuration.setAllowedMethods(Arrays.asList("GET","POST"));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}