spring-boot - 如何将Keycloak注册到Spring Eureka Server

有3个服务:

  • 用户服务
  • Eureka 发现服务
  • keycloak-authoriztaion-service

我已经配置了 eureka 服务器,它已经启动并正在运行,并且我已经注册了用户服务:@EnableDiscoveryClient

application.properties(用户服务)

spring.application.name=user-service
server.port=8040
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka

我可以看到已注册的用户服务,这对 Spring Boot 应用程序来说没问题。

如何将 Keycloak 注册到 Eureka 发现服务?

最佳答案

您可以尝试使用 spring security 和内置的 keycloak 支持。 (本指南)[ https://www.baeldung.com/spring-boot-keycloak]给出了一个非常完整的例子。

为完整起见,以下是指南的相关摘录。如果您还没有,则需要将 springs keycloak 依赖项添加到您的 pom.xml

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.keycloak.bom</groupId>
            <artifactId>keycloak-adapter-bom</artifactId>
            <version>3.3.0.Final</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

...
<dependency>
    <groupId>org.keycloak</groupId>
    <artifactId>keycloak-spring-boot-starter</artifactId>
</dependency>

您可以通过应用程序属性配置 spring。然后简单地设置扩展 key 斗篷适配器的 Spring 安全配置类。

@Configuration
@EnableWebSecurity
@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)
class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {

    @Autowired
    public void configureGlobal(
      AuthenticationManagerBuilder auth) throws Exception {

        KeycloakAuthenticationProvider keycloakAuthenticationProvider
         = keycloakAuthenticationProvider();
        keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(
          new SimpleAuthorityMapper());
        auth.authenticationProvider(keycloakAuthenticationProvider);
    }

    @Bean
    public KeycloakSpringBootConfigResolver KeycloakConfigResolver() {
        return new KeycloakSpringBootConfigResolver();
    }

    @Bean
    @Override
    protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
        return new RegisterSessionAuthenticationStrategy(
          new SessionRegistryImpl());
    }
 }

https://stackoverflow.com/questions/61889843/

相关文章:

laravel - Firefox 无法与 wss ://127. 0.0.1 的服务器建立连接

mongodb - mongoose 事务中创建集合的正确方法

karate - 在 Karate 中通过标签并行运行功能

postman - 如何为 postman (表单数据)中的特定字段传递空数组?

java - IntelliJ + Maven Shade : A JNI error has oc

c# - 在 Swagger 中隐藏参数的最佳方法

docker - 如何处理 make missing from alpine docker imag

python - 来自迭代器的随机项目?

css - 有没有办法使用变换比例使内容自动适合父 div?

angular - 如何从变量在 [innerHTML] 上启用 ngx-bootstrap 工具提