aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main/java/com/juick/util
diff options
context:
space:
mode:
authorGravatar Alexander Alexeev2016-12-11 00:52:57 +0700
committerGravatar Vitaly Takmazov2016-12-11 18:24:37 +0300
commitac6c86ddd482721e7011dcb727e4099b8cdf84b1 (patch)
tree233ff85e261037b0949e4aa5105ca0dbcd22e9c4 /juick-server/src/main/java/com/juick/util
parent5ff660e5f3e73382781f854a0afc350d2ad6d626 (diff)
anonymous user support
Diffstat (limited to 'juick-server/src/main/java/com/juick/util')
-rw-r--r--juick-server/src/main/java/com/juick/util/UserUtils.java27
1 files changed, 24 insertions, 3 deletions
diff --git a/juick-server/src/main/java/com/juick/util/UserUtils.java b/juick-server/src/main/java/com/juick/util/UserUtils.java
index ba1bd1ab..ddeedbbd 100644
--- a/juick-server/src/main/java/com/juick/util/UserUtils.java
+++ b/juick-server/src/main/java/com/juick/util/UserUtils.java
@@ -1,6 +1,11 @@
package com.juick.util;
-import java.security.Principal;
+import com.juick.User;
+import com.juick.entity.AnonymUser;
+import com.juick.server.security.entities.JuickUser;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.context.SecurityContextHolder;
+
import java.util.Random;
/**
@@ -22,7 +27,23 @@ public class UserUtils {
return sb.toString();
}
- public static String getUsername(final Principal principal, final String defaultUsername) {
- return principal == null ? defaultUsername : principal.getName();
+ public static Authentication getAuthentication() {
+ return SecurityContextHolder.getContext().getAuthentication();
+ }
+
+ public static Object getPrincipal(final Authentication authentication) {
+ return authentication == null ? null : authentication.getPrincipal();
+ }
+
+ public static User getCurrentUser() {
+ Object principal = getPrincipal(getAuthentication());
+
+ if (principal instanceof JuickUser)
+ return ((JuickUser) principal).getUser();
+
+ if (principal instanceof User)
+ return (User) principal;
+
+ return AnonymUser.INSTANCE;
}
}