casdoor / casdoor-java-sdk

Java client SDK for Casdoor
https://github.com/casdoor/casdoor
Apache License 2.0
27 stars 43 forks source link
auth authentication authn casdoor java jwt keycloak oauth oidc sdk sso

casdoor-java-sdk

GitHub Actions codebeat badge codecov Javadocs Maven Central Release Discord

This is Casdoor's SDK for java, which will allow you to easily connect your application to the Casdoor authentication system without having to implement it from scratch.

Casdoor SDK is very simple to use. We will show you the steps below.

Step1. Init Config

Initialization requires 5 parameters, which are all string type:

Name (in order) Must Description
endpoint Yes Casdoor Server Url, such as http://localhost:8000
clientId Yes Client ID for the Casdoor application
clientSecret Yes Client secret for the Casdoor application
certificate Yes The public key for the Casdoor application's cert
organizationName Yes The name for the Casdoor organization
applicationName No The name for the Casdoor application
CasdoorConfig config = new CasdoorConfig(endpoint, clientId, clientSecret, certificate, organizationName, applicationName);

Step2. Get Service and use

Now provide two services: CasdoorUserService, CasdoorAuthService

You can create them like

CasdoorUserService casdoorUserService = new CasdoorUserService(config);

UserService

CasdoorUserService support basic user operations, like:

AuthService

  1. Get token and parse

After casdoor verification passed, it will be redirected to your application with code and state, like http://forum.casbin.org?code=xxx&state=yyyy.

Your web application can get the code,state and call GetOAuthToken(code, state), then parse out jwt token.

The general process is as follows:

String token = authService.getOAuthToken(code, state);

CasdoorUser user = authService.parseJwtToken(token);
  1. Set Session in your app

user contains the basic information about the user provided by casdoor, you can use it as a keyword to set the session in your application, like this:

HttpSession session = request.getSession();
session.setAttribute("user", user);

SpringBoot Support

If you use SpingBoot for your application, you can use casdoor-spring-boot-starter