Logto is an Auth0 alternative designed for modern apps and SaaS products. It offers both Cloud and Open-source services to help you quickly launch your identity and management (IAM) system. Enjoy authentication, authorization, and multi-tenant management all in one.
We recommend starting with a free development tenant on Logto Cloud. This allows you to explore all the features easily.
In this article, we will go through the steps to quickly build the Alipay (Native) sign-in experience (user authentication) with Java Spring Boot and Logto.
Prerequisites
- A running Logto instance. Check out the introduction page to get started.
- Basic knowledge of Java Spring Boot.
- A usable Alipay (Native) account.
Create an application in Logtoâ
Logto is based on OpenID Connect (OIDC) authentication and OAuth 2.0 authorization. It supports federated identity management across multiple applications, commonly called Single Sign-On (SSO).
To create your Traditional web application, simply follow these steps:
- Open the Logto Console. In the "Get started" section, click the "View all" link to open the application frameworks list. Alternatively, you can navigate to Logto Console > Applications, and click the "Create application" button.
- In the opening modal, click the "Traditional web" section or filter all the available "Traditional web" frameworks using the quick filter checkboxes on the left. Click the "Java Spring Boot" framework card to start creating your application.
- Enter the application name, e.g., "Bookstore," and click "Create application".
đ Ta-da! You just created your first application in Logto. You'll see a congrats page which includes a detailed integration guide. Follow the guide to see what the experience will be in your application.
Integrate Java Spring Boot with Logtoâ
- You may find the sample code for this guide in our spring-boot-sample github repository.
- No official SDK is required to integrate Logto with your Java Spring Boot application. We will use the Spring Security and Spring Security OAuth2 libraries to handle the OIDC authentication flow with Logto.
Configure your Java Spring Boot applicationâ
Adding dependenciesâ
For gradle users, add the following dependencies to your build.gradle
file:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
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'
}
For maven users, add the following dependencies to your pom.xml
file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
OAuth2 Client Configurationâ
Register a new Java Spring Boot
application in Logto Console and get the client credential and IdP configurations for your web application.
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={{YOUR_CLIENT_ID}}
spring.security.oauth2.client.registration.logto.client-secret={{YOUR_CLIENT_ID}}
spring.security.oauth2.client.registration.logto.redirect-uri={baseUrl}/login/oauth2/code/{registrationId}
spring.security.oauth2.client.registration.logto.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.logto.scope=openid,profile,offline_access
spring.security.oauth2.client.registration.logto.provider=logto
spring.security.oauth2.client.provider.logto.issuer-uri={{LOGTO_ENDPOINT}}/oidc
spring.security.oauth2.client.provider.logto.authorization-uri={{LOGTO_ENDPOINT}}/oidc/auth
spring.security.oauth2.client.provider.logto.jwk-set-uri={{LOGTO_ENDPOINT}}/oidc/jwks
Implementationâ
Before we dive into the details, here's a quick overview of the end-user experience. The sign-in process can be simplified as follows:
- Your app invokes the sign-in method.
- The user is redirected to the Logto sign-in page. For native apps, the system browser is opened.
- The user signs in and is redirected back to your app (configured as the redirect URI).
Regarding redirect-based sign-inâ
- This authentication process follows the OpenID Connect (OIDC) protocol, and Logto enforces strict security measures to protect user sign-in.
- If you have multiple apps, you can use the same identity provider (Logto). Once the user signs in to one app, Logto will automatically complete the sign-in process when the user accesses another app.
To learn more about the rationale and benefits of redirect-based sign-in, see Logto sign-in experience explained.
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.
Configure redirect URIsâ
Switch to the application details page of Logto Console. Add a redirect URI http://localhost:3000/callback
.

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 section.
Then click "Save" to save the changes.
Implement the WebSecurityConfigâ
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 for more details.
package com.example.securingweb;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
// ...
}
Create a idTokenDecoderFactory
beanâ
This is required because Logto uses ES384
as the default algorithm, we need to overwrite the default OidcIdTokenDecoderFactory
to use the same algorithm.
import org.springframework.context.annotation.Bean;
import org.springframework.security.oauth2.client.oidc.authentication.OidcIdTokenDecoderFactory;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm;
import org.springframework.security.oauth2.jwt.JwtDecoderFactory;
public class WebSecurityConfig {
// ...
@Bean
public JwtDecoderFactory<ClientRegistration> idTokenDecoderFactory() {
OidcIdTokenDecoderFactory idTokenDecoderFactory = new OidcIdTokenDecoderFactory();
idTokenDecoderFactory.setJwsAlgorithmResolver(clientRegistration -> SignatureAlgorithm.ES384);
return idTokenDecoderFactory;
}
}
Create a LoginSuccessHandler class to handle the login success eventâ
We will redirect the user to the /user
page after a successful login.
package com.example.securingweb;
import java.io.IOException;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
public class CustomSuccessHandler implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
response.sendRedirect("/user");
}
}
Create a LogoutSuccessHandler class to handle the logout success eventâ
Clear the session and redirect the user to the home page.
package com.example.securingweb;
import java.io.IOException;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
public class CustomLogoutHandler implements LogoutSuccessHandler {
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
HttpSession session = request.getSession();
if (session != null) {
session.invalidate();
}
response.sendRedirect("/home");
}
}
Update the WebSecurityConfig
class with a securityFilterChain
â
securityFilterChain is a chain of filters that are responsible for processing the incoming requests and responses.
We will configure the securityFilterChain
to allow access to the home page and require authentication for all other requests. Use the CustomSuccessHandler
and CustomLogoutHandler
to handle the login and logout events.
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.DefaultSecurityFilterChain;
public class WebSecurityConfig {
// ...
@Bean
public DefaultSecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests(authorizeRequests ->
authorizeRequests
.antMatchers("/", "/home").permitAll() // Allow access to the home page
.anyRequest().authenticated() // All other requests require authentication
)
.oauth2Login(oauth2Login ->
oauth2Login
.successHandler(new CustomSuccessHandler())
)
.logout(logout ->
logout
.logoutSuccessHandler(new CustomLogoutHandler())
);
return http.build();
}
}
Create a home pageâ
(You may skip this step if you already have a home page in your project)
package com.example.securingweb;
import java.security.Principal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping({ "/", "/home" })
public String home(Principal principal) {
return principal != null ? "redirect:/user" : "home";
}
}
This controller will redirect the user to the user page if the user is authenticated, otherwise, it will show the home page. Add a sign-in link to the home page.
<body>
<h1>Welcome!</h1>
<p><a th:href="@{/oauth2/authorization/logto}">Login with Logto</a></p>
</body>
Create a user pageâ
Create a new controller to handle the user page:
package com.example.securingweb;
import java.security.Principal;
import java.util.Map;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class UserController {
@GetMapping
public String user(Model model, Principal principal) {
if (principal instanceof OAuth2AuthenticationToken) {
OAuth2AuthenticationToken token = (OAuth2AuthenticationToken) principal;
OAuth2User oauth2User = token.getPrincipal();
Map<String, Object> attributes = oauth2User.getAttributes();
model.addAttribute("username", attributes.get("username"));
model.addAttribute("email", attributes.get("email"));
model.addAttribute("sub", attributes.get("sub"));
}
return "user";
}
}
Once the user is authenticated, we will retrieve the OAuth2User
data from the authenticated principal object. Please refer OAuth2AuthenticationToken and OAuth2User for more details.
Read the user data and pass it to the user.html
template.
<body>
<h1>User Details</h1>
<div>
<p>
<div><strong>name:</strong> <span th:text="${username}"></span></div>
<div><strong>email:</strong> <span th:text="${email}"></span></div>
<div><strong>id:</strong> <span th:text="${sub}"></span></div>
</p>
</div>
<form th:action="@{/logout}" method="post">
<input type="submit" value="Logout" />
</form>
</body>
Request additional claimsâ
You may find some user information are missing in the returned object from principal (OAuth2AuthenticationToken)
. This is because OAuth 2.0 and OpenID Connect (OIDC) are designed to follow the principle of least privilege (PoLP), and Logto is built on top of these standards.
By default, limited claims are returned. If you need more information, you can request additional scopes to access more claims.
A "claim" is an assertion made about a subject; a "scope" is a group of claims. In the current case, a claim is a piece of information about the user.
Here's a non-normative example the scope - claim relationship:
The "sub" claim means "subject", which is the unique identifier of the user (i.e. user ID).
Logto SDK will always request three scopes: openid
, profile
, and offline_access
.
To retrieve additional user information, you can add extra scopes to the application.properties
file. For example, to request the email
, phone
, and urn:logto:scope:organizations
scope, add the following line to the application.properties
file:
spring.security.oauth2.client.registration.logto.scope=openid,profile,offline_access,email,phone,urn:logto:scope:organizations
Then you can access the additional claims in the OAuth2User
object.
Run and test the applicationâ
Run the application and navigate to http://localhost:8080
.
- You will see the home page with a sign-in link.
- Click on the link to sign in with Logto.
- After successful authentication, you will be redirected to the user page with your user details.
- Click on the logout button to sign out. You will be redirected back to the home page.
Add Alipay (Native) connectorâ
To enable quick sign-in and improve user conversion, connect with Java Spring Boot as an identity provider. The Logto social connector helps you establish this connection in minutes by allowing several parameter inputs.
To add a social connector, simply follow these steps:
- Navigate to Console > Connectors > Social Connectors.
- Click "Add social connector" and select "Alipay (Native)".
- Follow the README guide and complete required fields and customize settings.

If you are following the in-place Connector guide, you can skip the next section.
Set up Alipay mobile appâ
Register Alipay developer accountâ
Register an Alipay developer account if you don't have one.
Create and configure Alipay appâ
- Sign in to the Alipay console with the account you have just registered.
- Go to "Web & Mobile Apps" (įŊéĄĩ&į§ģå¨åēį¨) tab in "My Application" (æįåēį¨) panel.
- Click "Create an App" (įĢåŗååģē) button to start configuring your application.
- Name your application in "Application Name" (åēį¨åį§°) following the naming conventions and upload your "Application Icon" (åēį¨åžæ ), make sure you choose "mobile application" (į§ģå¨åēį¨) as "App type" (åēį¨įąģå). For building iOS App, a unique "Bundle ID" is required. Also, "application signature" (åēį¨įžå) and "application package name" (åēį¨å å) are required for Android apps.
- After finishing creating the application, we come to the Overview page, where we should click "add ability" (æˇģå čŊå) to add "Third-party application authorization" (įŦŦ䏿šåē፿æ), "Get member information" (čˇåäŧåäŋĄæ¯) and "App Alipay login" (App æ¯äģåŽįģåŊ) before enabling Alipay sign-in.
- Go to Alipay Customer Center, and sign in with the Alipay developer account. Click "Account Center" (č´Ļåˇä¸åŋ) on the topbar and go to "APPID binding" (APPID įģåŽ), whose entrance can be found at the bottom of the sidebar. "Add binding" (æˇģå įģåŽ) by type in the APPID of the mobile application you just created in step 4.
- Click on "Sign" button of "App Alipay login", and finish signing process following the guide. After finishing this step, you are expected to find abilities you have just added in step 5 kicks in.
- Come back to Alipay open platform console page, and you can find "Interface signing method" (æĨåŖå įžæšåŧīŧå¯éĨ/č¯äšĻīŧ) in "development information" (åŧåäŋĄæ¯) section. Click "set up" (莞įŊŽ) button, and you can find yourself on a page setting signing method. "Public Key" (å ŦéĨ) is the preferred signing mode, and fill in contents from the public key file you have generated in the text input box.
- Set up "Authorization Redirect URI" (ææåč°å°å) by clicking "set up" (莞įŊŽ) button on the bottom of the Alipay console page.
${your_logto_origin}/callback/${connector_id}
is the default redirect URI used in Logto core. Theconnector_id
can be found on the top bar of the Logto Admin Console connector details page. - After finishing all these steps, go back to the top right corner of Alipay console page, and click "Submit for review" (æäē¤åŽĄæ ¸). Once the review is approved, you are good to go with a smooth Alipay sign-in flow.
You can use openssl to generate key pairs on your local machine by executing following code snippet in terminal.
openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -outform PEM -pubout -out public.pem
When filling in the public key on the Alipay app setup website, you need to remove the header and footer of public.pem
, delete all newline characters, and paste the rest of the contents into the text input box for "public key".
Set up the Logto Alipay Native connector settingsâ
- In the Alipay console workspace go to "My application" (æįåēį¨) panel and click "Web & Mobile Apps" (įŊéĄĩ&į§ģå¨åēį¨) tab, you can find APPID of all applications.
- In step 7 of the previous part, you have already generated a key pair including a private key and a public key.
- Fill out the Logto connector settings:
- Fill out the
appId
field with APPID you've got from step 1. - Fill out the
privateKey
field with contents from the private key file mentioned in step 2. Please MAKE SURE to use '\n' to replace all newline characters. You don't need to remove header and footer in private key file. - Fill out the
signType
filed with 'RSA2' due to thePublic key
signing mode we chose in step 7 of "Create And Configure Alipay Apps".
- Fill out the
Config typesâ
Name | Type | Enum values |
---|---|---|
appId | string | N/A |
privateKey | string | N/A |
signType | enum string | 'RSA' | 'RSA2' |
Enable Alipay native sign-in in your appâ
iOSâ
We assume you have integrated Logto iOS SDK in your app. In this case, things are pretty simple, and you don't even need to read the Alipay SDK doc:
1. Add LogtoSocialPluginAlipay
to your Xcode project
Add the framework:

The plugin includes Alipay "minimalist SDK" (æįŽį SDK). You can directly use import AFServiceSDK
once imported the plugin.
2. Add the plugin to your LogtoClient
init options
let logtoClient = LogtoClient(
useConfig: config,
socialPlugins: [LogtoSocialPluginAlipay(callbackScheme: "your-scheme")]
)
Where callbackScheme
is one of the custom URL Schemes that can navigate to your app.
Androidâ
We assume you have integrated Logto Android SDK in your app. In this case, things are pretty simple, and you don't even need to read the Alipay SDK doc:
1. Download the Alipay "minimalist SDK" and add it to your project
Download the Alipay "minimalist SDK" (æįŽį SDK) from Logto 3rd-party Social SDKs to your project's app/libs
folder:
project-path/app/libs/alipaySdk-15.7.9-20200727142846.aar
2. Add the Alipay "minimalist SDK" as a dependency
Open your build.gradle
file:
project-path/app/build.gradle
Add the dependency:
dependencies {
// ...
implementation(files("./libs/alipaySdk-15.7.9-20200727142846.aar")) // kotlin-script
// or
implementation files('./libs/alipaySdk-15.7.9-20200727142846.aar') // groovy-script
}
Save your configurationâ
Double check you have filled out necessary values in the Logto connector configuration area. Click "Save and Done" (or "Save changes") and the Alipay (Native) connector should be available now.
Enable Alipay (Native) connector in Sign-in Experienceâ
Once you create a social connector successfully, you can enable it as a "Continue with Alipay (Native)" button in Sign-in Experience.
- Navigate to Console > Sign-in experience > Sign-up and sign-in.
- (Optional) Choose "Not applicable" for sign-up identifier if you need social login only.
- Add configured Alipay (Native) connector to the "Social sign-in" section.

Testing and Validationâ
Return to your Java Spring Boot app. You should now be able to sign in with Alipay (Native). Enjoy!
Further readingsâ
End-user flows: Logto provides a out-of-the-box authentication flows including MFA and enterprise SSO, along with powerful APIs for flexible implementation of account settings, security verification, and multi-tenant experience.
Authorization: Authorization defines the actions a user can do or resources they can access after being authenticated. Explore how to protect your API for native and single-page applications and implement Role-based Access Control (RBAC).
Organizations: Particularly effective in multi-tenant SaaS and B2B apps, the organization feature enable tenant creation, member management, organization-level RBAC, and just-in-time-provisioning.
Customer IAM series Our serial blog posts about Customer (or Consumer) Identity and Access Management, from 101 to advanced topics and beyond.