Commit 94a178776730815343ea34f5f45572bc77c8f6d4
1 parent
f9b4ae49
Add SecurityService to get username of logged user
Showing
2 changed files
with
29 additions
and
0 deletions
Show diff stats
src/main/java/fr/plil/sio/web/mvc/SecurityService.java
0 → 100644
src/main/java/fr/plil/sio/web/mvc/SecurityServiceImpl.java
0 → 100644
... | ... | @@ -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 | 24 | \ No newline at end of file | ... | ... |