-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding component to fetch authenticated user-id
- Loading branch information
1 parent
7b423c3
commit 9f7ff42
Showing
2 changed files
with
25 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/com/behl/flare/utility/AuthenticatedUserIdProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.behl.flare.utility; | ||
|
||
import java.util.Optional; | ||
|
||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class AuthenticatedUserIdProvider { | ||
|
||
public String getUserId() { | ||
return Optional.ofNullable(SecurityContextHolder.getContext().getAuthentication()) | ||
.map(Authentication::getPrincipal) | ||
.filter(String.class::isInstance) | ||
.map(String.class::cast) | ||
.orElseThrow(IllegalStateException::new); | ||
} | ||
|
||
} |