title: Spring Security + Keycloak Authorization: How to associate an endpoint with a Resource?
body: <p>I have created a Spring Boot app with <code>spring-boot-starter-security</code> and <code>keycloak-spring-security-adapter</code> and authenticating users with Keycloak works as intended.</p>
  <p>I now want to use Keycloak's <a href="https://www.keycloak.org/docs/latest/authorization_services/index.html#_overview" rel="nofollow noreferrer">Authorization Services</a> for fine-grained access control to my API.</p>
  <p>In Keycloak, I've enabled Authorization for my client. I've created a Role and I've assigned the Role to my test User. I've created a Resource and a Resource-based Permission and I've added a Role-based Policy. Using the Evaluate feature, I've confirmed, that my user has permission to access my resource.</p>
  <p><a href="https://i.stack.imgur.com/LTspe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LTspe.png" alt="enter image description here" /></a></p>
  <p>Let's say I have the following controller with one endpoint.</p>
  <pre><code>@RestController
  class GreetingController {
  
      @GetMapping(&quot;/hello&quot;)
      fun getPatientInfo(principal: Principal): String {
          return &quot;Hello, ${principal.name}!&quot;
      }
  }
  </code></pre>
  <p>Now my question is: How do I associate an endpoint in my Spring Boot application with the given Resource so that the permission is evaluated when a request is made?</p>