RealmOAuth2ExpressionUtils extends OAuth2ExpressionUtils { public static boolean hasAnyRealm(Authentication authentication, String[] realms) { if (authentication instanceof OAuth2Authentication) { Object details = ((OAuth2Authentication) authentication).getUserAuthentication().getDetails(); if (details instanceof Map) { Map<String, Object> map = (Map<String, Object>) details; Object realmFromMap = map.get("realm"); if (realmFromMap != null && realmFromMap instanceof String) { String realm = (String) realmFromMap; for (String s : realms) { if (realm.equals(s)) { return true; } } } } } return false; }  static boolean hasAnyRealm(Authentication authentication, String[] realms);  }
@Test public void testHasKaiserRealm() { OAuth2Authentication authentication = tokenInfoService.loadAuthentication("123456789"); boolean result = RealmOAuth2ExpressionUtils.hasAnyRealm(authentication, new String[] { "kaiser" }); Assertions.assertThat(result).isFalse(); }
@Test public void testHasCustomRealm() { OAuth2Authentication authentication = tokenInfoService.loadAuthentication("123456789"); boolean result = RealmOAuth2ExpressionUtils.hasAnyRealm(authentication, new String[] { "customrealm" }); Assertions.assertThat(result).isFalse(); }
@Test public void testHasCustomRealmWithSlash() { OAuth2Authentication authentication = tokenInfoService.loadAuthentication("123456789"); boolean result = RealmOAuth2ExpressionUtils.hasAnyRealm(authentication, new String[] { "/customrealm" }); Assertions.assertThat(result).isTrue(); }
@Test public void testHasCustomRealmWithMulti() { OAuth2Authentication authentication = tokenInfoService.loadAuthentication("123456789"); boolean result = RealmOAuth2ExpressionUtils.hasAnyRealm(authentication, new String[] { "/customrealm", "kaiser", "yourFancyRealm" }); Assertions.assertThat(result).isTrue(); }