{"id":20215705,"url":"https://github.com/casdoor/casdoor-spring-boot-example","last_synced_at":"2025-04-10T14:41:56.548Z","repository":{"id":43000895,"uuid":"420726240","full_name":"casdoor/casdoor-spring-boot-example","owner":"casdoor","description":"Spring Boot example app for Casdoor based on casdoor-spring-boot-starter","archived":false,"fork":false,"pushed_at":"2023-11-08T03:51:17.000Z","size":50,"stargazers_count":8,"open_issues_count":0,"forks_count":12,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-24T13:11:12.379Z","etag":null,"topics":["auth","authn","casdoor","iam","java","oauth2","spring","spring-boot","sso"],"latest_commit_sha":null,"homepage":"https://github.com/casdoor/casdoor-spring-boot-starter","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/casdoor.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-10-24T15:46:43.000Z","updated_at":"2025-03-18T05:34:39.000Z","dependencies_parsed_at":"2023-11-08T07:34:00.787Z","dependency_job_id":null,"html_url":"https://github.com/casdoor/casdoor-spring-boot-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casdoor%2Fcasdoor-spring-boot-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casdoor%2Fcasdoor-spring-boot-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casdoor%2Fcasdoor-spring-boot-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/casdoor%2Fcasdoor-spring-boot-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/casdoor","download_url":"https://codeload.github.com/casdoor/casdoor-spring-boot-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248233998,"owners_count":21069494,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["auth","authn","casdoor","iam","java","oauth2","spring","spring-boot","sso"],"created_at":"2024-11-14T06:24:04.615Z","updated_at":"2025-04-10T14:41:56.520Z","avatar_url":"https://github.com/casdoor.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# casdoor-spring-boot-example\n\nThis is an example on how to use `casdoor-spring-boot-starter` in SpringBoot project. We will show you the steps below.\n\n## What you need\n\nThe Casdoor should be deployed.\n\nYou can refer to the Casdoor official documentation for the [install guide](https://casdoor.org/docs/basic/server-installation). Please deploy your Casdoor instance in **production mode**.\n\nAfter a successful deployment, you need to ensure:\n\n- Open your favorite browser and visit **http://localhost:8000**, you will see the login page of Casdoor.\n- Input `admin` and `123` to test login functionality is working fine.\n\n## Quickstart\n\n### Include the dependency\n\nAdd ```casdoor-spring-boot-starter``` to the Spring Boot project.\n\nFor Apache Maven:\n\n```Maven\n\u003c!-- https://mvnrepository.com/artifact/org.casbin/casdoor-spring-boot-starter --\u003e\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.casbin\u003c/groupId\u003e\n    \u003cartifactId\u003ecasdoor-spring-boot-starter\u003c/artifactId\u003e\n    \u003cversion\u003e1.x.y\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\nFor Gradle:\n\n```gradle\n// https://mvnrepository.com/artifact/org.casbin/casdoor-spring-boot-starter\nimplementation group: 'org.casbin', name: 'casdoor-spring-boot-starter', version: '1.x.y'\n```\n\n### Configure your properties\n\nInitialization requires 6 parameters, which are all string type.\n\n| Name (in order)  | Must | Description                                         |\n|------------------|------|-----------------------------------------------------|\n| endpoint         | Yes  | Casdoor Server Url, such as `http://localhost:8000` |\n| clientId         | Yes  | Application.client_id                               |\n| clientSecret     | Yes  | Application.client_secret                           |\n| certificate      | Yes  | The public key for the Casdoor application's cert   |\n| organizationName | Yes  | Application.organization                            |\n| applicationName  | No   | Application.name                                    |\n\nYou can use Java properties or YAML files to init as below.\n\nFor properties:\n\n```properties\ncasdoor.endpoint = http://localhost:8000\ncasdoor.clientId = \u003cclient-id\u003e\ncasdoor.clientSecret = \u003cclient-secret\u003e\ncasdoor.certificate = \u003ccertificate\u003e\ncasdoor.organizationName = built-in\ncasdoor.applicationName = app-built-in\n```\n\nFor yaml:\n\n```yaml\ncasdoor:\n  endpoint: http://localhost:8000\n  client-id: \u003cclient-id\u003e\n  client-secret: \u003cclient-secret\u003e\n  certificate: \u003ccertificate\u003e\n  organization-name: built-in\n  application-name: app-built-in\n```\n\n### Get the Service and use\n\n\nNow provide 5 services: `CasdoorAuthService`, `CasdoorUserService`, `CasdoorEmailService`, `CasdoorSmsService` and `CasdoorResourceService`.\n\nYou can create them as below in SpringBoot project.\n\n```java\n@Resource\nprivate CasdoorAuthService casdoorAuthService;\n```\n\nWhen you need the authentication who access your app, you can send the target url and redirect to the login page provided by Casdoor.\n\nPlease be sure that you have added the callback url (e.g. http://localhost:8080/callback) in application configuration in advance.\n\n```java\n@RequestMapping(\"login\")\npublic String login() {\n    return \"redirect:\" + casdoorAuthService.getSigninUrl(\"http://localhost:8080/callback\");\n}\n```\n\nAfter Casdoor verification passed, it will be redirected to your application with code and state.\n\nYou can get the code and call `getOAuthToken` method, then parse out jwt token.\n\n`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.\n\n```java\n@RequestMapping(\"callback\")\npublic String callback(String code, String state, HttpSession session) {\n    String token = \"\";\n    CasdoorUser user = null;\n    try {\n        token = casdoorAuthService.getOAuthToken(code, state);\n        user = casdoorAuthService.parseJwtToken(token);\n    } catch (CasdoorAuthException e) {\n        e.printStackTrace();\n    }\n    session.setAttribute(\"casdoorUser\", user);\n    return \"redirect:/\";\n}\n```\n\nExamples of APIs are shown below.\n\n- CasdoorAuthService\n    - `String token = casdoorAuthService.getOAuthToken(code, \"app-built-in\");`\n    - `CasdoorUser casdoorUser = casdoorAuthService.parseJwtToken(token);`\n- CasdoorUserService\n    - `CasdoorUser casdoorUser = casdoorUserService.getUser(\"admin\");`\n    - `CasdoorUser casdoorUser = casdoorUserService.getUserByEmail(\"admin@example.com\");`\n    - `CasdoorUser[] casdoorUsers = casdoorUserService.getUsers();`\n    - `CasdoorUser[] casdoorUsers = casdoorUserService.getSortedUsers(\"created_time\", 5);`\n    - `int count = casdoorUserService.getUserCount(\"0\");`\n    - `CasdoorResponse response = casdoorUserService.addUser(user);`\n    - `CasdoorResponse response = casdoorUserService.updateUser(user);`\n    - `CasdoorResponse response = casdoorUserService.deleteUser(user);`\n- CasdoorEmailService\n    - `CasdoorResponse response = casdoorEmailService.sendEmail(title, content, sender, receiver);`\n- CasdoorSmsService\n    - `CasdoorResponse response = casdoorSmsService.sendSms(randomCode(), receiver);`\n- CasdoorResourceService\n    - `CasdoorResponse response = casdoorResourceService.uploadResource(user, tag, parent, fullFilePath, file);`\n    - `CasdoorResponse response = casdoorResourceService.deleteResource(file.getName());`\n\n## What's more\n\nYou can explore the following projects/docs to learn more about the integration of Java with Casdoor.\n\n- [casdoor-java-sdk](https://github.com/casdoor/casdoor-java-sdk)\n- [casdoor-spring-boot-starter](https://github.com/casdoor/casdoor-spring-boot-starter)\n- [casdoor-spring-boot-security-example](https://casdoor.org/docs/category/spring-security)\n- [casdoor-spring-boot-shiro-example](https://github.com/casdoor/casdoor-spring-boot-shiro-example)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasdoor%2Fcasdoor-spring-boot-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcasdoor%2Fcasdoor-spring-boot-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcasdoor%2Fcasdoor-spring-boot-example/lists"}