Commit 77c180612bdeecd24d38f79751997e5d79db09b8

Authored by jcartign
1 parent f130c6a2

Add check for principal not null in SecurityService

src/main/java/fr/plil/sio/web/mvc/SecurityServiceImpl.java
... ... @@ -3,6 +3,7 @@ package fr.plil.sio.web.mvc;
3 3  
4 4 import org.slf4j.Logger;
5 5 import org.slf4j.LoggerFactory;
  6 +import org.springframework.security.core.Authentication;
6 7 import org.springframework.security.core.context.SecurityContextHolder;
7 8 import org.springframework.security.core.userdetails.UserDetails;
8 9 import org.springframework.stereotype.Service;
... ... @@ -14,10 +15,18 @@ public class SecurityServiceImpl implements SecurityService{
14 15  
15 16 @Override
16 17 public String findLoggedInUsername() {
17   - Object userDetails = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
18   - if (userDetails instanceof UserDetails) {
19   - return ((UserDetails)userDetails).getUsername();
  18 + Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  19 +
  20 + if(authentication == null) {
  21 + return null;
20 22 }
  23 +
  24 + Object principal = authentication.getPrincipal();
  25 +
  26 + if (principal instanceof UserDetails) {
  27 + return ((UserDetails)principal).getUsername();
  28 + }
  29 +
21 30 logger.warn("cannot find logged user");
22 31 return null;
23 32 }
... ...