spring-boot - 无法使用 keycloak 构建基于 spring 的项目进行身份验证
问题描述
问题陈述
我无法通过 Spring Boot 项目构建,我想通过它来保护我的应用程序。钥匙披风。
预期产出
项目构建成功,当我点击其余 URL 时,它将重定向到密钥斗篷页面进行身份验证。
EmployeeRestController
@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>
错误日志
2019-09-16 17:44:24.525 INFO 10396 --- [ main] com.diwakar.Startup : Starting Startup on diwakarb with PID 10396 (started by diwakarb in E:\DB Godam\Sample-KeyCloak-Project)
2019-09-16 17:44:24.527 INFO 10396 --- [ main] com.diwakar.Startup : No active profile set, falling back to default profiles: default
2019-09-16 17:44:25.419 WARN 10396 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tomcatServletWebServerFactory' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryConfiguration$EmbeddedTomcat.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.keycloak.adapters.springboot.KeycloakAutoConfiguration': Unsatisfied dependency expressed through method 'setKeycloakSpringBootProperties' parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2019-09-16 17:44:25.429 INFO 10396 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-09-16 17:44:25.531 ERROR 10396 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 1 of method setKeycloakSpringBootProperties in org.keycloak.adapters.springboot.KeycloakBaseSpringBootConfiguration required a bean of type 'org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver' that could not be found.
Action:
Consider defining a bean of type 'org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver' in your configuration.
Process finished with exit code 1
注意:请考虑密钥披风配置正确
解决方案
我使用 springboot-2.2.0/keycloak-7.0.1 遇到了同样的问题。我将 bean KeycloakSpringBootConfigResolver 移动到另一个配置文件,它可以工作。
@Configuration
public class BeanKeycloakConfiguration {
@Bean
public KeycloakSpringBootConfigResolver KeycloakConfigResolver() {
return new KeycloakSpringBootConfigResolver();
}
}`
推荐阅读
- azure-data-explorer - 列内的日期时间差(Kusto 查询语言)
- java - 关于登录和验证码的 Vue.js 和 Springboot 应用程序错误
- python-3.x - 如何使用 pandas 读取 json 嵌套数据
- pine-script - 如何将交易入场蜡烛的 ema 50 设置为止损
- javascript - 参数被覆盖
- r - 创建一个 R 数据框,其中包含另一个数据框列中唯一值的计数
- sql - 无论记录是否存在,插入并返回 id
- r - 当您将 VR2AR 接收器用于 VPS 系统时,您是否需要包含额外的标记?
- firefox - 如何消除 html 文档中的空格?
- sql - DB2 中的 Cross Apply 等价物