An example Spring Security app that signs users in with Casdoor over OAuth 2.0 / OpenID Connect, using casdoor-spring-boot-starter.
- casdoor-spring-boot-starter reads the
casdoor.*properties and registers a Spring Security OAuth2 client namedcasdoor(endpoints from Casdoor's OIDC discovery, scopesopenid profile email). - SecurityConfig keeps
/public, protects everything else and turns onoauth2Login(). - Visiting
/fooswithout a session redirects to/oauth2/authorization/casdoorand then to the Casdoor sign-in page. - Casdoor redirects back to
http://localhost:8080/login/oauth2/code/casdoor. Spring Security checks the state, exchanges the code for tokens and verifies the ID token. - Controllers get the user as an
OidcUser(@AuthenticationPrincipal), its name is the Casdoor username (preferred_username). POST /logoutends the Spring session and calls Casdoor's logout API with the access token (AuthService.logoutCurrentSession()), so the next sign-in asks for the password again.
- Java 17+
- Maven 3.9+ (or your IDE)
- 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:8080/login/oauth2/code/casdoor to the application's Redirect URLs. Then fill in 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 applicationThe standard spring.security.oauth2.client.* properties still work and override what the starter sets, for example spring.security.oauth2.client.registration.casdoor.scope.
git clone https://github.com/casdoor/casdoor-spring-security-example
cd casdoor-spring-security-example
mvn spring-boot:runOpen http://localhost:8080 and click Login. On the demo server, sign in with username admin and password 123.
Run the tests:
mvn verify
