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.
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 casdoorConfig = new CasdoorConfig(endpoint, clientId, clientSecret, certificate, organizationName, applicationName);
Now provide two services: CasdoorUserService
, CasdoorAuthService
You can create them like
CasdoorUserService casdoorUserService = new CasdoorUserService(casdoorConfig);
CasdoorUserService
support basic user operations, like:
GetUser(name string)
, get one user by user name.GetUsers()
, get all users.UpdateUser(auth.User)/AddUser(auth.User)/DeleteUser(auth.User)
, write user to database.
- 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 = casdoorAuthService.getOAuthToken(code, state);
CasdoorUser casdoorUser = casdoorAuthService.parseJwtToken(token);
- Set Session in your app
casdoorUser
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);
If you use SpingBoot for your application, you can use casdoor-spring-boot-starter