List of usage examples for org.springframework.security.core Authentication getPrincipal
Object getPrincipal();
From source file:org.zkoss.spring.security.SecurityUtil.java
/** * Return the evaluated result per the given property of the current * Authentication object./*ww w .j ava 2 s .c o m*/ * * @param property Property of the Authentication object which would be * evaluated. Supports nested properties. For example if the principal * object is an instance of UserDetails, the property "principal.username" * will return the username. Alternatively, using "name" will call getName * method on the Authentication object directly. * * @return the evaluated result of the current Authentication object per the * given property. */ public static Object getAuthentication(String property) { // determine the value by... if (property != null) { if ((SecurityContextHolder.getContext() == null) || !(SecurityContextHolder.getContext() instanceof SecurityContext) || (SecurityContextHolder.getContext().getAuthentication() == null)) { return null; } Authentication auth = SecurityContextHolder.getContext().getAuthentication(); if (auth.getPrincipal() == null) { return null; } try { BeanWrapperImpl wrapper = new BeanWrapperImpl(auth); return wrapper.getPropertyValue(property); } catch (BeansException e) { throw new UiException(e); } } return null; }
From source file:org.zkoss.spring.security.SecurityUtil.java
/** * Return the current Authentication object. *//*from ww w. j a va2 s. c o m*/ public static Authentication getAuthentication() { if ((SecurityContextHolder.getContext() != null) && (SecurityContextHolder.getContext() instanceof SecurityContext)) { final Authentication auth = SecurityContextHolder.getContext().getAuthentication(); if (auth != null && auth.getPrincipal() != null) { return auth; } } return null; }
From source file:org.jamwiki.authentication.WikiUserDetailsImpl.java
/** * Utility method for converting a Spring Security <code>Authentication</code> * object into a <code>WikiUserDetailsImpl</code>. If the user is logged-in then the * <code>Authentication</code> object will have the <code>WikiUserDetailsImpl</code> * as its principal. If the user is not logged in then create an empty * <code>WikiUserDetailsImpl</code> object and assign it the same authorities as the * <code>Authentication</code> object. * * @param auth The Spring Security <code>Authentication</code> object that is being * converted into a <code>WikiUserDetailsImpl</code> object. * @return Returns a <code>WikiUserDetailsImpl</code> object that corresponds to the * Spring Security <code>Authentication</code> object. If the user is not currently * logged-in then an empty <code>WikiUserDetailsImpl</code> with the same authorities * as the <code>Authentication</code> object is returned. This method * will never return <code>null</code>. * @throws AuthenticationCredentialsNotFoundException If authentication * credentials are unavailable.//from w w w . java2 s . c o m */ public static WikiUserDetailsImpl initWikiUserDetailsImpl(Authentication auth) throws AuthenticationCredentialsNotFoundException { if (auth == null) { throw new AuthenticationCredentialsNotFoundException("No authentication credential available"); } if (auth instanceof AnonymousAuthenticationToken || !(auth.getPrincipal() instanceof UserDetails)) { // anonymous user return new WikiUserDetailsImpl(ANONYMOUS_USER_USERNAME, "", true, true, true, true, auth.getAuthorities()); } // logged-in (or remembered) user if (auth.getPrincipal() instanceof WikiUserDetailsImpl) { return (WikiUserDetailsImpl) auth.getPrincipal(); } return new WikiUserDetailsImpl((UserDetails) auth.getPrincipal()); }
From source file:com.evolveum.midpoint.security.api.SecurityUtil.java
/** * Returns short description of the subject suitable for log * and error messages./* w w w . j a v a 2 s . c om*/ * Does not throw errors. Safe to toString-like methods. * May return null (means anonymous or unknown) */ public static String getSubjectDescription() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null) { return null; } Object principalObject = authentication.getPrincipal(); if (principalObject == null) { return null; } if (!(principalObject instanceof MidPointPrincipal)) { return principalObject.toString(); } return ((MidPointPrincipal) principalObject).getUsername(); }
From source file:security.LoginService.java
public static UserAccount getPrincipal() { UserAccount result;//from ww w. j a va 2 s. c o m SecurityContext context; Authentication authentication; Object principal; // If the asserts in this method fail, then you're // likely to have your Tomcat's working directory // corrupt. Please, clear your browser's cache, stop // Tomcat, update your Maven's project configuration, // clean your project, clean Tomcat's working directory, // republish your project, and start it over. context = SecurityContextHolder.getContext(); Assert.notNull(context); authentication = context.getAuthentication(); Assert.notNull(authentication); principal = authentication.getPrincipal(); Assert.isTrue(principal instanceof UserAccount); result = (UserAccount) principal; Assert.notNull(result); Assert.isTrue(result.getId() != 0); return result; }
From source file:com.evolveum.midpoint.security.api.SecurityUtil.java
/** * Returns principal representing currently logged-in user. Returns null if the user is anonymous. *//*ww w . j av a2 s. c o m*/ public static MidPointPrincipal getPrincipal() throws SecurityViolationException { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null) { SecurityViolationException ex = new SecurityViolationException("No authentication"); LOGGER.error("No authentication", ex); throw ex; } Object principalObject = authentication.getPrincipal(); if (!(principalObject instanceof MidPointPrincipal)) { if (authentication.getPrincipal() instanceof String && "anonymousUser".equals(principalObject)) { return null; } else { throw new IllegalArgumentException("Expected that spring security principal will be of type " + MidPointPrincipal.class.getName() + " but it was " + MiscUtil.getObjectName(principalObject)); } } return (MidPointPrincipal) principalObject; }
From source file:com.dickthedeployer.dick.web.controller.UserController.java
@RequestMapping("/api/user") public Object user(Authentication user) { return user.getPrincipal(); }
From source file:org.taverna.server.master.utils.UsernamePrincipal.java
public UsernamePrincipal(Authentication auth) { this(auth.getPrincipal()); }
From source file:org.jtalks.common.web.util.SuccessfulAuthenticationHandlerTest.java
@Test public void testOnAuthenticationSuccess() throws Exception { User user = new User("username", "email", "password"); Authentication auth = mock(Authentication.class); when(auth.getPrincipal()).thenReturn(user); handler.onAuthenticationSuccess(new MockHttpServletRequest(), new MockHttpServletResponse(), auth); verify(userService).updateLastLoginTime(user); }
From source file:waffle.spring.boot.demo.DemoController.java
@RequestMapping public String demo(Authentication auth) { return String.format("Hello, %s. You have authorities: %s", auth.getPrincipal(), auth.getAuthorities().stream().map(a -> a.getAuthority()).collect(Collectors.joining(", "))); }