Blame view

src/main/java/fr/plil/sio/web/mvc/SecurityServiceImpl.java 976 Bytes
94a17877   jcartign   Add SecurityServi...
1
2
3
4
5
  package fr.plil.sio.web.mvc;
  
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
77c18061   jcartign   Add check for pri...
6
  import org.springframework.security.core.Authentication;
94a17877   jcartign   Add SecurityServi...
7
8
9
10
11
12
13
14
15
16
17
  import org.springframework.security.core.context.SecurityContextHolder;
  import org.springframework.security.core.userdetails.UserDetails;
  import org.springframework.stereotype.Service;
  
  @Service
  public class SecurityServiceImpl implements SecurityService{
  
      private static final Logger logger = LoggerFactory.getLogger(SecurityServiceImpl.class);
  
      @Override
      public String findLoggedInUsername() {
77c18061   jcartign   Add check for pri...
18
19
20
21
          Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  
          if(authentication == null) {
              return null;
94a17877   jcartign   Add SecurityServi...
22
          }
77c18061   jcartign   Add check for pri...
23
24
25
26
27
28
29
  
          Object principal = authentication.getPrincipal();
  
          if (principal instanceof UserDetails) {
              return ((UserDetails)principal).getUsername();
          }
  
19426ff3   jcartign   Fix SecurityServi...
30
          logger.warn("cannot find logged user");
94a17877   jcartign   Add SecurityServi...
31
32
33
          return null;
      }
  }