控制器:
package com.example.ex.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value="/")
public class KeycloakController {
@RequestMapping(value="/", method=RequestMethod.GET)
public String adminPage() {
return "redirect:admin.html";
}
@RequestMapping(value="/user", method=RequestMethod.GET)
public String userPage() {
return "redirect:user.html";
}
}
这是application.properties文件
keycloak.realm = finaltest
keycloak.auth-server-url = http://localhost:8180/auth
keycloak.resource = ex
keycloak.public-client=false
keycloak.enabled=true
keycloak.credentials.secret=2462fa5e-2f23-496f-a28f-b13ad7948999
keycloak.ssl-required = external
#keycloak.bearer-only=true
#keycloak.policy-enforcer-config.enforcement-mode=ENFORCING
#keycloak.principal-attribute=preferred_username
keycloak.autodetect-bearer-only=true
keycloak.use-resource-role-mappings = true
keycloak.security-constraints[0].auth-roles[0]=slave
keycloak.security-constraints[0].auth-roles[1]=master
keycloak.security-constraints[0].security-collections[0].patterns[0]=/*
keycloak.policy-enforcer-config.enforcement-mode=ENFORCING
keycloak.policy-enforcer-config.paths[0].path=/admin
#keycloak.use-resource-role-mappings=true
keycloak.json:
{
"realm": "finaltest",
"auth-server-url": "http://localhost:8180/auth",
"ssl-required": "external",
"resource": "ex",
"credentials": {
"secret": "2462fa5e-2f23-496f-a28f-b13ad7948999"
},
"use-resource-role-mappings": true,
"confidential-port": 0,
"policy-enforcer": {}
}
错误StackTrace:
java.lang.RuntimeException: Could not obtain configuration from server [http://localhost:8180/auth/realms/finaltest/.well-known/uma-configuration].
at org.keycloak.authorization.client.AuthzClient.<init>(AuthzClient.java:92) ~[keycloak-authz-client-3.4.2.Final.jar:3.4.2.Final]
at org.keycloak.authorization.client.AuthzClient.create(AuthzClient.java:64) ~[keycloak-authz-client-3.4.2.Final.jar:3.4.2.Final]
at org.keycloak.adapters.authorization.PolicyEnforcer.<init>(PolicyEnforcer.java:61) ~[keycloak-adapter-core-3.4.2.Final.jar:3.4.2.Final]
at org.keycloak.adapters.KeycloakDeploymentBuilder.internalBuild(KeycloakDeploymentBuilder.java:143) ~[keycloak-adapter-core-3.4.2.Final.jar:3.4.2.Final]
at org.keycloak.adapters.KeycloakDeploymentBuilder.build(KeycloakDeploymentBuilder.java:169) ~[keycloak-adapter-core-3.4.2.Final.jar:3.4.2.Final]
at org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver.resolve(KeycloakSpringBootConfigResolver.java:37) ~[keycloak-spring-boot-adapter-3.4.2.Final.jar:3.4.2.Final]
at org.keycloak.adapters.AdapterDeploymentContext.resolveDeployment(AdapterDeploymentContext.java:88) ~[keycloak-adapter-core-3.4.2.Final.jar:3.4.2.Final]
at org.keycloak.adapters.PreAuthActionsHandler.preflightCors(PreAuthActionsHandler.java:107) ~[keycloak-adapter-core-3.4.2.Final.jar:3.4.2.Final]
at org.keycloak.adapters.PreAuthActionsHandler.handleRequest(PreAuthActionsHandler.java:79) ~[keycloak-adapter-core-3.4.2.Final.jar:3.4.2.Final]
at org.keycloak.adapters.tomcat.AbstractKeycloakAuthenticatorValve.invoke(AbstractKeycloakAuthenticatorValve.java:177) ~[spring-boot-container-bundle-3.4.2.Final.jar:3.4.2.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) ~[tomcat-embed-core-8.5.34.jar:8.5.34]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.34.jar:8.5.34]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.34.jar:8.5.34]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.34.jar:8.5.34]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800) [tomcat-embed-core-8.5.34.jar:8.5.34]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.34.jar:8.5.34]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806) [tomcat-embed-core-8.5.34.jar:8.5.34]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498) [tomcat-embed-core-8.5.34.jar:8.5.34]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.34.jar:8.5.34]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_181]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.34.jar:8.5.34]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]
Caused by: org.keycloak.authorization.client.util.HttpResponseException: Unexpected response from server: 404 / Not Found
at org.keycloak.authorization.client.util.HttpMethod.execute(HttpMethod.java:95) ~[keycloak-authz-client-3.4.2.Final.jar:3.4.2.Final]
at org.keycloak.authorization.client.util.HttpMethodResponse$2.execute(HttpMethodResponse.java:48) ~[keycloak-authz-client-3.4.2.Final.jar:3.4.2.Final]
at org.keycloak.authorization.client.AuthzClient.<init>(AuthzClient.java:90) ~[keycloak-authz-client-3.4.2.Final.jar:3.4.2.Final]
... 22 common frames omitted
那我该怎么做才能摆脱这些错误? 我只是在将Spring Boot应用程序与keycloak集成时遇到问题。
是否有必要在应用程序的资源中包含keycloak.json文件?
答案 0 :(得分:2)
最好使用spring boot 2适配器(版本高于2.0.0)并使用最新版本的keycloak(4.5.0或4.6.0),因为它具有向后兼容性。