I get forbidden when i'm trying to post from postman in spring boot app with keycloak and mysql docker containers
I started to learn Spring Cloud Microservices from Manning - Spring Microservice in Action, and how to use Keycloak with my spring apps. But for the last few days i can't go further... I create keycloak docker container successfully, connected to mysql container in docker compose, the tabel from our spring app is created in mysql db, but when i'm trying to post on db, i get HTTP ERROR 403: FORBIDDEN, every time, i write simple test with roles, to see if i can access a simple String output and it's works, but not post for mysql db and i don't know why. I actived the logs for Keycloack container... get nothing wrong, the only thing i get is from mysql container with: mbind: Operation not permitted, but idk if it s related to this. Below i show my configuration, and please help me!
@RestController
@RequestMapping(value = "/facultate")
public class FacultateController {
private FacultateService facultateService;
public FacultateController(FacultateService facultateService) {
this.facultateService = facultateService;
}
//forbidden
@RolesAllowed({"USER", "ADMIN"})
@PostMapping(value = "/post")
public ResponseEntity postFacultate(@RequestBody Facultate facultate) {
facultateService.save(facultate);
return new ResponseEntity(HttpStatus.CREATED);
}
//forbidden
@RolesAllowed("ADMIN")
@PostMapping(value = "/")
public String save(@RequestBody Facultate facultate)
{
facultateService.save(facultate);
return "da";
}
//forbidden
@PostMapping(value = "/create")
public String create(@RequestBody Facultate facultate)
{
facultateService.save(facultate);
return "da";
}
//forbidden
@GetMapping(value = "/{facultateNameId}")
public ResponseEntity<Facultate> findById(@PathVariable("facultateNameId") String facultateNameId)
{
return new ResponseEntity<>(facultateService.getById(facultateNameId), HttpStatus.ACCEPTED);
}
//work
@RolesAllowed("USER")
@GetMapping(value = "/hi")
public String sal()
{
return "merge";
}
//work
@RolesAllowed({"USER", "ADMIN"})
@GetMapping(value = "/test")
public String test()
{
return "merge";
}
//work
@RolesAllowed({"ADMIN" , "USER"})
@GetMapping(value = "/test2")
public String test2()
{
return "merge";
}
//work
@RolesAllowed("ADMIN")
@GetMapping(value = "/test3")
public String test3()
{
return "merge";
}
-it works everything except post methods, doen't care if i used with roles or not, authentificated or not, authorzated or not, doesn't work against db
docker compose file:
version: '3'
services:
keycloak:
image: jboss/keycloak
restart: always
environment:
DB_VENDOR: MYSQL
DB_ADDR: database
DB_DATABASE: petrea
DB_USER: root
DB_PASSWORD: ravage123
KEYCLOAK_USER: admin
KEYCLOAK_PASSWORD: admin
KEYCLOAK_LOGLEVEL: DEBUG
JDBC_PARAMS: "connectTimeout=30000"
ports:
- "8080:8080"
depends_on:
- database
database:
image: mysql:8.0
container_name: database
restart: always
ports:
- 3306:3306
environment:
MYSQL_DATABASE: petrea
MYSQL_ROOT_PASSWORD: ravage123
MYSQL_ROOT_USER: root
If you want anyelse to see, please say
Solution 1:
Did you set up the CORS correctly? The origin that is requesting must be allowed to do so. Check out this video on CORS. In your main class for the Rest Application, there is a method called addCorsMappings
that you can override. Add a mapping to the CorsRegistry. These should be the domains you expect to be calling the REST app from.