authorities.add(new JaasGrantedAuthority(role, principal));
@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(); }
authorities.add(new JaasGrantedAuthority(role, principal));
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; }
public OverrideCredentials(Principal userPrincipal, Set<Principal> rolePrincipals) { super(); this.userPrincipal = userPrincipal; this.rolePrincipals = Collections.unmodifiableSet(new HashSet<>(rolePrincipals)); Collection<? extends GrantedAuthority> authorities = Stream.concat(Stream.of(this.userPrincipal), this.rolePrincipals.stream()) .map(p -> new JaasGrantedAuthority(p.getName(), p)) .collect(Collectors.toSet()); this.authentication = new UsernameAuthenticationToken(userPrincipal, authorities); }
@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)); } }
@Override public SecurityContext createSecurityContext(WithMockJaasUser withUser) { String username = StringUtils.hasLength(withUser.username()) ? withUser.username() : withUser.value(); List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>(); for (String authority : withUser.authorities()) { grantedAuthorities.add(new JaasGrantedAuthority(authority, new GroupPrincipal(authority))); } User principal = new User(username, withUser.password(), true, true, true, true, grantedAuthorities); Authentication authentication = new UsernamePasswordAuthenticationToken(principal, principal.getPassword(), principal.getAuthorities()); SecurityContext context = SecurityContextHolder.createEmptyContext(); context.setAuthentication(authentication); return context; }
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; }
authorities.add(new JaasGrantedAuthority(role, principal));
authorities.add(new JaasGrantedAuthority(role, principal));