@Test public void testFull() throws Exception { UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( "user", "password", AuthorityUtils.createAuthorityList("ROLE_ONE")); assertThat(jaasProvider.supports(UsernamePasswordAuthenticationToken.class)).isTrue(); Authentication auth = jaasProvider.authenticate(token); assertThat(jaasProvider.getAuthorityGranters()).isNotNull(); assertThat(jaasProvider.getCallbackHandlers()).isNotNull(); assertThat(jaasProvider.getLoginConfig()).isNotNull(); assertThat(jaasProvider.getLoginContextName()).isNotNull(); Collection<? extends GrantedAuthority> list = auth.getAuthorities(); Set<String> set = AuthorityUtils.authorityListToSet(list); assertThat(set.contains("ROLE_ONE")).withFailMessage("GrantedAuthorities should not contain ROLE_ONE").isFalse(); assertThat(set.contains("ROLE_TEST1")).withFailMessage("GrantedAuthorities should contain ROLE_TEST1").isTrue(); assertThat(set.contains("ROLE_TEST2")).withFailMessage("GrantedAuthorities should contain ROLE_TEST2").isTrue(); boolean foundit = false; for (GrantedAuthority a : list) { if (a instanceof JaasGrantedAuthority) { JaasGrantedAuthority grant = (JaasGrantedAuthority) a; assertThat(grant.getPrincipal()).withFailMessage("Principal was null on JaasGrantedAuthority").isNotNull(); foundit = true; } } assertThat(foundit).as("Could not find a JaasGrantedAuthority").isTrue(); assertThat(eventCheck.successEvent).as("Success event should be fired").isNotNull(); assertThat(eventCheck.successEvent.getAuthentication()).withFailMessage("Auth objects should be equal").isEqualTo(auth); assertThat(eventCheck.failedEvent).as("Failure event should not be fired").isNull(); }
public static Set<Principal> getCurrentPrincipals() { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); Set<Principal> principals = new HashSet<>(); if (auth != null) { for (GrantedAuthority grant : auth.getAuthorities()) { if (grant instanceof JaasGrantedAuthority) { JaasGrantedAuthority jaasGrant = (JaasGrantedAuthority) grant; principals.add(jaasGrant.getPrincipal()); } else { String authority = grant.getAuthority(); if (authority != null) { principals.add(new SimplePrincipal(authority)); } } } principals.add(new UsernamePrincipal(auth.getName())); } else { principals.add(new AnonymousPrincipal()); } return principals; }
@Override public boolean hasRole(String roleName) { boolean matched = this.authentication.getAuthorities().stream().anyMatch(grant -> { if (grant instanceof JaasGrantedAuthority) { JaasGrantedAuthority jaasGrant = (JaasGrantedAuthority) grant; return JcrAccessControlUtil.matchesRole(jaasGrant.getPrincipal(), roleName); } else { if (roleName.equals(grant.getAuthority())) { return true; } else { return false; } } }); if (matched) { return true; } else { return this.principals.stream().anyMatch(principal -> JcrAccessControlUtil.matchesRole(principal, roleName)); } }
public static Set<Principal> getCurrentPrincipals() { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); Set<Principal> principals = new HashSet<>(); if (auth != null) { for (GrantedAuthority grant : auth.getAuthorities()) { if (grant instanceof JaasGrantedAuthority) { JaasGrantedAuthority jaasGrant = (JaasGrantedAuthority) grant; principals.add(jaasGrant.getPrincipal()); } else { String authority = grant.getAuthority(); if (authority != null) { principals.add(new SimplePrincipal(authority)); } } } principals.add(new UsernamePrincipal(auth.getName())); } else { principals.add(new AnonymousPrincipal()); } return principals; }