【问题标题】:Keycloak secure Spring Boot applicationKeycloak 安全 Spring Boot 应用程序
【发布时间】:2019-10-09 11:51:10
【问题描述】:

我正在使用 keycloak 测试 Web 应用程序 Spring Boot 的安全性,我添加了所有必要的依赖项,但是在撕裂应用程序时遇到了问题。

我使用: 弹簧靴 2.2.0.M3 Java 11

我包括: keycloak-spring-boot-starter 和 keycloak-adapter-bom

这是我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.0.M3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>gn.traore</groupId>
    <artifactId>demo-spring-boot-keycloak</artifactId>
    <version>1.0</version>
    <name>demo-spring-boot-keycloak</name>
    <description>Demo project for Spring Boot with Keycloak</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.keycloak/keycloak-spring-boot-starter -->
        <dependency>
            <groupId>org.keycloak</groupId>
            <artifactId>keycloak-spring-boot-starter</artifactId>
            <version>6.0.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.keycloak.bom/keycloak-adapter-bom -->
        <dependency>
            <groupId>org.keycloak.bom</groupId>
            <artifactId>keycloak-adapter-bom</artifactId>
            <version>6.0.1</version>
            <type>pom</type>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>

</project>

它向我显示此错误消息:

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.IllegalArgumentException: Dupl
icate context initialization parameter [keycloak.config.resolver]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:157)
 ~[spring-boot-2.2.0.M3.jar:2.2.0.M3]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-5.2.0.M2.
jar:5.2.0.M2]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~
[spring-boot-2.2.0.M3.jar:2.2.0.M3]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:782) ~[spring-boot-2.2.0.M3.jar:2.2.0.M3]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:404) ~[spring-boot-2.2.0.M3.jar:2.2.0.M3]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:319) ~[spring-boot-2.2.0.M3.jar:2.2.0.M3]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1275) ~[spring-boot-2.2.0.M3.jar:2.2.0.M3]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1263) ~[spring-boot-2.2.0.M3.jar:2.2.0.M3]
        at gn.traore.DemoSpringBootKeycloakApplication.main(DemoSpringBootKeycloakApplication.java:10) ~[classes/:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
        at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.2.0.M3.jar:2.2.0.M3]
Caused by: java.lang.IllegalArgumentException: Duplicate context initialization parameter [keycloak.config.resolver]
        at org.apache.catalina.core.StandardContext.addParameter(StandardContext.java:3111) ~[tomcat-embed-core-9.0.19.jar:9.0.19]
        at org.keycloak.adapters.springboot.KeycloakBaseSpringBootConfiguration$KeycloakBaseTomcatContextCustomizer.customize(KeycloakBaseSpringB
ootConfiguration.java:271) ~[keycloak-spring-boot-adapter-core-6.0.1.jar:6.0.1]
        at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.configureContext(TomcatServletWebServerFactory.java:377) ~[
spring-boot-2.2.0.M3.jar:2.2.0.M3]
        at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.prepareContext(TomcatServletWebServerFactory.java:234) ~[sp
ring-boot-2.2.0.M3.jar:2.2.0.M3]
        at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:183) ~[spri
ng-boot-2.2.0.M3.jar:2.2.0.M3]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.jav
a:181) ~[spring-boot-2.2.0.M3.jar:2.2.0.M3]
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:154)
 ~[spring-boot-2.2.0.M3.jar:2.2.0.M3]
        ... 13 common frames omitted  

【问题讨论】:

标签: java spring-boot keycloak


【解决方案1】:

在 spring-boot 中有一个错误,即 webserver 定制器被调用了两次。因为那个 bug keycloak 被初始化了两次。它已在 spring-boot 2.2.0.M5 中解决。所以升级spring-boot将解决这个问题。

https://github.com/spring-projects/spring-boot/issues/17264

【讨论】:

    【解决方案2】:

    您可以通过添加排除KeycloakAutoConfiguration

    @SpringBootApplication( exclude = { KeycloakAutoConfiguration.class } )
    

    主应用程序类的注释。

    【讨论】:

    • 我已经排除了 KeycloakAutoConfiguration 并且它现在可以工作了......谢谢!
    • 不是正确或最佳答案,因为这只是一种解决方法,您禁用 sprintboot 自动配置并且您必须手动配置它。使用 KeycloakSpringBootConfigResolver 也会出现问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 2017-08-07
    • 2018-07-22
    • 2021-07-24
    • 2018-10-31
    • 2015-10-10
    • 2019-01-02
    相关资源
    最近更新 更多