title: Spring Boot Data JPA @CreatedBy and @UpdatedBy not populating with authenticating with OIDC
body: <p>I would like to get Spring JPA auditing to work with Spring Boot,I am authenticating with Keycloak using the latest feature of Spring Security.</p>
  
  <pre><code>springBootVersion = '2.1.0.RC1'
  </code></pre>
  
  <p>I am following the sample by spring security team <a href="https://github.com/jzheaux/messaging-app/tree/springone2018-demo/resource-server" rel="noreferrer">https://github.com/jzheaux/messaging-app/tree/springone2018-demo/resource-server</a></p>
  
  <p>ResourceServerConfig.kt</p>
  
  <pre><code>@EnableWebSecurity
  class OAuth2ResourceServerSecurityConfiguration(val resourceServerProperties: OAuth2ResourceServerProperties) : WebSecurityConfigurerAdapter() {
  
      @Throws(Exception::class)
      override fun configure(http: HttpSecurity) {
          http
                  .authorizeRequests()
                  .antMatchers("/api/**").authenticated()
                  .anyRequest().anonymous()
                  .and()
                  .oauth2ResourceServer()
                  .authenticationEntryPoint(MoreInformativeAuthenticationEntryPoint())
                  .jwt()
                  .jwtAuthenticationConverter(GrantedAuthoritiesExtractor())
                  .decoder(jwtDecoder())
  
      }
  
      private fun jwtDecoder(): JwtDecoder {
          val issuerUri = this.resourceServerProperties.jwt.issuerUri
  
          val jwtDecoder = JwtDecoders.fromOidcIssuerLocation(issuerUri) as NimbusJwtDecoderJwkSupport
  
          val withIssuer = JwtValidators.createDefaultWithIssuer(issuerUri)
          val withAudience = DelegatingOAuth2TokenValidator(withIssuer, AudienceValidator())
          jwtDecoder.setJwtValidator(withAudience)
  
          return jwtDecoder
      }
  }
  
  class MoreInformativeAuthenticationEntryPoint : AuthenticationEntryPoint {
      private val delegate = BearerTokenAuthenticationEntryPoint()
  
      private val mapper = ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL)
  
      @Throws(IOException::class, ServletException::class)
      override fun commence(request: HttpServletRequest, response: HttpServletResponse,
                            reason: AuthenticationException) {
  
          this.delegate.commence(request, response, reason)
  
          if (reason.cause is JwtValidationException) {
              val validationException = reason.cause as JwtValidationException
              val errors = validationException.errors
              this.mapper.writeValue(response.writer, errors)
          }
      }
  }
  
  class GrantedAuthoritiesExtractor : JwtAuthenticationConverter() {
      override fun extractAuthorities(jwt: Jwt): Collection&lt;GrantedAuthority&gt; {
          val scopes = jwt.claims["scope"].toString().split(" ")
          return scopes.map { SimpleGrantedAuthority(it) }
      }
  }
  
  class AudienceValidator : OAuth2TokenValidator&lt;Jwt&gt; {
  
      override fun validate(token: Jwt): OAuth2TokenValidatorResult {
          val audience = token.audience
          return if (!CollectionUtils.isEmpty(audience) &amp;&amp; audience.contains("mobile-client")) {
              OAuth2TokenValidatorResult.success()
          } else {
              OAuth2TokenValidatorResult.failure(MISSING_AUDIENCE)
          }
      }
  
      companion object {
          private val MISSING_AUDIENCE = BearerTokenError("invalid_token", HttpStatus.UNAUTHORIZED,
                  "The token is missing a required audience.", null)
      }
  }
  </code></pre>
  
  <p>application.yaml</p>
  
  <pre><code>spring:
    application:
      name: sociter
    datasource:
      url: jdbc:postgresql://localhost:5432/sociter
      username: postgres
      password: 123123
      driver-class-name: org.postgresql.Driver
    jpa:
      hibernate:
        ddl-auto: update
    security:
      oauth2:
        resourceserver:
          jwt:
            jwk-set-uri: http://localhost:8080/auth/realms/sociter/protocol/openid-connect/certs
            issuer-uri: http://localhost:8080/auth/realms/sociter
  </code></pre>
  
  <p>JpaAuditingConfiguration.kt</p>
  
  <pre><code>@Configuration
  @EnableJpaAuditing
  (auditorAwareRef = "auditorProvider")
  class JpaAuditingConfiguration {
  
      @Bean
      fun auditorProvider(): AuditorAware&lt;String&gt; {
          return if (SecurityContextHolder.getContext().authentication != null) {
              val oauth2 = SecurityContextHolder.getContext().authentication as JwtAuthenticationToken
          val claims = oauth2.token.claims
          val userId = claims["sub"]
          AuditorAware { Optional.of(userId.toString()) }
          } else
              AuditorAware { Optional.of("Unknown") }
      }
  }
  </code></pre>
  
  <p>BaseEntity.kt</p>
  
  <pre><code>@MappedSuperclass
  @JsonIgnoreProperties(value = ["createdOn, updatedOn"], allowGetters = true)
  @EntityListeners(AuditingEntityListener::class)
  abstract class BaseEntity {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      val id: UUID = UUID.randomUUID()
  
      @Column(nullable = false, updatable = false)
      @CreatedDate
      var createdOn: LocalDateTime = LocalDateTime.now()
  
      @Column(nullable = true)
      @LastModifiedDate
      var updatedOn: LocalDateTime? = null
  
      @Column(nullable = true, updatable = false)
      @CreatedBy
      var createdBy: String? = null
  
      @Column(nullable = true)
      @LastModifiedBy
      var updatedBy: String? = null
  }
  </code></pre>
  
  <p>I am getting createdBy and UpdatedBy set to as Unknown. During debugging, auditorProvider bean get invoked and sets the user to Unknown but when passing access_token, if condition is still false.</p>
  
  <p>Not sure what I am missing.</p>