[英]Spring Boot + React CORS issue without spring security
我使用 Spring Boot 2.2.2.RELEASE 作為 REST 服務和 React 作為前端。
只是實現了一個簡單的 GET 方法,但是在通過 REACT 與服務器通信時出現 CORS 問題。
https://spring.io/guides/gs/rest-service-cors/ ->點擊了這個鏈接,但沒有運氣。
我的 Spring 啟動 Controller:
@RestController
public class FeedController {
@Autowired
private IFeedService IFeedService;
@CrossOrigin(origins = "http://localhost:3000")
@GetMapping(path="/v1/getdashboard")
public ResponseEntity<String> feedDashBoardController(){
String result = null;
HttpStatus httpStatus = HttpStatus.BAD_REQUEST;
try {
List<FeedData> dashBoardFeedInfo = IFeedService.getDashBoardFeedService();
// Create ObjectMapper
ObjectMapper mapper = new ObjectMapper();
JsonNode dataNode = mapper.valueToTree(dashBoardFeedInfo);
result = FeedResponseData.generateFeedResponse(dataNode);
httpStatus = HttpStatus.OK;
}catch(TBServiceException e) {
result = AppExceptions.handleException("Something Went Wrong");
httpStatus = HttpStatus.BAD_REQUEST;
}
return new ResponseEntity<String>(result,httpStatus);
}
}
我的 Spring 啟動應用程序:
@SpringBootApplication
public class TechnicalBlogApplication {
public static void main(String[] args) {
SpringApplication.run(TechnicalBlogApplication.class, args);
System.out.println("Application Main - Update -1");
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/v1/getdashboard").allowedOrigins("http://localhost:3000");
}
};
}
}
我的 Spring 應用程序屬性:
spring.profiles.active=dev
server.port=6001
server.servlet.context-path=/technical-blog
我的反應代碼片段:
async componentDidMount() {
const dashboardData= await fetch("http://localhost:6001/technical-blog/v1/getdashboard");
console.log("dash ",dashboardData)
}
我也試過設置標題,下面是重新修改的 controller。 我得到了多個 CORS 定義錯誤。
@RestController
public class FeedController {
@Autowired
private IFeedService IFeedService;
@CrossOrigin(origins = "http://localhost:3000")
@GetMapping(path="/v1/getdashboard")
public ResponseEntity<String> feedDashBoardController(){
String result = null;
HttpStatus httpStatus = HttpStatus.BAD_REQUEST;
try {
List<FeedData> dashBoardFeedInfo = IFeedService.getDashBoardFeedService();
// Create ObjectMapper
ObjectMapper mapper = new ObjectMapper();
JsonNode dataNode = mapper.valueToTree(dashBoardFeedInfo);
result = FeedResponseData.generateFeedResponse(dataNode);
httpStatus = HttpStatus.OK;
}catch(TBServiceException e) {
result = AppExceptions.handleException("Something Went Wrong");
httpStatus = HttpStatus.BAD_REQUEST;
}
return new ResponseEntity<String>(result,setHeaders(),httpStatus);
}
}
private HttpHeaders setHeaders() {
List<HttpMethod> allowedMethods = new ArrayList<>();
allowedMethods.add(HttpMethod.GET);
allowedMethods.add(HttpMethod.POST);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
//httpHeaders.setAccessControlAllowOrigin("*");
httpHeaders.setAccessControlAllowCredentials(true);
httpHeaders.setAccessControlAllowMethods(allowedMethods);
httpHeaders.setAccessControlMaxAge(3600);
return httpHeaders;
}
我認為您應該將@CrossOrigin(origins = "http://localhost:3000")
放在 controller 上,因為請求的第一件事是 controller 而不是 ZC1C425268E68385D1ABAZ7C157
所以會是這樣
@RestController
@CrossOrigin(origins = "http://localhost:3000")
public class FeedController {
@Autowired
private IFeedService IFeedService;
@GetMapping(path="/v1/getdashboard")
public ResponseEntity<String> feedDashBoardController(){
String result = null;
HttpStatus httpStatus = HttpStatus.BAD_REQUEST;
try {
List<FeedData> dashBoardFeedInfo = IFeedService.getDashBoardFeedService();
// Create ObjectMapper
ObjectMapper mapper = new ObjectMapper();
JsonNode dataNode = mapper.valueToTree(dashBoardFeedInfo);
result = FeedResponseData.generateFeedResponse(dataNode);
httpStatus = HttpStatus.OK;
}catch(TBServiceException e) {
result = AppExceptions.handleException("Something Went Wrong");
httpStatus = HttpStatus.BAD_REQUEST;
}
return new ResponseEntity<String>(result,setHeaders(),httpStatus);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.