我正在使用key斗篷来保护我的spring引导后端。
依赖项:
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-spring-boot-2-adapter</artifactId>
<version>12.0.3</version>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-tomcat7-adapter-dist</artifactId>
<version>12.0.3</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-spring-security-adapter</artifactId>
<version>12.0.3</version>
</dependency>
安全配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry expressionInterceptUrlRegistry = http.cors()
.and()
.csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests();
expressionInterceptUrlRegistry = expressionInterceptUrlRegistry.antMatchers("/iam/accounts/promoters*").hasRole("PROMOTER");
expressionInterceptUrlRegistry.anyRequest().permitAll();
}
一切正常!
但是现在我在keydove令牌“roles”中添加了一个新的部分,我需要在spring引导中以某种方式扩展keydove jwt类,并编写一些代码来解析角色信息并将其存储到securitycontext。你能告诉我怎么把目标存档吗?
2条答案
按热度按时间gk7wooem1#
我不明白你为什么需要扩展密钥斗篷令牌。keydepot令牌中已经存在角色。我将尝试解释如何访问它,key斗篷有两个角色级别,1)领域级别和2)应用程序(客户端)级别,默认情况下,您的key斗篷适配器使用领域级别,要使用应用程序级别,您需要在application.yml中将propertie key斗篷.use-resource-role-mappings设置为true
如何在realmenter image description中创建角色
如何在这里创建客户图像描述中的角色
角色为admin(realm)和add\u user(application)的用户在此处输入图像描述
要获得访问角色,您可以在keydove适配器中使用keydoveauthenticationtoken类,您可以尝试调用以下方法:
要使用spring security保护任何路由器,您可以使用此注解, 示例如下:
obs:the 使用@preauthorize注解设置key斗篷.use-resource-role-mappings。 如果设置为true,@preauthorize检查token.getrealmaccess().getroles()中的角色,如果设置为false,则检查token.getresourceaccess()中的角色。
如果您想在token中添加任何自定义声明,请让我知道我可以更好地解释。
我在这里介绍了如何设置key斗篷适配器以及 应用程序.yml:
安全配置.java
应用程序.yml
roqulrg32#
首先,扩展keydrope accesstoken:
然后: