Java 不能张贴或放置。如何更改“否”访问控制允许来源“标题”以允许这些呼叫?

Java 不能张贴或放置。如何更改“否”访问控制允许来源“标题”以允许这些呼叫?,java,reactjs,cors,Java,Reactjs,Cors,我知道这个问题有很多答案。但我就是过不了这一关 Access to XMLHttpRequest at 'http://localhost:8080/user/2' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin'

我知道这个问题有很多答案。但我就是过不了这一关

Access to XMLHttpRequest at 'http://localhost:8080/user/2' from origin 'http://localhost:3000' has been blocked by CORS policy: 
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
我的get和delete路由工作正常,但是post和put调用被CORS错误阻止

我如何在我的reactjs应用程序中编辑配置以使其正常工作

我想我已经在Java控制器中说明了这个问题。我告诉java服务器localhost:3000很好。我的前端是否需要一些东西来编辑标题

@CrossOrigin(origins = { "http://localhost:3000", "http://localhost:4200" })
@RestController
public class UserController {

    @Autowired
    private UserRepository repo;

    @GetMapping("/user")
    public Iterable<User> getAllUsers() {
        return repo.findAll();
    }

    @GetMapping("/user/{id}")
    public User getUser(@PathVariable Long id) {
        Optional<User> user = repo.findById(id);
        if (id == -1) {
            return new User();
        }

        if (user == null) {
            return null;
        }

        return user.get();
    }

    @DeleteMapping("/user/{id}")
    public User deleteUser(@PathVariable Long id) {
        Optional<User> user = repo.findById(id);
        if (user == null) {
            return null;
        }

        repo.delete(user.get());
        return user.get();  
    } 

    @PutMapping("user/{id}")
    public User updateUser(@RequestBody User user) {
        User updatedUser = repo.save(user);
        return updatedUser;
    }

    @PostMapping("/user")
    public User addUser(@RequestBody User user) {
        if (user == null) {
            return null;
        }

        User createdUser = repo.save(user);
        return createdUser;
    }
}

你能在“网络”选项卡中看到飞行前请求是否通过吗?或者你们可以分享你们在网络中看到的东西吗?我猜您尚未将Spring配置为接受飞行前请求。我在寻找什么来查看请求是否通过?您可以检查HTTP请求的状态代码。您可以使用浏览器devtools中的网络窗格来检查响应的HTTP状态代码。您可以在网络选项卡中查看飞行前请求是否通过或通过不或者你们可以分享你们在网络中看到的东西吗?我猜您尚未将Spring配置为接受飞行前请求。我在寻找什么来查看请求是否通过?您可以检查HTTP请求的状态代码。您可以使用浏览器devtools中的网络窗格来检查响应的HTTP状态代码。
import axios from 'axios'

const USER_API_URL = 'http://localhost:8080'

class UserDataService {
    retrieveAllUsers() {
        return axios.get(`${USER_API_URL}/user`);
    }

    deleteUser(id) {
        console.log('excuted user deletion')
        return axios.delete(`${USER_API_URL}/user/${id}`);
    }

    getUser(id) {
        console.log('excuted user get')
        return axios.get(`${USER_API_URL}/user/${id}`);
    }

    createUser(user) {
        console.log('executed user create')
        return axios.post(`${USER_API_URL}/user)`, user);
    }

    updateUser(user) {
        console.log('executed user update')
        return axios.put(`${USER_API_URL}/user/${user.id}`, user);
    }
}

export default new UserDataService()