package com.juick.server.util; import com.juick.User; import com.juick.server.helpers.AnonymousUser; import com.juick.service.security.entities.JuickUser; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; /** * Created by aalexeev on 11/14/16. */ public class UserUtils { private UserUtils() { throw new IllegalStateException(); } 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 AnonymousUser.INSTANCE; } }