简体   繁体   中英

Problem with Spring Boot Security and CORS when it comes to POST requests

I want to secure a REST service for personal use and used as a backed for an Angular app using method to method security like this.

    @PreAuthorize("hasPermission(#id, 'Player', 'expenseReport.allowed')")
    @GetMapping(value = "editplayer")
    @CrossOrigin(origins = crossorg)
    public Player getEditPlayerAuthorize(@RequestParam Long id){
        return fetcher.fetchPlayer(id);
    }

The @CrossOrigin annotation works perfectly where, but when I try the same on a POST request it fails because of CORS:

    @PostMapping(value = "login", produces = "application/json")
    @CrossOrigin(origins = crossorg)
    public Login attemptLogin(@RequestBody Login payload) {        
        return handler.attemptLogin(payload);
    }

So I guess Spring Boot security is somehow interfering in this, but I see no solution why this should affect only POST requests? They worked fine before I added Spring Boot Security.

  1. Add the proper url for @CrossOrigin? (For eg. localhost url of front end).

  2. default enabled csrf may have to be disabled.

refer this How to Solve 403 Error in Spring Boot Post Request

Request Experts to help resolve this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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