List of usage examples for org.springframework.security.core Authentication getPrincipal
Object getPrincipal();
From source file:org.jtalks.common.security.SecurityService.java
/** * Get current authenticated {@link User} username. * * @return current authenticated {@link User} username or {@code null} if there is no {@link User} authenticated * or if no authentication information is available (request not went through spring security filters). *///from w w w . j a va 2 s . c o m public String getCurrentUserUsername() { Authentication auth = securityContextFacade.getContext().getAuthentication(); if (auth == null) { return null; } Object principal = auth.getPrincipal(); String username = extractUsername(principal); if (isAnonymous(username)) { return null; } return username; }
From source file:br.com.semanticwot.cd.controllers.HomeController.java
@RequestMapping(method = RequestMethod.GET) public ModelAndView index(Authentication authentication) { ModelAndView modelAndView = new ModelAndView("index"); SwotApplication swotApplication = new SwotApplication(); try {/* w ww.j ava2 s . co m*/ swotApplication = swotApplicationDAO.findOne((SystemUser) authentication.getPrincipal()); } catch (EmptyResultDataAccessException ex) { } catch (Exception ex) { } modelAndView.addObject("swotapplication", swotApplication); modelAndView.addObject("gatewayform", new GatewayForm()); return modelAndView; }
From source file:de.theit.jenkins.crowd.CrowdAuthenticationManager.java
/** * {@inheritDoc}/*from ww w. ja va2 s. c o m*/ * * @see org.springframework.security.AuthenticationManager#authenticate(org.springframework.security.Authentication) */ @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String username = authentication.getPrincipal().toString(); // checking whether there's already a SSO token if (null == authentication.getCredentials() && authentication instanceof CrowdAuthenticationToken && null != ((CrowdAuthenticationToken) authentication).getSSOToken()) { // SSO token available => user already authenticated if (LOG.isLoggable(Level.FINER)) { LOG.finer("User '" + username + "' already authenticated"); } return authentication; } String password = authentication.getCredentials().toString(); // ensure that the group is available, active and that the user // is a member of it if (!this.configuration.isGroupMember(username)) { throw new InsufficientAuthenticationException( userNotValid(username, this.configuration.allowedGroupNames)); } String displayName = null; try { // authenticate user if (LOG.isLoggable(Level.FINE)) { LOG.fine("Authenticating user: " + username); } User user = this.configuration.crowdClient.authenticateUser(username, password); displayName = user.getDisplayName(); } catch (UserNotFoundException ex) { if (LOG.isLoggable(Level.INFO)) { LOG.info(userNotFound(username)); } throw new BadCredentialsException(userNotFound(username), ex); } catch (ExpiredCredentialException ex) { LOG.warning(expiredCredentials(username)); throw new CredentialsExpiredException(expiredCredentials(username), ex); } catch (InactiveAccountException ex) { LOG.warning(accountExpired(username)); throw new AccountExpiredException(accountExpired(username), ex); } catch (ApplicationPermissionException ex) { LOG.warning(applicationPermission()); throw new AuthenticationServiceException(applicationPermission(), ex); } catch (InvalidAuthenticationException ex) { LOG.warning(invalidAuthentication()); throw new AuthenticationServiceException(invalidAuthentication(), ex); } catch (OperationFailedException ex) { LOG.log(Level.SEVERE, operationFailed(), ex); throw new AuthenticationServiceException(operationFailed(), ex); } // user successfully authenticated // => retrieve the list of groups the user is a member of List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); // add the "authenticated" authority to the list of granted // authorities... authorities.add(SecurityRealm.AUTHENTICATED_AUTHORITY); // ..and finally all authorities retrieved from the Crowd server authorities.addAll(this.configuration.getAuthoritiesForUser(username)); // user successfully authenticated => create authentication token if (LOG.isLoggable(Level.FINE)) { LOG.fine("User successfully authenticated; creating authentication token"); } return new CrowdAuthenticationToken(username, password, authorities, null, displayName); }
From source file:com.che.software.testato.web.controller.SessionController.java
/** * Getter for the private field value sessionUser. * /*from w ww. ja v a 2 s . c om*/ * @return the sessionUser field value. */ public User getSessionUser() { if (null == sessionUser) { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); if (null != auth) { Object principal = auth.getPrincipal(); if (null != principal && principal instanceof UserDetails) { try { LOGGER.debug("Loading " + ((UserDetails) principal).getUsername() + " properties."); sessionUser = userManager .searchUsers(new UserSearch(((UserDetails) principal).getUsername())).get(0); sessionUser.setService(serviceManager.searchServiceFromUserId(sessionUser.getUserId())); } catch (UserSearchManagerException e) { LOGGER.error("Error during the recovery of the user's properties.", e); } catch (ServiceSearchManagerException e) { LOGGER.error("Error during the recovery of the user's service.", e); } } } } return sessionUser; }
From source file:org.jtalks.common.service.nontransactional.SecurityServiceImplTest.java
@Test public void testGetCurrentUserUsername() throws Exception { User user = getUser();//w w w .ja v a 2 s. com Authentication auth = mock(Authentication.class); when(auth.getPrincipal()).thenReturn(user); when(securityContext.getAuthentication()).thenReturn(auth); String username = securityService.getCurrentUserUsername(); assertEquals(username, USERNAME, "Username not equals"); verify(auth).getPrincipal(); verify(securityContext).getAuthentication(); }
From source file:org.jtalks.common.service.nontransactional.SecurityServiceImplTest.java
@Test public void testGetCurrentUserUsernamePrincipal() throws Exception { Principal user = new PrincipalImpl(USERNAME); Authentication auth = mock(Authentication.class); when(auth.getPrincipal()).thenReturn(user); when(securityContext.getAuthentication()).thenReturn(auth); String username = securityService.getCurrentUserUsername(); assertEquals(username, USERNAME, "Username not equals"); verify(auth).getPrincipal();//from w ww . j ava 2s. c om verify(securityContext).getAuthentication(); }
From source file:org.jtalks.common.service.nontransactional.SecurityServiceImplTest.java
@Test public void testGetCurrentUserUsernameAnonymousUser() throws Exception { Principal user = new PrincipalImpl(SecurityConstants.ANONYMOUS_USERNAME); Authentication auth = mock(Authentication.class); when(auth.getPrincipal()).thenReturn(user); when(securityContext.getAuthentication()).thenReturn(auth); String username = securityService.getCurrentUserUsername(); assertNull(username, "Username not null"); verify(auth).getPrincipal();//from w w w. j a va 2 s .c om verify(securityContext).getAuthentication(); }
From source file:org.meruvian.yama.service.DefaultSessionCredential.java
@Override public org.meruvian.yama.repository.user.User getCurrentUser() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null || !authentication.isAuthenticated()) { return null; }/*from ww w . j av a 2 s. c om*/ if (authentication.getPrincipal() instanceof DefaultUserDetails) { DefaultUserDetails user = (DefaultUserDetails) authentication.getPrincipal(); return user.getUser(); } return null; }
From source file:com.bisone.saiku.security.replace.SessionService.java
public Map<String, Object> login(HttpServletRequest req, String username, String password) throws Exception { if (authenticationManager != null) { authenticate(req, username, password); }/*from w w w. j ava2s. c o m*/ if (SecurityContextHolder.getContext() != null && SecurityContextHolder.getContext().getAuthentication() != null) { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); Object p = auth.getPrincipal(); createSession(auth, username, password); return sessionHolder.get(p); } return new HashMap<String, Object>(); }
From source file:com.bisone.saiku.security.replace.SessionService.java
public void clearSessions(HttpServletRequest req, String username, String password) throws Exception { if (authenticationManager != null) { authenticate(req, username, password); }/*from ww w . ja va 2 s .c om*/ if (SecurityContextHolder.getContext() != null && SecurityContextHolder.getContext().getAuthentication() != null) { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); Object p = auth.getPrincipal(); if (sessionHolder.containsKey(p)) { sessionHolder.remove(p); } } }