From 20f10d1d8ddf16429b2c522ba5dd50307d6f5c93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B7=E5=86=B7?= <2270033969@qq.com> Date: Wed, 13 Nov 2024 15:40:40 +0800 Subject: [PATCH] =?UTF-8?q?refactor(satoken):=20=E5=A2=9E=E5=8A=A0=20conte?= =?UTF-8?q?xt-path=20=E5=8D=95=E4=BD=93=E6=9E=B6=E6=9E=84=E4=B8=8B?= =?UTF-8?q?=E4=B9=9F=E6=94=AF=E6=8C=81=E6=8E=88=E6=9D=83=E7=A0=81=E8=B7=B3?= =?UTF-8?q?=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pig/auth/endpoint/PigTokenEndpoint.java | 10 +++++--- .../support/handle/ConfirmViewHandle.java | 24 ++++++++++++------- .../support/handle/NoLoginViewHandle.java | 5 +++- .../src/main/resources/templates/confirm.ftl | 8 +++---- .../src/main/resources/templates/login.ftl | 2 +- 5 files changed, 32 insertions(+), 17 deletions(-) diff --git a/pig-auth/src/main/java/com/pig4cloud/pig/auth/endpoint/PigTokenEndpoint.java b/pig-auth/src/main/java/com/pig4cloud/pig/auth/endpoint/PigTokenEndpoint.java index 4add75e0..cfb68427 100644 --- a/pig-auth/src/main/java/com/pig4cloud/pig/auth/endpoint/PigTokenEndpoint.java +++ b/pig-auth/src/main/java/com/pig4cloud/pig/auth/endpoint/PigTokenEndpoint.java @@ -121,7 +121,9 @@ public class PigTokenEndpoint { } // 如果未登录,跳转到登录页面 - return ResponseEntity.ok(noLoginViewHandle.get(Map.of("error", saResult.getMsg()))); + Map model = new HashMap<>(); + model.put("error", saResult.getMsg()); + return ResponseEntity.ok(noLoginViewHandle.get(model)); } /** @@ -146,7 +148,9 @@ public class PigTokenEndpoint { } // 如果未登录,跳转到登录页面 - return ResponseEntity.ok(noLoginViewHandle.get(Map.of("error", saResult.getMsg()))); + Map model = new HashMap<>(); + model.put("error", saResult.getMsg()); + return ResponseEntity.ok(noLoginViewHandle.get(model)); } /** @@ -156,7 +160,7 @@ public class PigTokenEndpoint { @RequestMapping("/oauth2/logout") public ResponseEntity oauth2Logout() { StpUtil.logout(); - return ResponseEntity.ok(noLoginViewHandle.get(Map.of())); + return ResponseEntity.ok(noLoginViewHandle.get().toString()); } /** diff --git a/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/handle/ConfirmViewHandle.java b/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/handle/ConfirmViewHandle.java index 43619d19..f06a60fd 100644 --- a/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/handle/ConfirmViewHandle.java +++ b/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/handle/ConfirmViewHandle.java @@ -2,7 +2,9 @@ package com.pig4cloud.pig.auth.support.handle; import cn.dev33.satoken.oauth2.function.SaOAuth2ConfirmViewFunction; import cn.dev33.satoken.stp.StpUtil; +import com.pig4cloud.pig.common.core.util.WebUtils; import freemarker.template.Template; +import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import org.springframework.stereotype.Service; @@ -22,14 +24,20 @@ import java.util.Map; @RequiredArgsConstructor public class ConfirmViewHandle implements SaOAuth2ConfirmViewFunction { - private final FreeMarkerConfigurer freeMarker; + private final FreeMarkerConfigurer freeMarker; - @Override - @SneakyThrows - public Object apply(String clientId, List scopes) { - Template confirmTemplate = freeMarker.getConfiguration().getTemplate("confirm.ftl"); - Map model = Map.of("clientId", clientId, "scopes", scopes, "loginId", StpUtil.getLoginId()); - return FreeMarkerTemplateUtils.processTemplateIntoString(confirmTemplate, model); - } + @Override + @SneakyThrows + public Object apply(String clientId, List scopes) { + HttpServletRequest httpServletRequest = WebUtils.getRequest().get(); + + Template confirmTemplate = freeMarker.getConfiguration().getTemplate("confirm.ftl"); + Map model = Map.of("clientId", clientId + , "scopes", scopes + , "loginId", StpUtil.getLoginId() + , "redirectUri", httpServletRequest.getParameter("redirect_uri") + , "contextPath", httpServletRequest.getContextPath()); + return FreeMarkerTemplateUtils.processTemplateIntoString(confirmTemplate, model); + } } diff --git a/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/handle/NoLoginViewHandle.java b/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/handle/NoLoginViewHandle.java index 6fa1152a..c0c80b04 100644 --- a/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/handle/NoLoginViewHandle.java +++ b/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/handle/NoLoginViewHandle.java @@ -1,6 +1,7 @@ package com.pig4cloud.pig.auth.support.handle; import cn.dev33.satoken.oauth2.function.SaOAuth2NotLoginViewFunction; +import com.pig4cloud.pig.common.core.util.WebUtils; import freemarker.template.Template; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; @@ -8,6 +9,7 @@ import org.springframework.stereotype.Service; import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; +import java.util.HashMap; import java.util.Map; /** @@ -28,12 +30,13 @@ public class NoLoginViewHandle implements SaOAuth2NotLoginViewFunction { */ @Override public Object get() { - return get(Map.of()); + return get(new HashMap<>()); } @SneakyThrows public String get(Map model) { Template loginTemplate = freeMarker.getConfiguration().getTemplate("login.ftl"); + model.put("contextPath", WebUtils.getRequest().get().getContextPath()); return FreeMarkerTemplateUtils.processTemplateIntoString(loginTemplate, model); } diff --git a/pig-auth/src/main/resources/templates/confirm.ftl b/pig-auth/src/main/resources/templates/confirm.ftl index a039e14f..22ba5ae1 100644 --- a/pig-auth/src/main/resources/templates/confirm.ftl +++ b/pig-auth/src/main/resources/templates/confirm.ftl @@ -5,8 +5,8 @@ Pig 第三方授权 - - + + @@ -26,11 +26,11 @@
-
+ - +

将获得以下权限:

    diff --git a/pig-auth/src/main/resources/templates/login.ftl b/pig-auth/src/main/resources/templates/login.ftl index 0aeb7fa5..e000db18 100755 --- a/pig-auth/src/main/resources/templates/login.ftl +++ b/pig-auth/src/main/resources/templates/login.ftl @@ -25,7 +25,7 @@

    统一身份平台

    - +