An example app that signs users in with Casdoor: a React frontend, and a Spring Security backend that accepts Casdoor access tokens. It also shows silent sign-in.
| Part | SDK | Language | Port |
|---|---|---|---|
| Frontend | casdoor-react-sdk, casdoor-js-sdk | JavaScript + React | 3000 |
| Backend | casdoor-spring-boot-starter | Java + Spring Security 6 | 8080 |
- Casdoor Login sends the user to the Casdoor sign-in page (
CasdoorSDK.getSigninUrl()); the randomstatein the URL is kept in sessionStorage. - Casdoor redirects back to
http://localhost:3000/callback.AuthCallbackof casdoor-react-sdk checks the state and posts the code to the backend:POST /api/signin?code=...&state=.... - The backend exchanges the code for an access token (
AuthService.getOAuthToken()) and returns it. The frontend keeps it in localStorage. - The frontend calls the other APIs with
Authorization: Bearer <access token>. The backend is an OAuth2 resource server: Spring Security verifies the token (a JWT) against Casdoor's JWKS, casdoor-spring-boot-starter sets that up from thecasdoor.*properties. - Logout removes the token and calls
POST /api/logout, which ends the Casdoor session (AuthService.logoutCurrentSession()).
Silent sign-in: open http://localhost:3000/?silentSignin=1 while you are signed in to Casdoor in the same browser. SilentSignin signs in through a hidden iframe without showing the Casdoor page.
| API | Auth | Description |
|---|---|---|
POST /api/signin |
public | Exchanges the code for an access token |
GET /api/userinfo |
bearer token | Returns the claims of the token, i.e. the user |
POST /api/logout |
bearer token | Ends the Casdoor session of the token |
- Java 17+ and Maven 3.9+
- Node.js 18+ and Yarn
- A Casdoor server. The example is preconfigured for the public demo server https://door.casdoor.com, so it runs as is. To use your own, see Casdoor installation.
Skip this section to try the example with the public demo server.
In your Casdoor, create (or reuse) an organization and an application, and add http://localhost:3000/callback to the application's Redirect URLs. Then fill in both parts:
src/main/resources/application.yml:
casdoor:
endpoint: https://door.casdoor.com # Casdoor server URL
client-id: 294b09fbc17f95daf2fe # client ID of the application
client-secret: dd8982f7046ccba1bbd7851d5c1ece4e52bf039d # client secret of the application
organization-name: casbin # organization of the application
application-name: app-vue-python-example # name of the application
# the React frontend, allowed to call the APIs (CORS)
frontend-url: http://localhost:3000export const ServerUrl = "http://localhost:8080"; // the backend
const sdkConfig = {
serverUrl: "https://door.casdoor.com", // Casdoor server URL
clientId: "294b09fbc17f95daf2fe", // client ID of the application
appName: "app-vue-python-example", // name of the application
organizationName: "casbin", // organization of the application
redirectPath: "/callback",
signinPath: "/api/signin",
};git clone https://github.com/casdoor/casdoor-spring-security-react-example
cd casdoor-spring-security-react-examplemvn spring-boot:runcd web
yarn install
yarn startOpen http://localhost:3000 and click Casdoor Login. On the demo server, sign in with username admin and password 123.
Run the tests:
mvn verify
cd web && yarn test