title: keycloak spring boot always receives Anonymous Authentication token
body: <p>Im using keycloak spring boot adapter version 10.0.2 and I am trying to get the username using the authentication token, for that I created a web filter and trying to get the Principal but although the user is authenticated I only get Anonymous user like this:</p>
  <pre><code>org.springframework.security.authentication.AnonymousAuthenticationToken@81a4f960: 
  Principal: anonymousUser; 
  Credentials: [PROTECTED]; Authenticated: true; 
  Details: org.springframework.security.web.authentication.WebAuthenticationDetails@0: 
  RemoteIpAddress: 10.128.15.244; 
  SessionId: 6F41901438BFC9AD6FEEB34A3A6926EB; 
  Granted 
  Authorities: ROLE_ANONYMOUS
  </code></pre>
  <p>I have the following application.yaml</p>
  <pre><code>cheques.paging.size: 10
  spring.jpa.hibernate.ddl-auto: none
  spring.jpa.hibernate.use-new-id-generator-mappings: FALSE
  spring.datasource.url: &quot;jdbc:oracle:thin:@${db_host}:${db_port}/${db_instance}&quot;
  spring.datasource.username: &quot;${db_user}&quot;
  spring.datasource.password: &quot;${db_password}&quot;
  spring.datasource.driver.class: &quot;${db_driver}&quot;
  spring.jpa.properties.hibernate.dialect: &quot;org.hibernate.dialect.Oracle10gDialect&quot;
  spring.jpa.hibernate.naming.physical-strategy: &quot;org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl&quot;
  management.health.ldap.enabled: false
  management.endpoints.enabled-by-default: false
  management.endpoint.health.enabled: true
  
  keycloak:
    realm: mrdc
    auth-server-url: ${keycloak_url}
    ssl-required: external
    principal-attribute: preferred_username
    disableTrustManager: true
    allow-any-hostname: true
    resource: mrdc-client
    cors: true
    credentials:
      secret: secret
    securityConstraints:
      - securityCollections:
          - name: default
            patterns:
              - /*
        authRoles:
          - user
    policy-enforcer-config:
      lazy-load-paths: false
      enforcement-mode: ENFORCING
      paths:
        - name: actuator
          path: /actuator/*
          enforcement-mode: DISABLED
          methods:
            - method: GET
  </code></pre>
  <p>I really want to know what I've done wrong,I mean I have used keycloak before and used a similar web filter and always got the username like this:</p>
  <pre><code>public class CustomerFilter extends GenericFilterBean {
      private static final Logger LOGGER = LoggerFactory.getLogger(CustomerFilter.class);
  
      @Override
      public void doFilter(ServletRequest servletRequest,
                           ServletResponse servletResponse,
                           FilterChain filterChain) throws IOException, ServletException {
          Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
          HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
          Principal userPrincipal = httpServletRequest.getUserPrincipal();
          if (userPrincipal != null) {
              userPrincipal.getName();
              LOGGER.info(&quot;username: &quot; + userPrincipal);
          }
          filterChain.doFilter(servletRequest, servletResponse);
      }
  }
  </code></pre>
  <p>and here is my keycloak realm.json file in case that helps:</p>
  <pre><code>{
    &quot;realm&quot;: &quot;mrdc&quot;,
    &quot;displayName&quot;: &quot;PS-mRDC&quot;,
    &quot;enabled&quot;: true,
    &quot;requiredCredentials&quot;: [
      &quot;password&quot;
    ],
    &quot;users&quot;: [
      {
        &quot;username&quot;: &quot;service-account-mrdc&quot;,
        &quot;enabled&quot;: true,
        &quot;serviceAccountClientId&quot;: &quot;mrdc-client&quot;,
        &quot;clientRoles&quot;: {
          &quot;mrdc-client&quot;: [
            &quot;uma_protection&quot;
          ]
        }
      },
      {
        &quot;username&quot;: &quot;mrdc.user&quot;,
        &quot;enabled&quot;: true,
        &quot;credentials&quot;: [
          {
            &quot;type&quot;: &quot;password&quot;,
            &quot;value&quot;: &quot;mrdc&quot;
          }
        ],
        &quot;realmRoles&quot;: [],
        &quot;groups&quot;: [
          &quot;user-group&quot;
        ]
      },
      {
        &quot;username&quot;: &quot;admin.user&quot;,
        &quot;enabled&quot;: true,
        &quot;credentials&quot;: [
          {
            &quot;type&quot;: &quot;password&quot;,
            &quot;value&quot;: &quot;admin&quot;
          }
        ],
        &quot;realmRoles&quot;: [],
        &quot;groups&quot;: [
          &quot;user-group&quot;,
          &quot;admin-group&quot;
        ]
      }
    ],
    &quot;roles&quot;: {
      &quot;realm&quot;: [
        {
          &quot;name&quot;: &quot;user&quot;
        }
      ]
    },
    &quot;groups&quot;: [
      {
        &quot;name&quot;: &quot;user-group&quot;,
        &quot;path&quot;: &quot;user-group&quot;,
        &quot;attributes&quot;: {},
        &quot;realmRoles&quot;: [
          &quot;user&quot;
        ],
        &quot;clientRoles&quot;: {
          &quot;account&quot;: [
            &quot;view-profile&quot;,
            &quot;manage-account&quot;
          ]
        },
        &quot;subGroups&quot;: []
      },
      {
        &quot;name&quot;: &quot;admin-group&quot;,
        &quot;path&quot;: &quot;admin-group&quot;,
        &quot;attributes&quot;: {},
        &quot;realmRoles&quot;: [
          &quot;user&quot;
        ],
        &quot;clientRoles&quot;: {
          &quot;account&quot;: [
            &quot;view-profile&quot;,
            &quot;manage-account&quot;
          ],
          &quot;realm-management&quot;: [
            &quot;view-users&quot;,
            &quot;view-realm&quot;,
            &quot;view-clients&quot;,
            &quot;manage-users&quot;
          ]
        },
        &quot;subGroups&quot;: []
      }
    ],
    &quot;scopeMappings&quot;: [
      {
        &quot;client&quot;: &quot;mrdc-client&quot;,
        &quot;roles&quot;: [
          &quot;user&quot;
        ]
      },
      {
        &quot;clientScope&quot;: &quot;offline_access&quot;,
        &quot;roles&quot;: [
          &quot;offline_access&quot;
        ]
      }
    ],
    &quot;clients&quot;: [
      {
        &quot;clientId&quot;: &quot;mrdc-client&quot;,
        &quot;rootUrl&quot;: &quot;&quot;,
        &quot;baseUrl&quot;: &quot;&quot;,
        &quot;enabled&quot;: true,
        &quot;redirectUris&quot;: [
          &quot;https://*&quot;
        ],
        &quot;secret&quot;: &quot;secret&quot;,
        &quot;fullScopeAllowed&quot;: false,
        &quot;directAccessGrantsEnabled&quot;: true,
        &quot;authorizationServicesEnabled&quot;: true,
        &quot;authorizationSettings&quot;: {
          &quot;allowRemoteResourceManagement&quot;: false,
          &quot;policyEnforcementMode&quot;: &quot;ENFORCING&quot;,
          &quot;resources&quot;: [
            {
              &quot;name&quot;: &quot;Default Resource&quot;,
              &quot;uri&quot;: &quot;/*&quot;
            }
          ],
          &quot;policies&quot;: [
            {
              &quot;name&quot;: &quot;Default Policy&quot;,
              &quot;description&quot;: &quot;A policy that grants access only for users within this realm&quot;,
              &quot;type&quot;: &quot;js&quot;,
              &quot;logic&quot;: &quot;POSITIVE&quot;,
              &quot;decisionStrategy&quot;: &quot;AFFIRMATIVE&quot;,
              &quot;config&quot;: {
                &quot;code&quot;: &quot;// by default, grants any permission associated with this policy\n$evaluation.grant();\n&quot;
              }
            },
            {
              &quot;name&quot;: &quot;Only User Role Policy&quot;,
              &quot;type&quot;: &quot;role&quot;,
              &quot;logic&quot;: &quot;POSITIVE&quot;,
              &quot;decisionStrategy&quot;: &quot;UNANIMOUS&quot;,
              &quot;config&quot;: {
                &quot;roles&quot;: &quot;[{\&quot;id\&quot;:\&quot;user\&quot;,\&quot;required\&quot;:false}]&quot;
              }
            },
            {
              &quot;name&quot;: &quot;User Permission&quot;,
              &quot;type&quot;: &quot;resource&quot;,
              &quot;logic&quot;: &quot;POSITIVE&quot;,
              &quot;decisionStrategy&quot;: &quot;UNANIMOUS&quot;,
              &quot;config&quot;: {
                &quot;resources&quot;: &quot;[\&quot;Default Resource\&quot;]&quot;,
                &quot;applyPolicies&quot;: &quot;[\&quot;Only User Role Policy\&quot;]&quot;
              }
            }
          ],
          &quot;scopes&quot;: [
            &quot;view&quot;
          ]
        }
      }
    ]
  }
  </code></pre>