Commit 77c180612bdeecd24d38f79751997e5d79db09b8
1 parent
f130c6a2
Add check for principal not null in SecurityService
Showing
1 changed file
with
12 additions
and
3 deletions
Show diff stats
src/main/java/fr/plil/sio/web/mvc/SecurityServiceImpl.java
@@ -3,6 +3,7 @@ package fr.plil.sio.web.mvc; | @@ -3,6 +3,7 @@ package fr.plil.sio.web.mvc; | ||
3 | 3 | ||
4 | import org.slf4j.Logger; | 4 | import org.slf4j.Logger; |
5 | import org.slf4j.LoggerFactory; | 5 | import org.slf4j.LoggerFactory; |
6 | +import org.springframework.security.core.Authentication; | ||
6 | import org.springframework.security.core.context.SecurityContextHolder; | 7 | import org.springframework.security.core.context.SecurityContextHolder; |
7 | import org.springframework.security.core.userdetails.UserDetails; | 8 | import org.springframework.security.core.userdetails.UserDetails; |
8 | import org.springframework.stereotype.Service; | 9 | import org.springframework.stereotype.Service; |
@@ -14,10 +15,18 @@ public class SecurityServiceImpl implements SecurityService{ | @@ -14,10 +15,18 @@ public class SecurityServiceImpl implements SecurityService{ | ||
14 | 15 | ||
15 | @Override | 16 | @Override |
16 | public String findLoggedInUsername() { | 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 | logger.warn("cannot find logged user"); | 30 | logger.warn("cannot find logged user"); |
22 | return null; | 31 | return null; |
23 | } | 32 | } |