private boolean isAuthenticated() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null || authentication instanceof AnonymousAuthenticationToken) { return false; } return true; }
private String getCurrentUsername() { Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal(); if (principal instanceof UserDetails) { return ((UserDetails) principal).getUsername(); } if (principal instanceof Principal) { return ((Principal) principal).getName(); } return String.valueOf(principal); }
private String getUserName() { String username = SecurityContextHolder.getContext().getAuthentication().getName(); if (StringUtils.isEmpty(username)) { username = ""; } return username; }
@Test(expected = AccessDeniedException.class) public void protectedMethodIsIntercepted() throws Exception { SecurityContextHolder.getContext().setAuthentication(anne); secured.protectedMethod(); }
public void changePassword(String oldPassword, String newPassword) throws AuthenticationException { Authentication currentUser = SecurityContextHolder.getContext() .getAuthentication(); if (currentUser == null) { // This would indicate bad coding somewhere throw new AccessDeniedException( "Can't change password as no Authentication object found in context " + "for current user."); } String username = currentUser.getName(); // If an authentication manager has been set, re-authenticate the user with the // supplied password. if (authenticationManager != null) { logger.debug("Reauthenticating user '" + username + "' for password change request."); authenticationManager.authenticate(new UsernamePasswordAuthenticationToken( username, oldPassword)); } else { logger.debug("No authentication manager set. Password won't be re-checked."); } logger.debug("Changing password for user '" + username + "'"); getJdbcTemplate().update(changePasswordSql, newPassword, username); SecurityContextHolder.getContext().setAuthentication( createNewAuthentication(currentUser, newPassword)); userCache.removeUserFromCache(username); }
@Test public void afterSendCompletionNullAuthentication() throws Exception { interceptor.afterSendCompletion(messageBuilder.build(), channel, true, null); assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull(); }
@Test public void testNullCreatedById() throws Exception { Authentication authentication = mock(Authentication.class); when(authentication.getPrincipal()).thenReturn(null); SecurityContextHolder.getContext().setAuthentication(authentication); String client1 = "client1"; String client2 = "client2"; BaseClientDetails clientDetails = new BaseClientDetails(); clientDetails.setClientId(client1); service.addClientDetails(clientDetails); assertNull(service.getCreatedByForClientAndZone(client1, OriginKeys.UAA)); authenticateAsClient(); clientDetails = new BaseClientDetails(); clientDetails.setClientId(client2); service.addClientDetails(clientDetails); assertNull(service.getCreatedByForClientAndZone(client2, OriginKeys.UAA)); }
protected AdminUser getPersistentAdminUser() { SecurityContext ctx = SecurityContextHolder.getContext(); if (ctx != null) { Authentication auth = ctx.getAuthentication(); if (auth != null && !auth.getName().equals(ANONYMOUS_USER_NAME)) { UserDetails temp = (UserDetails) auth.getPrincipal(); return adminSecurityService.readAdminUserByUserName(temp.getUsername()); } } return null; }
@RequestMapping(value = "/authenticate", method = RequestMethod.POST) public TokenTransfer authenticate(@RequestParam("username") String username, @RequestParam("password") String password) { UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password); Authentication authentication = authManager.authenticate(authenticationToken); SecurityContextHolder.getContext().setAuthentication(authentication); UserDetails userDetails = userDetailsService.loadUserByUsername(username); return new TokenTransfer(TokenUtils.createToken(userDetails)); }
@Test public void postFilterIsApplied() throws Exception { configureForElAnnotations(); SecurityContextHolder.getContext().setAuthentication(anne); List<String> objects = prePostSecured.postFilterMethod(); assertThat(objects).hasSize(2); assertThat(objects.contains("apple")).isTrue(); assertThat(objects.contains("aubergine")).isTrue(); }
@Test public void afterSendCompletion() throws Exception { SecurityContextHolder.getContext().setAuthentication(authentication); interceptor.afterSendCompletion(messageBuilder.build(), channel, true, null); assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull(); }
@Test public void afterMessageHandledUserNotSet() throws Exception { interceptor.afterMessageHandled(messageBuilder.build(), channel, handler, null); assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull(); }
String getCurrentUserName() { return SecurityContextHolder.getContext().getAuthentication().getName(); }
@Override public UmsMember getCurrentMember() { SecurityContext ctx = SecurityContextHolder.getContext(); Authentication auth = ctx.getAuthentication(); MemberDetails memberDetails = (MemberDetails) auth.getPrincipal(); return memberDetails.getUmsMember(); }
private Authentication getAuthentication() { if (this.authentication != null) { return this.authentication; } SecurityContext context = SecurityContextHolder.getContext(); return context.getAuthentication(); } }
protected AdminUser getPersistentAdminUser() { SecurityContext ctx = SecurityContextHolder.getContext(); if (ctx != null) { Authentication auth = ctx.getAuthentication(); if (auth != null && !auth.getName().equals(ANONYMOUS_USER_NAME)) { UserDetails temp = (UserDetails) auth.getPrincipal(); return securityService.readAdminUserByUserName(temp.getUsername()); } } return null; }
@Test public void targetShouldAllowProtectedMethodInvocationWithCorrectRole() { UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( "Test", "Password", AuthorityUtils.createAuthorityList("ROLE_USER")); SecurityContextHolder.getContext().setAuthentication(token); target.doSomething(); }
@Test public void afterMessageHandled() throws Exception { SecurityContextHolder.getContext().setAuthentication(authentication); interceptor.afterMessageHandled(messageBuilder.build(), channel, handler, null); assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull(); }
@Test public void preSendUserSet() throws Exception { messageBuilder.setHeader(SimpMessageHeaderAccessor.USER_HEADER, authentication); interceptor.preSend(messageBuilder.build(), channel); assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs( authentication); }
@Override public String getUserName() { Authentication a = SecurityContextHolder.getContext().getAuthentication(); return a == null ? null : a.getName(); }