我正在创建一个简单的 SpringBoot 应用程序并尝试与 OAuth 2.0 提供程序 Keycloak 集成。我在领域级别创建了领域、客户端、角色(成员、PremiumMember),最后创建了用户并分配了角色(成员、PremiumMember)。
如果我使用 Keycloak https://www.keycloak.org/docs/latest/securing_apps/index.html#_spring_boot_adapter提供的 SpringBoot 适配器,那么当我成功登录并检查登录用户的权限时,我可以看到分配的角色例如会员、高级会员。
Collection<? extends GrantedAuthority> authorities =
SecurityContextHolder.getContext().getAuthentication().getAuthorities();
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用通用 SpringBoot Auth2 客户端配置,我可以登录,但是当我检查权限时,它总是仅显示ROLE_USER、SCOPE_email、SCOPE_openid、SCOPE_profile ,并且不包括我映射的角色(Member、PremiumMember)。
我的 SpringBoot OAuth2 配置:
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
应用程序属性
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
Run Code Online (Sandbox Code Playgroud)
我正在使用SpringBoot 2.5.5和Keycloak 15.0.2。
使用这种通用的 OAuth2.0 配置方法(不使用 Keycloak SpringBootAdapter)有没有办法获取分配的角色?
OAuth2AuthenticationToken我正在尝试从被检测为 Spring Security 机构的角色声明中获取角色声明。在 OIDC 提供者端(在我的例子中是 Azure AD)定义了一个自定义角色,该角色嵌套在 中DefaultOidcUser,但不会自动添加到权限中:

我尝试像这样从 Jwt 令牌中提取它们
但是,当我这样做时,以下方法都不会被调用(无论是在登录期间,还是稍后,即使在默认配置中):
JwtGrantedAuthoritiesConverter.convert(Jwt)JwtAuthenticationConverter.convert(Jwt)JwtAuthenticationConverter.extractAuthorities(Jwt)我当前的配置是:
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
@Import(SecurityProblemSupport.class)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.csrf()
<some more config that has nothing to do with oauth/oidc>
.and()
.oauth2Login()
.and()
.oauth2ResourceServer()
.jwt()
.jwtAuthenticationConverter(jwtAuthenticationConverter())
.and()
.and()
.oauth2Client()
;
}
private JwtAuthenticationConverter jwtAuthenticationConverter() {
// create a custom JWT converter to map …Run Code Online (Sandbox Code Playgroud) 我正在尝试在具有 spring-security 和 keycloak 的 java 应用程序中同时使用领域和资源角色。不幸的是,keycloak 只会根据 的值返回一个或另一个:
keycloak.use-resource-role-mappings=true
Run Code Online (Sandbox Code Playgroud)
您仍然可以使用自定义代码获得两者,但它会混淆@PreAuthorize 或 spring-boot 方法 .isUserInRole 等注释,从而导致代码丑陋。
有没有办法覆盖 @PreAuthorize 方法或 Keycloak 返回的 JSON 令牌以同时使用领域和资源角色?目前,我的 keyclaok 实现使用自定义方法替换每个方法开始时的 @PreAuthorize,它并不漂亮。
先感谢您。
我有一个 JWT,可以在特定声明下找到角色。该声明处于嵌套结构中。如何告诉 JwtAuthenticationConverter 查找特定路径下的角色?
作为授权服务器,我使用 Keycloak。可以为角色添加映射器。但我现在想排除这种可能性,因为目标是找到特定声明下的角色。
这是我解码的 JWT。角色“user-role”应位于声明下:“resource_access”->“user”->“roles”:
"resource_access": {
"admin": {
"roles": [
"admin-role"
]
},
"user": {
"roles": [
"user-role"
]
},
"account": {
"roles": [
"manage-account",
"manage-account-links",
"view-profile"
]
}
},
Run Code Online (Sandbox Code Playgroud)
这是我的 JwtAuthenticationConverter 配置:
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter
{
@Override
public void configure(HttpSecurity http) throws Exception
{
http.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.oauth2ResourceServer()
.jwt()
.jwtAuthenticationConverter(jwtAuthenticationConverter());
}
private static JwtAuthenticationConverter jwtAuthenticationConverter()
{
var jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("resource_access.user.roles");
jwtGrantedAuthoritiesConverter.setAuthorityPrefix("ROLE_");
var jwtAuthenticationConverter = new JwtAuthenticationConverter(); …Run Code Online (Sandbox Code Playgroud) 我有一个由 Keycloak 保护的 Spring Boot 管理应用程序,我定义了一个具有领域角色的用户ACTUATOR。问题是身份验证后 Spring Security 无权访问领域角色。我可以看到授予的权限:Granted Authorities: ROLE_USER, SCOPE_actuator_access, SCOPE_profile'
查看文档,我发现这一部分:基于委派的策略与 OAuth2UserService这是我的配置:
这是我的配置:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure( HttpSecurity http ) throws Exception {
http.csrf().disable().cors().disable();
http.authorizeRequests()
.antMatchers( "/error","/instances", "/**/*.css", "/**/img/**", "/**/third-party/**", "/*.js" )
.permitAll()
.anyRequest().hasRole( "ACTUATOR" )
.and()
.oauth2Login( oauth2 -> oauth2.userInfoEndpoint(
userInfo -> userInfo.oidcUserService( this.oidcUserService() ) ) );
}
// I just copy-paste the doc's code to play with it...
private OAuth2UserService<OidcUserRequest, OidcUser> oidcUserService() { …Run Code Online (Sandbox Code Playgroud) spring-security spring-boot spring-security-oauth2 keycloak spring-boot-admin
有人在使用 Spring Boot 资源服务器和 Keycloak 吗?
我配置了我的 application.properties
spring.security.oauth2.resourceserver.jwt.issuer-uri = http://localhost:9080/auth/realms/<myrealm>
在我的 WebSecurityConfigurerAdapter 中,我可以使用客户端范围,例如
.antMatchers(HttpMethod.GET, "/user/**").hasAuthority("SCOPE_read")
但我无法使用这些角色!
.antMatchers(HttpMethod.GET, "/user/**").hasRole("ADMIN")
该信息在 jwt 中可用,但 spring 不知何故不使用它。有人知道我在哪里可以找到描述映射的和平文档吗?不知怎的,我觉得我的脑子里有一个节点,但在哪里,是哪一个?
那是我的jwt:
"exp": 1603373908,
"iat": 1603373608,
"jti": "0b18b386-9f62-4c42-810e-692ccc4ed7d1",
"iss": "http://localhost:9080/auth/realms/jhipster",
"aud": "account",
"sub": "4c973896-5761-41fc-8217-07c5d13a004b",
"typ": "Bearer",
"azp": "web_app",
"session_state": "17411db5-8d50-4f25-b520-9a3e8b19fd67",
"acr": "1",
"allowed-origins": [
"*"
],
"realm_access": {
"roles": [
"test",
"ROLE_USER",
"offline_access",
"ROLE_ADMIN",
"uma_authorization"
]
},
"resource_access": {
"account": {
"roles": [
"manage-account",
"manage-account-links",
"view-profile"
]
}
},
"scope": "email profile",
"email_verified": true,
"roles": [
"test", …Run Code Online (Sandbox Code Playgroud) spring-security spring-boot openid-connect keycloak spring-oauth2