From adb42ed966ab10780a544c04afc4b01534f4b077 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 4 Apr 2016 22:36:38 -0700 Subject: [PATCH] Polish --- .../resource/FixedAuthoritiesExtractor.java | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/FixedAuthoritiesExtractor.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/FixedAuthoritiesExtractor.java index a7ea1ddf16..52eb830fc1 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/FixedAuthoritiesExtractor.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/FixedAuthoritiesExtractor.java @@ -38,6 +38,8 @@ public class FixedAuthoritiesExtractor implements AuthoritiesExtractor { private static final String AUTHORITIES = "authorities"; + private static final String[] AUTHORITY_KEYS = { "authority", "role", "value" }; + @Override public List extractAuthorities(Map map) { String authorities = "ROLE_USER"; @@ -60,22 +62,7 @@ public class FixedAuthoritiesExtractor implements AuthoritiesExtractor { authorities.add(value); } else if (value instanceof Map) { - Map map = (Map) value; - if (map.size() == 1) { - authorities.add(map.values().iterator().next()); - } - else if (map.containsKey("authority")) { - authorities.add(map.get("authority")); - } - else if (map.containsKey("role")) { - authorities.add(map.get("role")); - } - else if (map.containsKey("value")) { - authorities.add(map.get("value")); - } - else { - authorities.add(map); - } + authorities.add(asAuthority((Map) value)); } else { authorities.add(value); @@ -86,4 +73,16 @@ public class FixedAuthoritiesExtractor implements AuthoritiesExtractor { return object.toString(); } + private Object asAuthority(Map map) { + if (map.size() == 1) { + return map.values().iterator().next(); + } + for (String key : AUTHORITY_KEYS) { + if (map.containsKey(key)) { + return map.get(map); + } + } + return map; + } + }