简体   繁体   English

Crossorigin不起作用

[英]Crossorigin doesn't work

React keep me saying Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8080/users . React让我说“跨域请求被阻止”:“同源策略”不允许读取http:// localhost:8080 / users上的远程资源。 (Reason: CORS header 'Access-Control-Allow-Origin' missing). (原因:CORS标头“ Access-Control-Allow-Origin”缺失)。

The strange think it worked to me and it stopped working without any change to the controller code... 奇怪的是,它对我有用,并且在不更改控制器代码的情况下停止了工作。

UserController.java UserController.java

@RestController
@CrossOrigin
@RequestMapping(value = "/users")
public class UserController {

@Autowired
private UserService userService;

@RequestMapping(value = "/{userId}", method = RequestMethod.GET)
private User findOne(@PathVariable("userId") Integer userId) {
    return userService.findOne(userId);
}

@RequestMapping(method = RequestMethod.GET)
private List<User> findAll() {
    return userService.findAll();
}

@RequestMapping(method = RequestMethod.POST)
private User create(@RequestBody User user) {
    user.setId(null); // To ensure we create instead of update
    return userService.save(user);
}

@RequestMapping(value = "/{userId}", method = RequestMethod.PUT)
private User update(@PathVariable("userId") Integer userId, @RequestBody User user) {
    user.setId(userId); // To ensure we update instead of create
    return userService.save(user);
}

@RequestMapping(value = "/{userId}", method = RequestMethod.DELETE)
private void delete(@PathVariable("userId") Integer userId) {
    final User user = userService.findOne(userId);
    userService.delete(user);
}
}

and this is my fetch on React 这是我在React上获取的

 fetch('http://localhost:8080/users', {
    method: 'GET',
    headers: {
    'content-type': 'application/json'
  },
}).then(response => { return response.json();
}).then(data => {
  this.setState({users:data});
});
}

Any reason why it stopped working suddenly? 它突然停止工作的原因是什么?

edit: 编辑:

I tried making a clean new maven project and copied all packages into the new project and now it works, but I still don't know why it stopped work, the code is the same and now its working. 我尝试制作一个干净的新Maven项目并将所有包复制到该新项目中,现在它可以工作了,但是我仍然不知道为什么它停止工作,代码相同,并且现在可以工作。

If you are using Spring Security and want CORS globally in your project then try to add it to the configuration: 如果您使用的是Spring Security,并且希望在项目中全局使用CORS,请尝试将其添加到配置中:

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {

    httpSecurity
            .cors()
            .and()
            ........ other config

}

But before that try the following approaches. 但在此之前,请尝试以下方法。 To apply CORS on class level use it like this: 要在班级应用CORS,请按以下方式使用它:

@CrossOrigin(origins = "http://domain2.com", maxAge = 3600)
@Controller
public class SomeController{

}

or use it in method level like this: 或在这样的方法级别使用它:

@CrossOrigin("http://example.com")
@RequestMapping("/some")
public Account retrieve() {
    // ...
}

OR 要么

@CrossOrigin(origins = "*")
@RequestMapping("/some")
public Account retrieve() {
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM