I have the same problem and my solution is:
Configuration Aware class in order to get user:
import java.security.Principal;
import java.util.Optional;
import javax.servlet.http.HttpServletRequest;
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
import org.keycloak.representations.AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.AuditorAware;
public class AuditorAwareConfig implements AuditorAware<String> {
@Autowired
private HttpServletRequest request;
@Override
public Optional<String> getCurrentAuditor() {
AccessToken accessToken = this.getKeycloakToken(request.getUserPrincipal());
String userName = accessToken.getPreferredUsername();
return Optional.ofNullable(userName);
}
private AccessToken getKeycloakToken(Principal principal) {
KeycloakAuthenticationToken keycloakAuthenticationToken = (KeycloakAuthenticationToken) principal;
return keycloakAuthenticationToken.getAccount().getKeycloakSecurityContext().getToken();
}
Class enabling JPA Auditing:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
public class AuditConfig {
@Bean
AuditorAware<String> auditorProvider() {
return new AuditorAwareConfig();
}
}
Auditable class:
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(
value = {"createdAt", "createdBy","updatedAt", "updatedBy"},
allowGetters = true
)
public abstract class Auditable {
@CreatedDate
@Column(name="AUD_CREATE_AT", nullable = false, updatable = false)
private Instant createdAt;
@CreatedBy
@Column(name="AUD_CREATE_BY", nullable = false, updatable = false)
private String createdBy;
@LastModifiedDate
@Column(name="AUD_UPDATE_AT",nullable = false)
private Instant updatedAt;
@LastModifiedBy
@Column(name="AUD_UPDATE_BY",nullable = false)
private String updatedBy;
//Getters & Setters
At entity class:
@Entity
@Table(name="FOO")
public class FooEntity extends Auditable implements Serializable
...
kp.getKeycloakSecurityContext().getToken()…
— (2) What type of token is that? (3) Is this a component in the same project your other question asks about? (4) Are the properties in the application.properties of this project, the same as in the application.properties of that other Keycloak question? (5) Do you appreciate how frustrating it is trying to help someone that's hesitant to answer questions? TIA – Galingalejava -jar /path/to/boot-app.jar…
on a remote machine. That users can still access the web app that way. I mention this because Spring Boot is designed to simplify things. I suspect that part of your problem is you may be over-complicating things by unnecessarily „deploying a WAR to Tomcat“. —„…once i fix keycloak with spring secured, this will work…“ — Is it a good idea to pile more components on top of a malconfigured system? – Galingaleapplication.properties
in your other question fromkeycloak.public-client=true
tokeycloak.public-client=false
as I proposed in my answer below. What's the reason for not also addingkeycloak.principal-attribute=preferred_username
— which I also proposed? I think that property is needed as much as (possibly more than) thekeycloak.public-client
property. If you don't mind me asking. TIA. – Galingale