# LY码本录 实现登录授权 *码本API* 我们准备了一个案例,用于告知您如何使用 LY API 实现 三方登录授权功能! ## 目录 [TOC] ## 开始 我们现在就开始教大家如何调用 码本录 的登录授权嗷! ### 申请一个服务名称 服务名称是您的系统的一个标识,用于和其它系统区分开!您可以 [点击这里](mailto:public@lingyuzhao.top?subject=【LY码本录】《申请一个新的服务名称》) 发送邮件给我们然后申请哦! ### 开始开发 > 开发逻辑非常简单,我们在这里准备了一个简单的例子! #### 引入码本API maven ```xml <dependency> <groupId>io.github.BeardedManZhao</groupId> <artifactId>CodeBookApi</artifactId> <version>1.13</version> </dependency> ``` #### 开始开发控制器 ```java package top.lingyuzhao.codeBookShop.controller; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpSession; import org.apache.http.client.HttpClient; import org.apache.http.impl.client.HttpClients; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import top.lingyuzhao.codeBook.api.lyMbl.LyMblApi; import top.lingyuzhao.codeBook.api.lyMbl.ThirdPartyAuth; import java.io.UnsupportedEncodingException; import java.util.UUID; /** * 一个简易的测试控制器 * @author 赵凌宇 */ @Controller @RestController public class TestController { private String random; private final HttpClient httpClient; public TestController() { this.httpClient = HttpClients.createDefault(); } // 这个是为了可以让系统能够正常接受这个对象,直接继承一下就好了,其它的不用做 public static class ThirdAuth extends ThirdPartyAuth { public ThirdAuth(String systemUserID, String serviceProviderName, String thirdPartyUserID, String thirdPartyKey) { super(systemUserID, serviceProviderName, thirdPartyUserID, thirdPartyKey); } } /** * 在这模拟一个用户访问时需要登录的例子 * @param httpServletRequest 请求对象 * @param httpServletResponse 响应对象 * @return 回复数据 */ @GetMapping("/home") @ResponseBody public String login(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){ // 查看会话中是否有第三方认证对象 HttpSession session = httpServletRequest.getSession(); if (session.getAttribute("thirdPartyAuth") != null) { System.out.println("用户已经登录了 在这里可以开始使用这个三方数据构建数据库中的用户信息"); // 获取第三方认证对象 ThirdPartyAuth thirdPartyAuth = (ThirdPartyAuth) session.getAttribute("thirdPartyAuth"); // 获取到此认证对象所属用户在码本中的id System.out.println(thirdPartyAuth.getSystemUserID()); // 获取三方用户ID 也就是当前系统可以使用的 id 和码本录可以分离,同一个码本账号的三方ID 是有固定值,但是却不同的 System.out.println(thirdPartyAuth.getThirdPartyUserID()); // 获取到三方密钥 也就是当前系统可以使用的 key 和码本录可以分离 System.out.println(thirdPartyAuth.getThirdPartyKey()); return "您好:" + thirdPartyAuth.getThirdPartyUserID(); } else { System.out.println("用户还没有登录 在这里开始演示如何授权"); // 首先准备一个随机值 作为回调地址的校验 random = UUID.randomUUID().toString(); // 调用码本API 构建一个授权链接 第一个参数是当前系统的标识符,第二个参数是授权成功后的回调地址 String codeBook = LyMblApi.getLoginAuthorizationLink( "codeBookAPI", // 这个回调地址中 建议使用一个随机的值 作为校验 "http://localhost:8080/login/" + random ); // 直接跳转 httpServletResponse.setStatus(302); httpServletResponse.setHeader("Location", codeBook); return codeBook; } } @PostMapping("/login/{sk}") @ResponseBody public String test(HttpServletRequest httpServletRequest, @PathVariable("sk") String sk, ThirdAuth thirdPartyAuth) throws UnsupportedEncodingException { // 这里是回调地址,在这里我们可以直接将 sk 进行校验了,防止非法的请求 if (sk == null || !sk.equals(random) || !LyMblApi.checkThirdPartyAuth(httpClient, thirdPartyAuth)){ // 如果发现这个链接 和 上面链接 中的随机值 不一致,则直接返回非法请求 return "非法请求"; } // 校验通过之后 建议立刻将随机值失效 random = null; // 然后我们就可以进行 thirdPartyAuth 的使用啦!在这里我们是直接将他们设置到 session 中 System.out.println(httpServletRequest.getRequestURL()); System.out.println(thirdPartyAuth); // 获取当前会话 HttpSession session = httpServletRequest.getSession(); // 设置会话的最大不活动时间为30分钟(1800秒) session.setMaxInactiveInterval(1800); // 将第三方认证对象存储在会话中 session.setAttribute("thirdPartyAuth", thirdPartyAuth); return "授权成功了,您可以看看 home 页面是否有变化"; } } ``` ### 测试 访问 `http://localhost:8080/home` 就可以出发授权了!  当授权操作成功,您应该会看到 `test` 控制器给您返回的 `授权成功了,您可以看看 home 页面是否有变化` 字符串!  这个时候,您再一次访问 `http://localhost:8080/home` 就可以看到,系统已经收到了您的身份信息!  ------ ***操作记录*** 作者:[zhao](https://www.lingyuzhao.top//index.html?search=4 "zhao") 操作时间:2025-02-01 19:37:34 星期六 【时区:UTC 8】 事件描述备注:保存/发布 中国 天津 [](如果不需要此记录可以手动删除,每次保存都会自动的追加记录)