nkengbeza
nkengbeza

Reputation: 365

How to get an Authenticated User's Details in Spring Security OAuth2

I am unable to extract the current logged in user in Spring Security OAuth2. My goal is to extract the user when the create event on ClientSuggestion entity is triggered and persist it to the database.

Employee.java

@Entity
@Table(name = "er_employee")
public class Employee implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "username", unique = true)
    @NotNull
    @Size(max = 10)
    private String username;

    @Column(name = "password_hash")
    @NotNull
    @Size(min = 8, max = 512)
    private String password;


    @Column(name = "email_verification_token")
    @Size(max = 512)
    private String emailVerificationToken;

    @Column(name = "password_reset_token")
    @Size(max = 512)
    private String passwordResetToken;

    @Column(name = "active")
    @NotNull
    private boolean active;

    @Column(name = "is_deleted")
    @NotNull
    private boolean deleted;

    @Column(name = "date_of_creation")
    @Temporal(TemporalType.TIMESTAMP)
    @NotNull
    private Date dateOfCreation;

    @OneToMany(mappedBy = "employee")
    private List<ClientSuggestion> clientSuggestions;


    //Constructors
    //Getters ans setters
}

ClientSuggestion.java

@Entity
@Table(name = "er_suggestion")
public class ClientSuggestion implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "content", unique = true)
    @NotNull
    @Size(max = 200)
    private String suggestion;

    @ManyToOne
    @JoinColumn(name = "employee_id")
    private Employee employee;

    //Constructors
    //Getters ans setters
}

EmployeeRepository.java

public interface EmployeeRepository extends CrudRepository<Employee, Long> {

ClientSuggestionRepository .java

public interface ClientSuggestionRepository extends CrudRepository<ClientSuggestion, Long> {
}

The event handler

@Component
@RepositoryEventHandler(ClientSuggestion.class)
public class ClientSuggestionEventHandler {
    Employee employee= (Employee ) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

    @HandleBeforeCreate
    public void handleClientSuggestionBeforeCreate(ClientSuggestion cs) {
       cs.setDeleted(false);
       cs.setActive(true);
       cs.setPasswordResetToken(Encryptor.generateHash(cs.getPassword, 512));
       cs.setEmployee(employee);
    }
}

The bean, ClientSuggestionEventHandler, is registered in a configuration class. When I tried running the project, NullPointerException exception is thrown. I wish to find out how to get the current logged employee. I'm new to Spring Security OAuth2. Thanks.

Upvotes: 0

Views: 627

Answers (1)

hrdkisback
hrdkisback

Reputation: 908

In Employee.java implement org.springframework.security.core.userdetails.UserDetails class

Employee.java

@Entity
@Table(name = "er_employee")
public class Employee implements Serializable, UserDetails {

And then use Employee employee= (Employee) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

Upvotes: 1

Related Questions