រំលងទៅមាតិកា
Rotha Dapravith

ការផ្តល់សិទ្ធិលម្អិតជាមួយ Keycloak

API បញ្ជាទិញអាហារជាមួយ Spring Boot ដែល Keycloak Authorization Services សម្រេចសិទ្ធិចូលប្រើតាម resource និង HTTP method ជំនួសឱ្យការពិនិត្យ role នៅក្នុង controller នីមួយៗ។

ប្រភេទ
គម្រោងផ្ទាល់ខ្លួន
ឆ្នាំ
2024
ស្ថានភាព
ដំណើរការលើម៉ាស៊ីនផ្ទាល់
បច្ចេកវិទ្យា
  • Java 21
  • Spring Boot
  • Spring Security
  • Keycloak
  • PostgreSQL
  • Docker Compose

ការសិក្សាករណីលម្អិតនេះមានជាភាសាអង់គ្លេសនៅឡើយ។

បញ្ហា

Single sign-on tells an API who the caller is, not what they may do. This project moves those decisions into Keycloak for a small restaurant domain: public menus, staff-managed restaurants and customer orders.

Express rules such as “anyone can read menus”, “only staff can change a restaurant” and “users can only read their own profile” without hard-coding role checks in every controller, and keep them changeable without a redeploy.

តួនាទីរបស់ខ្ញុំ

Personal project. I wrote the Spring Boot API and its security configuration, and exported the Keycloak realm and Docker Compose setup so it can be recreated.

វិធីសាស្ត្រ

Authorization is treated as configuration, not code. Resources, scopes and policies are defined in the Keycloak realm; the API only enforces the decision. The realm is exported to JSON and started with Docker Compose, so the whole setup can be recreated from the repository.

ស្ថាបត្យកម្ម

Spring Security runs as a stateless bearer-token resource server. Once a token is accepted, Keycloak’s policy-enforcer filter asks Keycloak whether this path and HTTP method are allowed, using the resources and policies stored in the realm.

  1. Client
    • Browser or API client
  2. Identity
    • Keycloakrealm dive-dev · Authorization Services
  3. API
    • Spring Securitybearer token · no sessions
    • Policy enforcerpath + method → permission
    • Controllers/restaurant · /order · /keycloak/users
  4. Data
    • PostgreSQLrestaurants · menus · orders
ទិដ្ឋភាពរួមនៃប្រព័ន្ធ។ អានស្រទាប់ពីលើចុះក្រោម ហើយការតភ្ជាប់មានរាយខាងក្រោម។

លំហូរសំណើ

  1. The client signs in through Keycloak and sends the access token as a bearer token.
  2. Spring Security validates the token; no HTTP session is created.
  3. The policy enforcer maps the path to a Keycloak resource and the HTTP method to a scope, then asks Keycloak for a decision.
  4. Public paths such as /restaurant/public/** and the API docs skip enforcement. /keycloak/users/{userId} passes the request URI as a claim so a policy can compare it with the caller.
  5. Allowed requests reach the controllers, which use Spring Data repositories on PostgreSQL.

ការសម្រេចចិត្តបច្ចេកទេស

Keycloak Authorization Services
Policies live in the identity server and can change without redeploying the API.
HTTP method as scope
GET, POST and PUT become separate permissions on one resource with no extra code.
Claim information point
Feeds request data (the URI) into policy evaluation for ownership-style rules.
Stateless sessions
Every request carries its own token, so instances can be added without sticky sessions.
Docker Compose
Keycloak and PostgreSQL start with one command, and the realm is imported from a JSON export.

ការអនុវត្ត

SecurityConfig adds Keycloak’s ServletPolicyEnforcerFilter after Spring’s BearerTokenAuthenticationFilter and turns sessions off. The enforcer reads a JSON file on the classpath that lists protected and public paths.

User and role operations go through the Keycloak admin client, wrapped in small resource classes, so the API manages identities through the same server that issues tokens.

The realm, clients and authorization settings are exported to keycloak/realm-export.json.

បញ្ហាប្រឈម និងដំណោះស្រាយ

  1. បញ្ហាប្រឈម

    Role checks spread across controllers are hard to audit, and changing one needs a redeploy.

    ដំណោះស្រាយ

    Keycloak’s ServletPolicyEnforcerFilter runs after Spring’s bearer-token filter; each path maps to a Keycloak resource and each HTTP method to a scope.

  2. បញ្ហាប្រឈម

    “Users can only read their own profile” depends on the request, not just on the caller’s role.

    ដំណោះស្រាយ

    A claim information point passes the request URI into policy evaluation, so a Keycloak policy can compare it with the caller.

  3. បញ្ហាប្រឈម

    Menus and API docs must stay public while everything else is protected.

    ដំណោះស្រាយ

    The enforcer configuration lists public paths explicitly; any path not listed is checked by Keycloak (default deny).

សុវត្ថិភាព និងភាពជឿជាក់

  • Default deny: any path not explicitly marked public is checked by Keycloak.
  • CSRF protection is off because the API is stateless and authenticates with bearer tokens, not cookies.
  • Ownership rules are Keycloak policies driven by the request URI, not controller code.

ការធ្វើតេស្ត

The repository contains a Spring Boot context-load test. The authorization rules themselves have no automated tests yet.

ការប្តូរយកអ្វីមួយ

  1. ជម្រើស

    Authorization decisions in Keycloak

    តម្លៃដែលត្រូវបង់

    Protected requests depend on Keycloak’s policy evaluation, and the rules live outside the codebase where code review does not see them.

  2. ជម្រើស

    Stateless sessions with CSRF protection off

    តម្លៃដែលត្រូវបង់

    Instances can be added without sticky sessions, but the API must not move to cookie-based authentication without turning CSRF protection back on.

លទ្ធផល

A runnable example of keeping authorization decisions in Keycloak rather than in application code, covering public paths, per-method permissions and a URI-based ownership check.

មេរៀន និងជំហានបន្ទាប់

ចំណុចខ្វះខាតដែលដឹង

  • The client secret for the local realm is committed in policy-enforcer.json; it should come from environment configuration.
  • No automated tests cover the policies, so a realm change could silently open or close a path.

អ្វីដែលខ្ញុំនឹងកែប្រែបន្ទាប់

  • Move the client secret out of policy-enforcer.json into environment configuration.
  • Add integration tests that call each protected path as different users, so a realm change that opens or closes a path fails the build.

គម្រោងបន្ទាប់ការតាមដាន metrics សម្រាប់ Spring microservices