#java #spring-boot #oauth-2.0 #keycloak
Вопрос:
Я создаю простое приложение SpringBoot и пытаюсь интегрировать его с ключом поставщика OAuth 2.0. Я создал область, клиента, роли (Участник, участник) на уровне области и, наконец, создал пользователей и назначил роли (Участник, участник).
Если я использую адаптер SpringBoot, предоставляемый Keycloak https://www.keycloak.org/docs/latest/securing_apps/index.html#_spring_boot_adapter затем, когда я успешно войду в систему и проверю полномочия пользователя loggedin, я смогу увидеть назначенные роли, такие как Участник, участник.
Collection<? extends GrantedAuthority> authorities =
SecurityContextHolder.getContext().getAuthentication().getAuthorities();
Но если я использую общую конфигурацию клиента SpringBoot Auth2, я могу войти в систему,но когда я проверяю полномочия,он всегда показывает только ROLE_USER, SCOPE_email, SCOPE_openid, SCOPE_profile и не включает роли, которые я сопоставил (Участник, участник).
Моя конфигурация SpringBoot OAuth2:
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
применение.свойства
spring.security.oauth2.client.provider.spring-boot-thymeleaf-client.issuer-uri=http://localhost:8181/auth/realms/myrealm
spring.security.oauth2.client.registration.spring-boot-thymeleaf-client.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.spring-boot-thymeleaf-client.client-id=spring-boot-app
spring.security.oauth2.client.registration.spring-boot-thymeleaf-client.client-secret=XXXXXXXXXXXXXX
spring.security.oauth2.client.registration.spring-boot-thymeleaf-client.scope=openid,profile,roles
spring.security.oauth2.client.registration.spring-boot-thymeleaf-client.redirect-uri=http://localhost:8080/login/oauth2/code/spring-boot-app
Я использую SpringBoot 2.5.5 и Keycloak 15.0.2.
Используя этот общий подход к конфигурации OAuth2.0 (без использования SpringBootAdapter Keycloak), есть ли способ получить назначенные роли?
Ответ №1:
По умолчанию Spring Security создает список GrantedAuthority
использования значений в утверждении scope
или scp
и SCOPE_
префикса.
Keycloak сохраняет роли области во вложенном утверждении realm_access.roles
. У вас есть два варианта извлечения ролей и сопоставления их со списком GrantedAuthority
.
Клиент OAuth2
Если ваше приложение настроено как клиент OAuth2, вы можете извлечь роли либо из маркера идентификатора, либо из конечной точки userInfo. Keycloak включает роли только в маркере доступа, поэтому вам необходимо изменить конфигурацию, чтобы включить их также либо в маркер идентификатора, либо в конечную точку userInfo (которую я использую в следующем примере). Вы можете сделать это из консоли администратора Keycloak, перейдя в Client Scopes > roles > Mappers > realm roles
Затем в конфигурации безопасности Spring определите a GrantedAuthoritiesMapper
, которая извлекает роли из конечной точки userInfo и сопоставляет их с GrantedAuthority
s. Здесь я расскажу, как должен выглядеть конкретный компонент. Полный пример доступен на моем GitHub: https://github.com/ThomasVitale/spring-security-examples/tree/main/oauth2/login-user-authorities
@Bean
public GrantedAuthoritiesMapper userAuthoritiesMapperForKeycloak() {
return authorities -> {
Set<GrantedAuthority> mappedAuthorities = new HashSet<>();
var authority = authorities.iterator().next();
boolean isOidc = authority instanceof OidcUserAuthority;
if (isOidc) {
var oidcUserAuthority = (OidcUserAuthority) authority;
var userInfo = oidcUserAuthority.getUserInfo();
if (userInfo.hasClaim("realm_access")) {
var realmAccess = userInfo.getClaimAsMap("realm_access");
var roles = (Collection<String>) realmAccess.get("roles");
mappedAuthorities.addAll(generateAuthoritiesFromClaim(roles));
}
} else {
var oauth2UserAuthority = (OAuth2UserAuthority) authority;
Map<String, Object> userAttributes = oauth2UserAuthority.getAttributes();
if (userAttributes.containsKey("realm_access")) {
var realmAccess = (Map<String,Object>) userAttributes.get("realm_access");
var roles = (Collection<String>) realmAccess.get("roles");
mappedAuthorities.addAll(generateAuthoritiesFromClaim(roles));
}
}
return mappedAuthorities;
};
}
Collection<GrantedAuthority> generateAuthoritiesFromClaim(Collection<String> roles) {
return roles.stream()
.map(role -> new SimpleGrantedAuthority("ROLE_" role))
.collect(Collectors.toList());
}
Сервер ресурсов OAuth2
Если ваше приложение настроено как сервер ресурсов OAuth2, вы можете извлечь роли из маркера доступа. В конфигурации безопасности Spring определите JwtAuthenticationConverter
компонент, который извлекает роли из маркера доступа и сопоставляет их с GrantedAuthority
s. Здесь я расскажу, как должен выглядеть конкретный компонент. Полный пример доступен на моем GitHub: https://github.com/ThomasVitale/spring-security-examples/tree/main/oauth2/resource-server-jwt-authorities
public JwtAuthenticationConverter jwtAuthenticationConverterForKeycloak() {
Converter<Jwt, Collection<GrantedAuthority>> jwtGrantedAuthoritiesConverter = jwt -> {
Map<String, Collection<String>> realmAccess = jwt.getClaim("realm_access");
Collection<String> roles = realmAccess.get("roles");
return roles.stream()
.map(role -> new SimpleGrantedAuthority("ROLE_" role))
.collect(Collectors.toList());
};
var jwtAuthenticationConverter = new JwtAuthenticationConverter();
jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(jwtGrantedAuthoritiesConverter);
return jwtAuthenticationConverter;
}
Комментарии:
1. Мое приложение является клиентом OAuth, и ваш подход хорошо сработал для меня. Большое спасибо.
Ответ №2:
Я использую эту конфигурацию:
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.core.convert.converter.Converter;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.BeanIds;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// We can safely disable CSRF protection on the REST API because we do not rely on cookies (https://security.stackexchange.com/questions/166724/should-i-use-csrf-protection-on-rest-api-endpoints)
http.csrf(httpSecurityCsrfConfigurer -> httpSecurityCsrfConfigurer.ignoringAntMatchers("/api/**"));
http.cors();
http.authorizeRequests(registry -> {
registry.mvcMatchers("/api-docs/**", "/architecture-docs/**").permitAll();
registry.mvcMatchers("/api/integrationtest/**").permitAll();
registry.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll();
registry.mvcMatchers("/actuator/info", "/actuator/health").permitAll();
registry.anyRequest().authenticated();
});
http.oauth2ResourceServer()
.jwt()
.jwtAuthenticationConverter(jwtAuthenticationConverter());
}
@Bean(name = BeanIds.AUTHENTICATION_MANAGER)
public AuthenticationManager authenticationManagerBean() throws Exception {
// Although this seems like useless code,
// it is required to prevent Spring Boot creating a default password
return super.authenticationManagerBean();
}
@Bean
public JwtAuthenticationConverter jwtAuthenticationConverter() {
JwtAuthenticationConverter converter = new JwtAuthenticationConverter();
converter.setJwtGrantedAuthoritiesConverter(jwtToAuthorityConverter());
return converter;
}
@Bean
public Converter<Jwt, Collection<GrantedAuthority>> jwtToAuthorityConverter() {
return new Converter<Jwt, Collection<GrantedAuthority>>() {
@Override
public List<GrantedAuthority> convert(Jwt jwt) {
Map<String, Object> realmAccess = jwt.getClaimAsMap("realm_access");
if (realmAccess != null) {
@SuppressWarnings("unchecked")
List<String> roles = (List<String>) realmAccess.get("roles");
if (roles != null) {
return roles.stream()
.map(rn -> new SimpleGrantedAuthority("ROLE_" rn))
.collect(Collectors.toList());
}
}
return Collections.emptyList();
}
};
}
}
С этими зависимостями:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
И это свойство:
spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8181/auth/realms/myrealm
Дополнительный совет: Используйте https://github.com/ch4mpy/spring-addons для тестирования. Вы также можете взглянуть там на пример конфигурации (который отличается от того, что я делаю, но также должен работать нормально, см. https://github.com/ch4mpy/spring-addons/issues/27 для получения дополнительной информации об этих различиях): https://github.com/ch4mpy/starter/tree/master/api/webmvc/common-security-webmvc/src/main/java/com/c4_soft/commons/security
Комментарии:
1. Мое приложение является клиентом OAuth2, а не сервером ресурсов. Проект Spring-аддонов выглядит очень интересно, посмотрим на него. Спасибо @WimDeblauwe за то, что поделились своими мыслями.