GithubHelp home page GithubHelp logo

casdoor / casdoor-java-sdk Goto Github PK

View Code? Open in Web Editor NEW
25.0 4.0 37.0 227 KB

Java client SDK for Casdoor

Home Page: https://github.com/casdoor/casdoor

License: Apache License 2.0

Java 100.00%
java oauth auth authn authentication sso oidc casdoor keycloak jwt

casdoor-java-sdk's Introduction

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:

  • 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.

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

casdoor-java-sdk's People

Contributors

abingcbc avatar bsheepcoder avatar caoshengdong avatar conghuhu avatar cwp0 avatar d1zzzy1 avatar ebreak avatar gtn1024 avatar hsluoyz avatar jakiuncle avatar resulte avatar selflocking avatar seriouszyx avatar tangyang9464 avatar towerhe avatar windydante avatar wintbiit avatar zerek-cheng avatar zhuying1999 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

casdoor-java-sdk's Issues

There is no api "/api/login/oauth/authorize"

Dear,

I tested to integrate use java sdk. It has error 403 when get the token. I checked, it dose not have api: "/api/login/oauth/authorize" Please check again help me.

Thanks!
image

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can fix this 💪.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here are some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


Deployment to maven failed.

The deployment to maven failed for an unknown reason.

Please check the logs on the CI server to see what happened.


Good luck with your project ✨

Your semantic-release bot 📦🚀

Access token not found in JWT payload

CleanShot 2023-08-08 at 22 36 54@2x Is there any issue with my jwttoken ? i'm upgrade the spring-starter version to 1.9.0 and the sdk version is 1.16.1

parse the jwttoken it has 60 size claims
but haven't access_token
#54 #53 #

[feature] How to get group tree? maybe need to add API

Similar to: casdoor/casdoor-nodejs-sdk#53

The group tree means this part: https://door.casdoor.com/trees/built-in

image

The group tree mainly relies on the data returned by the get-groups API. But the returned data is a list. The Casdoor frontend code will build the tree from the list. We may need to do so too. There are two ways:

  1. Build the tree in Casdoor Go backend: https://github.com/casdoor/casdoor/blob/master/controllers/group.go#L31
  2. Build the tree in the SDK (like this casdoor-java-sdk)

image

Is a more generic service required?

I found a lot of duplicated codes in each services. IMO we can provide a more generic service ( Maybe named CasdoorCrudService ) to achive the common crud actions of entities.

public class CasdoorCrudService<T> extends CasdoorService {
    public CasdoorResponse<List<T>, Integer> query(String action, Map<String, String> queryParams) { }
    
    public T get(String action) { }

    public T get(String action, String id) {}

    public boolean create(String action, T t) {}

    public boolean update(String action, String id, T t) {}

    public boolean delete(String action, T t) {}
}

why not check the exp time when parse token

The api "parseJwtToken" need return the JWT exp time or check the exp.
The jwt exp time setted is no use now.

public User parseJwtToken(String token) {
// parse jwt token
SignedJWT parseJwt = null;
try {
parseJwt = SignedJWT.parse(token);
} catch (ParseException e) {
throw new AuthException("Cannot parse jwt token.", e);
}
// verify the jwt public key
try {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(config.certificate.getBytes()));
RSAPublicKey publicKey = (RSAPublicKey) cert.getPublicKey();
JWSVerifier verifier = new RSASSAVerifier(publicKey);
boolean verify = parseJwt.verify(verifier);
if (!verify) {
throw new AuthException("Cannot verify signature.");
}
} catch (CertificateException | JOSEException e) {
throw new AuthException("Cannot verify signature.", e);
}
// read "access_token" from payload and convert to CasdoorUser
try {
JWTClaimsSet claimsSet = parseJwt.getJWTClaimsSet();
String userJson = claimsSet == null ? null : claimsSet.toString();
if (userJson == null || userJson.isEmpty()) {
throw new AuthException("Cannot get claims from JWT payload");
}
return objectMapper.readValue(userJson, User.class);
} catch (JsonProcessingException | java.text.ParseException e) {
throw new AuthException("Cannot convert claims to User", e);
}
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.