Spring Boot
casdoor-spring-boot-example是一个如何在Spring Boot项目中使用casdoor-spring-boot-starter的示例。 我们将引导您完成以下步骤。
步骤1:部署Casdoor
Deploy Casdoor in production mode. See Server installation. Ensure the server is reachable and you can sign in (e.g. admin / 123).
步骤2:导入casdoor-spring-boot-starter
您可以使用Maven或Gradle导入casdoor-spring-boot-starter。
- Maven
- Gradle
<!-- https://mvnrepository.com/artifact/org.casbin/casdoor-spring-boot-starter -->
<dependency>
<groupId>org.casbin</groupId>
<artifactId>casdoor-spring-boot-starter</artifactId>
<version>1.x.y</version>
</dependency>
// https://mvnrepository.com/artifact/org.casbin/casdoor-spring-boot-starter
implementation group: 'org.casbin', name: 'casdoor-spring-boot-starter', version: '1.x.y'
步骤3:初始化配置
初始化需要按以下顺序的6个字符串类型参数:
| 名称 | 必需的 | 描述 |
|---|---|---|
| endpoint | 是 | Casdoor 服务器 URL,例如 http://localhost:8000 |
| clientId | 是 | 应用程序客户端ID |
| clientSecret | 是 | 应用程序客户端密钥 |
| certificate | 是 | 应用程序证书 |
| organizationName | 是 | 应用程序组织 |
| applicationName | 否 | 应用程序名称 |
| 您可以使用Java属性或YAML文件进行初始化。 |
- Properties
- YML
casdoor.endpoint = http://localhost:8000
casdoor.clientId = <client-id>
casdoor.clientSecret = <client-secret>
casdoor.certificate = <certificate>
casdoor.organizationName = built-in
casdoor.applicationName = app-built-in
casdoor:
endpoint: http://localhost:8000
client-id: <client-id>
client-secret: <client-secret>
certificate: <certificate>
organization-name: built-in
application-name: app-built-in
注意事项
将配置值替换为您自己的Casdoor实例,特别是clientId,clientSecret和jwtPublicKey。
步骤4:重定向到登录页面
To authenticate users, redirect them to Casdoor’s login page (with the target URL as needed).
Make sure you have added the callback URL (e.g. http://localhost:8080/login) in the application configuration beforehand.
@Resource
private CasdoorAuthService casdoorAuthService;
@RequestMapping("toLogin")
public String toLogin() {
return "redirect:" + casdoorAuthService.getSigninUrl("http://localhost:8080/login");
}