有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

没有Spring安全性的java Spring Boot+React CORS问题

我使用的是SpringBoot2.2.2。作为休息服务释放,并对前端作出反应

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

https://spring.io/guides/gs/rest-service-cors/->;跟踪此链接,但没有运气

我的Spring启动控制器:

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

我也尝试过设置标题,下面是重新修改的控制器。我得到多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;
}

共 (1) 个答案

  1. # 1 楼答案

    我认为您应该将@CrossOrigin(origins = "http://localhost:3000")放在它自己的控制器上,因为请求首先到达的是控制器,而不是函数

    就是这样

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