Commit 94a178776730815343ea34f5f45572bc77c8f6d4

Authored by jcartign
1 parent f9b4ae49

Add SecurityService to get username of logged user

src/main/java/fr/plil/sio/web/mvc/SecurityService.java 0 → 100644
@@ -0,0 +1,6 @@ @@ -0,0 +1,6 @@
  1 +package fr.plil.sio.web.mvc;
  2 +
  3 +public interface SecurityService {
  4 +
  5 + String findLoggedInUsername();
  6 +}
0 \ No newline at end of file 7 \ No newline at end of file
src/main/java/fr/plil/sio/web/mvc/SecurityServiceImpl.java 0 → 100644
@@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
  1 +package fr.plil.sio.web.mvc;
  2 +
  3 +
  4 +import org.slf4j.Logger;
  5 +import org.slf4j.LoggerFactory;
  6 +import org.springframework.security.core.context.SecurityContextHolder;
  7 +import org.springframework.security.core.userdetails.UserDetails;
  8 +import org.springframework.stereotype.Service;
  9 +
  10 +@Service
  11 +public class SecurityServiceImpl implements SecurityService{
  12 +
  13 + private static final Logger logger = LoggerFactory.getLogger(SecurityServiceImpl.class);
  14 +
  15 + @Override
  16 + public String findLoggedInUsername() {
  17 + Object userDetails = SecurityContextHolder.getContext().getAuthentication().getDetails();
  18 + if (userDetails instanceof UserDetails) {
  19 + return ((UserDetails)userDetails).getUsername();
  20 + }
  21 + return null;
  22 + }
  23 +}
0 \ No newline at end of file 24 \ No newline at end of file