@Override public List<GrantedAuthority> extractAuthorities(Map<String, Object> map) { String authorities = "ROLE_USER"; if (map.containsKey(AUTHORITIES)) { authorities = asAuthorities(map.get(AUTHORITIES)); } return AuthorityUtils.commaSeparatedStringToAuthorityList(authorities); }
private String asAuthorities(Object object) { List<Object> authorities = new ArrayList<>(); if (object instanceof Collection) { Collection<?> collection = (Collection<?>) object; object = collection.toArray(new Object[0]); } if (ObjectUtils.isArray(object)) { Object[] array = (Object[]) object; for (Object value : array) { if (value instanceof String) { authorities.add(value); } else if (value instanceof Map) { authorities.add(asAuthority((Map<?, ?>) value)); } else { authorities.add(value); } } return StringUtils.collectionToCommaDelimitedString(authorities); } return object.toString(); }
@Test public void authoritiesArray() { this.map.put("authorities", new String[] { "ROLE_USER", "ROLE_ADMIN" }); assertThat(this.extractor.extractAuthorities(this.map).toString()) .isEqualTo("[ROLE_USER, ROLE_ADMIN]"); }
@Test public void authoritiesAsListOfMaps() { this.map.put("authorities", Arrays.asList(Collections.singletonMap("authority", "ROLE_ADMIN"))); assertThat(this.extractor.extractAuthorities(this.map).toString()) .isEqualTo("[ROLE_ADMIN]"); }
@Test public void authoritiesAsListOfMapsWithStandardKey() { Map<String, String> map = new LinkedHashMap<>(); map.put("role", "ROLE_ADMIN"); map.put("extra", "value"); this.map.put("authorities", Arrays.asList(map)); assertThat(this.extractor.extractAuthorities(this.map).toString()) .isEqualTo("[ROLE_ADMIN]"); }
@Override public List<GrantedAuthority> extractAuthorities(Map<String, Object> map) { String authorities = "ROLE_USER"; if (map.containsKey(AUTHORITIES)) { authorities = asAuthorities(map.get(AUTHORITIES)); } return AuthorityUtils.commaSeparatedStringToAuthorityList(authorities); }
private String asAuthorities(Object object) { List<Object> authorities = new ArrayList<>(); if (object instanceof Collection) { Collection<?> collection = (Collection<?>) object; object = collection.toArray(new Object[0]); } if (ObjectUtils.isArray(object)) { Object[] array = (Object[]) object; for (Object value : array) { if (value instanceof String) { authorities.add(value); } else if (value instanceof Map) { authorities.add(asAuthority((Map<?, ?>) value)); } else { authorities.add(value); } } return StringUtils.collectionToCommaDelimitedString(authorities); } return object.toString(); }
@Test public void authoritiesAsListOfMapsWithMultipleNonStandardKeys() { Map<String, String> map = new HashMap<>(); map.put("any", "ROLE_ADMIN"); map.put("foo", "bar"); this.map.put("authorities", Arrays.asList(map)); assertThat(this.extractor.extractAuthorities(this.map).toString()) .isEqualTo("[{foo=bar, any=ROLE_ADMIN}]"); }
@Test public void authorities() { this.map.put("authorities", "ROLE_ADMIN"); assertThat(this.extractor.extractAuthorities(this.map).toString()) .isEqualTo("[ROLE_ADMIN]"); }
@Test public void authoritiesCommaSeparated() { this.map.put("authorities", "ROLE_USER,ROLE_ADMIN"); assertThat(this.extractor.extractAuthorities(this.map).toString()) .isEqualTo("[ROLE_USER, ROLE_ADMIN]"); }
@Test public void authoritiesList() { this.map.put("authorities", Arrays.asList("ROLE_USER", "ROLE_ADMIN")); assertThat(this.extractor.extractAuthorities(this.map).toString()) .isEqualTo("[ROLE_USER, ROLE_ADMIN]"); }
@Test public void authoritiesAsListOfMapsWithNonStandardKey() { this.map.put("authorities", Arrays.asList(Collections.singletonMap("any", "ROLE_ADMIN"))); assertThat(this.extractor.extractAuthorities(this.map).toString()) .isEqualTo("[ROLE_ADMIN]"); }