跳到主内容

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。

<!-- 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>

步骤3:初始化配置

初始化需要按以下顺序的6个字符串类型参数:

名称必需的描述
endpointCasdoor 服务器 URL,例如 http://localhost:8000
clientId应用程序客户端ID
clientSecret应用程序客户端密钥
certificate应用程序证书
organizationName应用程序组织
applicationName应用程序名称
您可以使用Java属性或YAML文件进行初始化。
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实例,特别是clientIdclientSecretjwtPublicKey

步骤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");
}

步骤5:获取令牌并解析

在通过Casdoor验证后,它将带着代码和状态重定向回您的应用程序。

您可以获取代码并调用getOAuthToken方法,然后解析JWT令牌。

Casdoor User 包含了由Casdoor提供的有关用户的基本信息。 您可以使用它来设置应用程序中的会话。

@RequestMapping("login")
public String login(String code, String state, HttpServletRequest request) {
String token = "";
CasdoorUser user = null;
try {
token = casdoorAuthService.getOAuthToken(code, state);
user = casdoorAuthService.parseJwtToken(token);
} catch (CasdoorAuthException e) {
e.printStackTrace();
}
HttpSession session = request.getSession();
session.setAttribute("casdoorUser", user);
return "redirect:/";
}

服务

以下是API的示例:

  • CasdoorAuthService
    • String token = casdoorAuthService.getOAuthToken(code, "app-built-in");
    • CasdoorUser casdoorUser = casdoorAuthService.parseJwtToken(token);
  • CasdoorUserService
    • CasdoorUser casdoorUser = casdoorUserService.getUser("admin");
    • CasdoorUser casdoorUser = casdoorUserService.getUserByEmail("admin@example.com");
    • CasdoorUser[] casdoorUsers = casdoorUserService.getUsers();
    • CasdoorUser[] casdoorUsers = casdoorUserService.getSortedUsers("created_time", 5);
    • int count = casdoorUserService.getUserCount("0");
    • CasdoorResponse response = casdoorUserService.addUser(user);
    • CasdoorResponse response = casdoorUserService.updateUser(user);
    • CasdoorResponse response = casdoorUserService.deleteUser(user);
  • CasdoorEmailService
    • CasdoorResponse response = casdoorEmailService.sendEmail(title, content, sender, receiver);
  • CasdoorSmsService
    • CasdoorResponse response = casdoorSmsService.sendSms(randomCode(), receiver);
  • CasdoorResourceService
    • CasdoorResponse response = casdoorResourceService.uploadResource(user, tag, parent, fullFilePath, file);
    • CasdoorResponse response = casdoorResourceService.deleteResource(file.getName());

更多资源

您可以探索以下项目/文档,以了解更多关于将Java与Casdoor集成的信息: