Preface
Reference in this article A Quick Guide to Using Keycloak with Spring Boot , sort out the problems encountered in the actual combat.
Docker installation keyload
Download mirroring
docker pull quay.io/keycloak/keycloak
If you fail, try again.
Start keyclock
docker run -p 6060:8080 -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin quay.io/keycloak/keycloak:latest
The port is mapped according to the actual situation.
The official is: docker run - P 8080:8080 - e keycloak? User = admin - e keycloak? Password = admin quay. IO / keycloak / keycloak: 9.0.2
At present, the latest version is 9.0.2. It is recommended to directly replace it with: latest, automatically pull the latest image.
Start successful:
This example starts in the local environment. If it starts in the production environment, SSL needs to be enabled.
Browsers and applications that interact with the realm must honor the SSL/HTTPS requirements defined by the SSL Mode or they will not be allowed to interact with the server.
Land
http://localhost:6060/auth
Click Administration Console
User name: admin Password: admin
Keyload configuration
Reference resources A Quick Guide to Using Keycloak with Spring Boot , the same configuration.
Spring Boot application launch
Download source code
Change configuration
keycloak.auth-server-url=http://localhost:6060/auth
keycloak.realm=SpringBootKeycloak keycloak.resource=login-app keycloak.public-client=true keycloak.security-constraints[0].authRoles[0]=user keycloak.security-constraints[0].securityCollections[0].patterns[0]=/customers/*
Change dependency
Part of the source code is dependent on and cannot be pulled.
Remove parent
<parent> <groupId>com.baeldung</groupId> <artifactId>parent-boot-1</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../../parent-boot-1</relativePath> </parent>
Increase dependency management
<properties> <spring-boot.version>1.5.22.RELEASE</spring-boot.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
Increase dependency
<dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>org.keycloak</groupId> <artifactId>keycloak-spring-boot-starter</artifactId> <version>3.3.0.Final</version> </dependency>
Add Maven compiler plugin
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>10</source> <target>10</target> </configuration> </plugin> </plugins> </build>
start-up
23:37:26.668 [main] INFO o.s.j.e.a.AnnotationMBeanExporter - Registering beans for JMX exposure on startup 23:37:26.681 [main] INFO o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8081"] 23:37:26.689 [main] INFO o.a.tomcat.util.net.NioSelectorPool - Using a shared selector for servlet write/read 23:37:26.701 [main] INFO o.s.b.c.e.t.TomcatEmbeddedServletContainer - Tomcat started on port(s): 8081 (http) 23:37:26.735 [main] INFO com.baeldung.keycloak.SpringBoot - Started SpringBoot in 4.541 seconds (JVM running for 10.586)
Visit
http://localhost:8081/
Click customers to log in and get the list
User name: user1 Password: xsw2@WSX