jpree-sso.mp4 (8.44MB)

环境说明

  • pig 2.10
  • jpress 3.0

    代码新增

  • _AdminController 新增 SSO 端点

注意:授权码模式下回调地址不能使用localhost,可以使用127.0.0.1

  1. @Clear(PermissionInterceptor.class)
  2. public void sso(String code) {
  3. if (StrUtil.isBlank(code)){
  4. String url = String.format("%s?response_type=code&scope=%s&client_id=%s&state=%s&redirect_uri=%s",
  5. "http://192.168.0.33:3000/oauth/authorize",
  6. "server",
  7. "jfinal",
  8. "jfinal",
  9. URLEncoder.encode("http://127.0.0.1:8080/admin/sso"));
  10. redirect(url);
  11. return;
  12. }
  13. String template = "http://192.168.0.33:3000/oauth/token?grant_type=authorization_code&scope=%s&code=%s&redirect_uri=%s";
  14. final String url = String.format(template, "server", code, URLEncoder.encode("http://127.0.0.1:8080/admin/sso"));
  15. String body = HttpRequest.get(url)
  16. .basicAuth("jfinal", "jfinal")
  17. .execute()
  18. .body();
  19. JSONObject parse = JSONUtil.parseObj(body);
  20. String username = parse.getStr("username");
  21. //登录并创建token
  22. User user = userService.findFistByUsername(username);
  23. Ret ret = Ret.ok().set("user_id", user.getId());
  24. if (ret.isOk()) {
  25. SessionUtils.record(user.getId());
  26. CookieUtil.put(this, JPressConsts.COOKIE_UID, user.getId());
  27. }
  28. redirect("/admin");
  29. }
  • AdminInterceptor 暴露此端点

image.png

前端使用

  1. http://127.0.0.1:8080/admin/sso

退出功能

image.png

❤ 问题咨询

手势点击蓝字求关注简约风动态引导关注__2022-09-07+23_18_38.gif