I have created two api's in my spring mvc project which have identical structure. But while calling using Postman or while hitting the APIs in browser/UI project, one is working without any credential or cookie[SessionId] but another one is not working. Please help. Thanks is advance
code of API which is not working without session/credentials:
@CrossOrigin(origins = "http://123.123.151.123:4201")
@RequestMapping(value = "/troubleshootingHitRate",method = RequestMethod.GET)
public ResponseEntity<Map<String, Object>> troubleshootingHitRate(@RequestParam String fromDate,
@RequestParam String toDate) {
System.out.println("=== Controller::" + " " + "FromDate::" + fromDate + " " + "ToDate::" + toDate);
Map<String, Object> failureReport = ssaService.hitRateWithComparison(fromDate, toDate, "failure");
HttpHeaders headers = new HttpHeaders();
headers.add("CustomHeader", "Custom-Header");
return new ResponseEntity<>(failureReport, headers, HttpStatus.OK);
}
code of API which is working without session/credentials:
public String report(@PathVariable("envid") int envid, Model model) {
String report_path = reportPath;
// System.out.println("Teating"+ report_path);
report_path = report_path.replace("////", "\\\\\\\\");
report_path = report_path.replace("/", "\\\\");
// System.out.println("Teating08"+ report_path);
List<Result> result = ssaService.getReport(envid + "");
model.addAttribute("reportPaths", report_path);
model.addAttribute("report", result);
model.addAttribute("envid", envid);
return "report";
}
Configuration Details:
public UrlBasedCorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("http://123.123.151.123:4201")); // Allow all origins or specify your front-end URL
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
configuration.setAllowedHeaders(Arrays.asList("*"));
configuration.setAllowCredentials(false); // set true if using cookies/auth
configuration.setMaxAge(3600L);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
@CrossOrigin(origins = "http://123.123.151.123:4201")seems to be the most obvious difference between the two methods.