Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 无法使用KeyClope为身份验证构建基于spring的项目_Spring Boot_Keycloak - Fatal编程技术网

Spring boot 无法使用KeyClope为身份验证构建基于spring的项目

Spring boot 无法使用KeyClope为身份验证构建基于spring的项目,spring-boot,keycloak,Spring Boot,Keycloak,问题陈述 我无法通过spring boot项目进行构建,我想通过保护我的应用程序。钥匙斗篷 预期产出 项目构建成功,当我点击RESTURL时,它将重定向到密钥隐藏页面进行身份验证 员工关系控制员 @RestController public class EmployeeRestController { @GetMapping(path = "/username") public String getAuthorizedUserName() { return "Username

问题陈述

我无法通过spring boot项目进行构建,我想通过保护我的应用程序。钥匙斗篷

预期产出

项目构建成功,当我点击RESTURL时,它将重定向到密钥隐藏页面进行身份验证

员工关系控制员

@RestController
public class EmployeeRestController {

  @GetMapping(path = "/username")
  public String getAuthorizedUserName() {
    return "Username Returned";
  }

  @GetMapping(path = "/roles")
  public String getAuthorizedUserRoles() {
    return "Roles Returned";
  }
}
启动

@SpringBootApplication
public class Startup {

  public static void main(String[] args) {
    SpringApplication.run(Startup.class, args);
  }
}
应用程序属性

server.port=8085

keycloak.realm=wow
keycloak.auth-server-url=http://localhost:8180/auth
keycloak.resource=wow-client
keycloak.public-client=true
keycloak.securityConstraints[0].authRoles[0]=user
keycloak.securityConstraints[0].securityCollections[0].patterns[0]=/*
pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.keycloak/keycloak-spring-boot-starter -->
    <dependency>
        <groupId>org.keycloak</groupId>
        <artifactId>keycloak-spring-boot-starter</artifactId>
        <version>7.0.0</version>
    </dependency>
</dependencies>

<强>注释:请考虑密钥外罩的正确配置< <强>


我使用springboot-2.2.0/keydove-7.0.1也遇到了同样的问题。 我将bean keydepospringbootconfiguresolver移动到另一个配置文件,它可以工作

@Configuration
public class BeanKeycloakConfiguration {
    @Bean
    public KeycloakSpringBootConfigResolver KeycloakConfigResolver() {
        return new KeycloakSpringBootConfigResolver();
    }
}`

这是从7.0.0版开始的KeyClope Spring引导适配器的一个已知问题。该问题已在本相关问题中得到解决,并记录在Jira项目中:

另一种解决方案是使用显式的
keydepospringbootproperties
参数声明一个自定义
keydepospringbootconfigresolver
bean,这使得Spring Boot能够从
application.properties
文件正确读取keydeport配置

@Configuration
public class MyKeycloakSpringBootConfigResolver extends KeycloakSpringBootConfigResolver {
    private final KeycloakDeployment keycloakDeployment;

    public MyKeycloakSpringBootConfigResolver(KeycloakSpringBootProperties properties) {
        keycloakDeployment = KeycloakDeploymentBuilder.build(properties);
    }

    @Override
    public KeycloakDeployment resolve(HttpFacade.Request facade) {
        return keycloakDeployment;
    }
}
如果还使用Spring安全性,那么您还需要在安全配置类中定义这个主bean,并返回您的自定义
keydeposteSpringBootConfigResolver
bean

@Bean
@Primary
public KeycloakConfigResolver keycloakConfigResolver(KeycloakSpringBootProperties properties) {
    return new MyKeycloakSpringBootConfigResolver(properties);
}

关于循环依赖问题的一个注记

在我将Spring Boot Web从Tomcat切换到Jetty之后,我的
公共类SecurityConfiguration Extendes KeyDopperWebSecurityConfigureAdapter
KeyDopperSpringBootConfigResolver
之间出现了一个无法解决的循环依赖关系的异常(请参阅)

Spring Boot app start上的异常如下所示:

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfiguration': Unsatisfied dependency expressed through field 'keycloakConfigResolver'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'keycloakConfigResolver': Requested bean is currently in creation: Is there an unresolvable circular reference
为了修复无法解决的循环依赖,我改变了

@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)

在我的
SecurityConfiguration
类中


初始配置来自。

我遇到了某种程度上相同的问题,当我定义bean时,我收到:创建名为“keydeposconfigresolver”的bean时出错:请求的bean当前正在创建中:是否存在无法解决的循环引用?使用弹簧靴2.1.8/KeyClope弹簧靴启动器7.0.0。您能编辑并添加您正在使用的Spring Boot版本吗?
@ComponentScan(basePackageClasses = KeycloakSecurityComponents.class)
@ComponentScan(basePackageClasses = KeycloakSpringBootConfigResolver.class)