From 372ea5b0e81b5a24acc3bfad9f152f366b0929ce Mon Sep 17 00:00:00 2001 From: simeng-li Date: Fri, 28 Jun 2024 14:28:04 +0800 Subject: [PATCH 1/3] docs(console): update the java spring guide update the java spring guide --- .../docs/fragments/_redirect-uris-web.mdx | 2 +- .../guides/web-java-spring-boot/README.mdx | 80 +++++++++---------- 2 files changed, 38 insertions(+), 44 deletions(-) diff --git a/packages/console/src/assets/docs/fragments/_redirect-uris-web.mdx b/packages/console/src/assets/docs/fragments/_redirect-uris-web.mdx index 60f310f2a7b..0092296a212 100644 --- a/packages/console/src/assets/docs/fragments/_redirect-uris-web.mdx +++ b/packages/console/src/assets/docs/fragments/_redirect-uris-web.mdx @@ -13,6 +13,6 @@ Now, let's configure your redirect URI. E.g. {`${props.defaultUri ?? 'http://loc -Just like signing in, users should be redirected to Logto for signing out of the shared session. Once finished, it would be great to redirect the user back to your website. For example, add `http://localhost:3000` as the post sign-out redirect URI below. +Just like signing in, users should be redirected to Logto for signing out of the shared session. Once finished, it would be great to redirect the user back to your website. For example, add {`${props.defaultLogoutUri ?? 'http://localhost:3000'}`} as the post sign-out redirect URI below. diff --git a/packages/console/src/assets/docs/guides/web-java-spring-boot/README.mdx b/packages/console/src/assets/docs/guides/web-java-spring-boot/README.mdx index b043d49b775..0987b7eb7b8 100644 --- a/packages/console/src/assets/docs/guides/web-java-spring-boot/README.mdx +++ b/packages/console/src/assets/docs/guides/web-java-spring-boot/README.mdx @@ -2,23 +2,16 @@ import UriInputField from '@/mdx-components/UriInputField'; import Steps from '@/mdx-components/Steps'; import Step from '@/mdx-components/Step'; +import Checkpoint from '../../fragments/_checkpoint.md'; +import RedirectUrisWeb from '../../fragments/_redirect-uris-web.mdx'; + - This tutorial will show you how to integrate Logto into your Java Spring Boot web application. - -
    -
  • - The sample was created using the Spring Boot [securing web - starter](https://spring.io/guides/gs/securing-web). Following the instructions to bootstrap a - new web application. -
  • -
  • - The sample uses the [Spring Security - OAuth2](https://spring.io/guides/tutorials/spring-boot-oauth2) library to handle OIDC - authentication and integrate with Logto. -
  • -
+ +This tutorial will show you how to integrate Logto into your Java Spring Boot web application. + +No official SDK is required to integrate Logto with your Java Spring Boot application. We will use the [Spring Security](https://spring.io/projects/spring-security) and [Spring Security OAuth2](https://spring.io/guides/tutorials/spring-boot-oauth2) libraries to handle the OIDC authentication flow with Logto. Before we begin, make sure you have went through the spring boot guides linked above. @@ -27,21 +20,21 @@ Before we begin, make sure you have went through the spring boot guides linked a Include the following dependencies in your `build.gradle` file: -```gradle +```groovy title="build.gradle" dependencies { implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' - implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-security' - implementation 'org.springframework.boot:spring-boot-starter-oauth2-client' + implementation 'org.springframework.boot:spring-boot-starter-oauth2-client' } ``` -The sample uses [gradle](https://spring.io/guides/gs/gradle) as the build tool. You can use +Our sample project uses [gradle](https://spring.io/guides/gs/gradle) as the build tool. You can use maven or any other build tool as well. The configurations might be slightly different. For maven, include the following dependencies in your `pom.xml` file: -```maven +```xml title="pom.xml" org.springframework.boot spring-boot-starter-thymeleaf @@ -67,7 +60,7 @@ For maven, include the following dependencies in your `pom.xml` file: Register your application with Logto to get the client credentials and IdP configurations. Add the following configuration to your `application.properties` file: - + {`spring.security.oauth2.client.registration.logto.client-name=logto spring.security.oauth2.client.registration.logto.client-id=${props.app.id} spring.security.oauth2.client.registration.logto.client-secret=${props.app.secret} @@ -86,19 +79,19 @@ spring.security.oauth2.client.provider.logto.jwk-set-uri=${props.endpoint}oidc/j -In order to redirect users back to your application after they sign in, you need to set the redirect URI using the `client.registration.logto.redirect-uri` property in the previous step. + - - -e.g. In our example, the redirect URI is `http://localhost:8080/login/oauth2/code/logto`. +Make sure the redirect URI in Logto matches the `redirect-uri` set in the `application.properties` file in the previous step. -#### Create a new class `WebSecurityConfig` in your project: +The `WebSecurityConfig` class will be used to configure the security settings for your application. It is the key class that will handle the authentication and authorization flow. Please check the [Spring Security documentation](https://spring.io/guides/topicals/spring-security-architecture) for more details. + +### Create a new class `WebSecurityConfig` in your project -```java +```java title="WebSecurityConfig.java" package com.example.securingweb; import org.springframework.context.annotation.Configuration; @@ -112,11 +105,11 @@ public class WebSecurityConfig { } ``` -#### Create a idTokenDecoderFactory bean to set the JWS algorithm to `ES384`: +### Create a idTokenDecoderFactory bean to set the JWS algorithm to `ES384` This is required because Logto uses ES384 as the default algorithm, we need to update the OidcIdTokenDecoderFactory to use the same algorithm. -```java +```java title="WebSecurityConfig.java" import org.springframework.context.annotation.Bean; import org.springframework.security.oauth2.client.oidc.authentication.OidcIdTokenDecoderFactory; import org.springframework.security.oauth2.client.registration.ClientRegistration; @@ -135,11 +128,11 @@ public class WebSecurityConfig { } ``` -#### Create a LoginSuccessHandler class to handle the login success event: +### Create a LoginSuccessHandler class to handle the login success event Redirect the user to the user page after successful login: -```java +```java title="LoginSuccessHandler.java" package com.example.securingweb; import java.io.IOException; @@ -160,11 +153,11 @@ public class CustomSuccessHandler implements AuthenticationSuccessHandler { } ``` -#### Create a LogoutSuccessHandler class to handle the logout success event: +### Create a LogoutSuccessHandler class to handle the logout success event Clear the session and redirect the user to the home page. -```java +```java title="LogoutSuccessHandler.java" package com.example.securingweb; import java.io.IOException; @@ -192,11 +185,11 @@ public class CustomLogoutHandler implements LogoutSuccessHandler { } ``` -#### Create a `securityFilterChain` bean to configure the security configuration: +#### Create a `securityFilterChain` bean to configure the security configuration Add the following code to complete the `WebSecurityConfig` class: -```java +```java title="WebSecurityConfig.java" import org.springframework.context.annotation.Bean; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.web.DefaultSecurityFilterChain; @@ -231,9 +224,8 @@ public class WebSecurityConfig { (You may skip this step if you already have a home page in your project) -HomeController.java: -```java +```java title="HomeController.java" package com.example.securingweb; import java.security.Principal; @@ -252,9 +244,7 @@ public class HomeController { This controller will redirect the user to the user page if the user is authenticated, otherwise, it will show the home page. -home.html: - -```html +```html title="resources/templates/home.html"

Welcome!

@@ -268,7 +258,7 @@ home.html: Create a new controller to handle the user page: -```java +```java title="UserController.java" package com.example.securingweb; import java.security.Principal; @@ -304,9 +294,7 @@ public class UserController { Read the user information from the `OAuth2User` object and pass it to the `user.html` template. -user.html: - -```html +```html title="resources/templates/user.html"

User Details

@@ -325,4 +313,10 @@ user.html: + + + + + + From 17b66c2899a08149193ac115ef6216ebe83b9e80 Mon Sep 17 00:00:00 2001 From: simeng-li Date: Sun, 30 Jun 2024 18:36:07 +0800 Subject: [PATCH 2/3] chore(console): polish springboot docs polish springboot docs --- .../console/src/assets/docs/fragments/_redirect-uris-web.mdx | 2 +- .../src/assets/docs/guides/web-java-spring-boot/README.mdx | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/console/src/assets/docs/fragments/_redirect-uris-web.mdx b/packages/console/src/assets/docs/fragments/_redirect-uris-web.mdx index 0092296a212..36f88086853 100644 --- a/packages/console/src/assets/docs/fragments/_redirect-uris-web.mdx +++ b/packages/console/src/assets/docs/fragments/_redirect-uris-web.mdx @@ -13,6 +13,6 @@ Now, let's configure your redirect URI. E.g. {`${props.defaultUri ?? 'http://loc -Just like signing in, users should be redirected to Logto for signing out of the shared session. Once finished, it would be great to redirect the user back to your website. For example, add {`${props.defaultLogoutUri ?? 'http://localhost:3000'}`} as the post sign-out redirect URI below. +Just like signing in, users should be redirected to Logto for signing out of the shared session. Once finished, it would be great to redirect the user back to your website. For example, add {`${props.defaultSignOutUri ?? 'http://localhost:3000'}`} as the post sign-out redirect URI below. diff --git a/packages/console/src/assets/docs/guides/web-java-spring-boot/README.mdx b/packages/console/src/assets/docs/guides/web-java-spring-boot/README.mdx index 0987b7eb7b8..c3a736adf87 100644 --- a/packages/console/src/assets/docs/guides/web-java-spring-boot/README.mdx +++ b/packages/console/src/assets/docs/guides/web-java-spring-boot/README.mdx @@ -79,7 +79,7 @@ spring.security.oauth2.client.provider.logto.jwk-set-uri=${props.endpoint}oidc/j - + Make sure the redirect URI in Logto matches the `redirect-uri` set in the `application.properties` file in the previous step. @@ -254,7 +254,7 @@ This controller will redirect the user to the user page if the user is authentic - + Create a new controller to handle the user page: From e19b856b0f73015b926eec5f63984e60369464d0 Mon Sep 17 00:00:00 2001 From: simeng-li Date: Sun, 30 Jun 2024 18:38:40 +0800 Subject: [PATCH 3/3] chore(console): rename the section title rename the section title --- .../src/assets/docs/guides/web-java-spring-boot/README.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/console/src/assets/docs/guides/web-java-spring-boot/README.mdx b/packages/console/src/assets/docs/guides/web-java-spring-boot/README.mdx index c3a736adf87..c9699e2a853 100644 --- a/packages/console/src/assets/docs/guides/web-java-spring-boot/README.mdx +++ b/packages/console/src/assets/docs/guides/web-java-spring-boot/README.mdx @@ -220,7 +220,7 @@ public class WebSecurityConfig { - + (You may skip this step if you already have a home page in your project) @@ -254,7 +254,7 @@ This controller will redirect the user to the user page if the user is authentic - + Create a new controller to handle the user page: