gpt4 book ai didi

java - Spring Boot + React CORS 问题,没有 spring security

转载 作者:行者123 更新时间:2023-12-01 17:11:32 25 4
gpt4 key购买 nike

我使用 Spring Boot 2.2.2.RELEASE 作为 REST 服务,并使用 React 作为前端。

只是实现了一个简单的 GET 方法,但是通过 REACT 与服务器通信时出现 CORS 问题。

https://spring.io/guides/gs/rest-service-cors/ -> 点击此链接,但没有成功。

我的 Spring Boot 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 Boot 应用程序:

@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

我的 react 代码片段:

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 而不是函数

所以就会像这样

@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);
}
}

关于java - Spring Boot + React CORS 问题,没有 spring security,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61426673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com