Input data:
Keycloak:
/developer/**
(it's a prefix for my developer-service endpoint)Case: in browser making request
http://localhost:8765/developer/developers
logging in as manager
!!!
EXPECTED: access denied
ACTUAL: 200 with response = list of developers
QUESTION:
have i missed something ? Is this role permission filtration inside of keacloak already?
Have already watched several videos and posts, some of them are based on front-end keycloak-js
lib and filtration, backend @RolesAllowed
. I'm just curious if it's possible to block the request just using the keycloak admin console?
GATEWAY yaml:
server:
port: 8765
logging:
level:
root: info
eureka:
client:
serviceUrl:
defaultZone: http://eurekauser:eureka!@localhost:8761/eureka
instance:
hostname: localhost
prefer-ip-address: false
spring:
application:
name: GATEWAY
cloud:
gateway:
discovery.locator.enabled: true
routes:
- id: developer
uri: lb://DEVELOPER
predicates:
- Path=/developer/**
filters:
TokenRelay=
security:
oauth2:
client:
registration:
keycloak:
provider: keycloak
client-id: test_client
client-secret: lBIz3la07j3a5uEEFdQgoapFa4s1seeD
authorization-grant-type: authorization_code
redirect-uri: "http://localhost:${server.port}/login/oauth2/code/{registrationId}"
scope:
- openid
provider:
keycloak:
issuer-uri: http://localhost:8080/realms/TestRealm
authorization-uri: http://localhost:8080/realms/TestRealm/protocol/openid-connect/auth
token-uri: http://localhost:8080/realms/TestRealm/protocol/openid-connect/token
user-info-uri: http://localhost:8080/realms/TestRealm/protocol/openid-connect/userinfo
jwk-set-uri: http://localhost:8080/realms/TestRealm/protocol/openid-connect/certs
jackson:
date-format: yyyy-MM-dd HH:mm:ss
DEVELOPER SERVICE: YAML:
server:
port: 8082
error:
include-message: always
servlet:
context-path: /developer
spring:
application:
name: DEVELOPER
security:
oauth2:
resource-server:
jwt:
jwk-set-uri: http://localhost:8080/realms/TestRealm/protocol/openid-connect/certs
eureka:
client:
serviceUrl:
defaultZone: http://eurekauser:eureka!@localhost:8761/eureka
instance:
prefer-ip-address: false
hostname: localhost
Endpoint:
@RestController
@RequestMapping("/developers")
class DevelopController {
private val developers = mapOf(
Pair(1L, "developer#1"),
Pair(2L, "developer#2"),
Pair(3L, "developer#3"),
Pair(4L, "developer#4"),
Pair(5L, "developer#5")
)
@GetMapping
fun findAll(authentication: Authentication) = developers.entries
@GetMapping("/{developerId}")
fun findById(@PathVariable developerId: Long): String = developers[developerId] ?: let {
throw RuntimeException("Not found by id=$developerId")
}
}
Role based access-control won't be achieved on access-token emission: an access-token is emitted for a given user on a specific client and can be used to authorize many requests to many resources.
Details for role-based access-control in Spring-boot resource-servers in the accepted answer to "Use Keycloak Spring Adapter with Spring Boot 3"
There is an authorization-service in Keycloak which you could use to centralize access-control, but:
@Controller
method