refactor(satoken): 增加 context-path 单体架构下也支持授权码跳转

This commit is contained in:
冷冷 2024-11-13 15:40:40 +08:00
parent 391d332b57
commit 20f10d1d8d
5 changed files with 32 additions and 17 deletions

View File

@ -121,7 +121,9 @@ public class PigTokenEndpoint {
} }
// 如果未登录跳转到登录页面 // 如果未登录跳转到登录页面
return ResponseEntity.ok(noLoginViewHandle.get(Map.of("error", saResult.getMsg()))); Map<String, Object> 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<String, Object> model = new HashMap<>();
model.put("error", saResult.getMsg());
return ResponseEntity.ok(noLoginViewHandle.get(model));
} }
/** /**
@ -156,7 +160,7 @@ public class PigTokenEndpoint {
@RequestMapping("/oauth2/logout") @RequestMapping("/oauth2/logout")
public ResponseEntity<String> oauth2Logout() { public ResponseEntity<String> oauth2Logout() {
StpUtil.logout(); StpUtil.logout();
return ResponseEntity.ok(noLoginViewHandle.get(Map.of())); return ResponseEntity.ok(noLoginViewHandle.get().toString());
} }
/** /**

View File

@ -2,7 +2,9 @@ package com.pig4cloud.pig.auth.support.handle;
import cn.dev33.satoken.oauth2.function.SaOAuth2ConfirmViewFunction; import cn.dev33.satoken.oauth2.function.SaOAuth2ConfirmViewFunction;
import cn.dev33.satoken.stp.StpUtil; import cn.dev33.satoken.stp.StpUtil;
import com.pig4cloud.pig.common.core.util.WebUtils;
import freemarker.template.Template; import freemarker.template.Template;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -22,14 +24,20 @@ import java.util.Map;
@RequiredArgsConstructor @RequiredArgsConstructor
public class ConfirmViewHandle implements SaOAuth2ConfirmViewFunction { public class ConfirmViewHandle implements SaOAuth2ConfirmViewFunction {
private final FreeMarkerConfigurer freeMarker; private final FreeMarkerConfigurer freeMarker;
@Override @Override
@SneakyThrows @SneakyThrows
public Object apply(String clientId, List<String> scopes) { public Object apply(String clientId, List<String> scopes) {
Template confirmTemplate = freeMarker.getConfiguration().getTemplate("confirm.ftl");
Map<String, Object> model = Map.of("clientId", clientId, "scopes", scopes, "loginId", StpUtil.getLoginId());
return FreeMarkerTemplateUtils.processTemplateIntoString(confirmTemplate, model);
}
HttpServletRequest httpServletRequest = WebUtils.getRequest().get();
Template confirmTemplate = freeMarker.getConfiguration().getTemplate("confirm.ftl");
Map<String, Object> model = Map.of("clientId", clientId
, "scopes", scopes
, "loginId", StpUtil.getLoginId()
, "redirectUri", httpServletRequest.getParameter("redirect_uri")
, "contextPath", httpServletRequest.getContextPath());
return FreeMarkerTemplateUtils.processTemplateIntoString(confirmTemplate, model);
}
} }

View File

@ -1,6 +1,7 @@
package com.pig4cloud.pig.auth.support.handle; package com.pig4cloud.pig.auth.support.handle;
import cn.dev33.satoken.oauth2.function.SaOAuth2NotLoginViewFunction; import cn.dev33.satoken.oauth2.function.SaOAuth2NotLoginViewFunction;
import com.pig4cloud.pig.common.core.util.WebUtils;
import freemarker.template.Template; import freemarker.template.Template;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows; import lombok.SneakyThrows;
@ -8,6 +9,7 @@ import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
/** /**
@ -28,12 +30,13 @@ public class NoLoginViewHandle implements SaOAuth2NotLoginViewFunction {
*/ */
@Override @Override
public Object get() { public Object get() {
return get(Map.of()); return get(new HashMap<>());
} }
@SneakyThrows @SneakyThrows
public String get(Map<String, Object> model) { public String get(Map<String, Object> model) {
Template loginTemplate = freeMarker.getConfiguration().getTemplate("login.ftl"); Template loginTemplate = freeMarker.getConfiguration().getTemplate("login.ftl");
model.put("contextPath", WebUtils.getRequest().get().getContextPath());
return FreeMarkerTemplateUtils.processTemplateIntoString(loginTemplate, model); return FreeMarkerTemplateUtils.processTemplateIntoString(loginTemplate, model);
} }

View File

@ -5,8 +5,8 @@
<meta name="viewport" <meta name="viewport"
content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/> content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
<title>Pig 第三方授权</title> <title>Pig 第三方授权</title>
<link rel="stylesheet" type="text/css" href="/css/bootstrap.min.css"/> <link rel="stylesheet" type="text/css" href="${contextPath}/css/bootstrap.min.css"/>
<link rel="stylesheet" type="text/css" href="/css/signin.css"/> <link rel="stylesheet" type="text/css" href="${contextPath}/css/signin.css"/>
</head> </head>
<body> <body>
@ -26,11 +26,11 @@
</div> </div>
</nav> </nav>
<div style="padding-top: 80px;width: 300px; color: #555; margin:0px auto;"> <div style="padding-top: 80px;width: 300px; color: #555; margin:0px auto;">
<form id='confirmationForm' name='confirmationForm' action="/oauth2/doConfirm" method='post'> <form id='confirmationForm' name='confirmationForm' action="${contextPath}/oauth2/doConfirm" method='post'>
<input type="hidden" name="client_id" value="${clientId}"> <input type="hidden" name="client_id" value="${clientId}">
<input type="hidden" name="build_redirect_uri" value="true"> <input type="hidden" name="build_redirect_uri" value="true">
<input type="hidden" name="response_type" value="code"> <input type="hidden" name="response_type" value="code">
<input type="hidden" name="redirect_uri" value="https://pig4cloud.com"> <input type="hidden" name="redirect_uri" value="${redirectUri}">
<p> <p>
将获得以下权限:</p> 将获得以下权限:</p>
<ul class="list-group"> <ul class="list-group">

View File

@ -25,7 +25,7 @@
<div class="mb-7"> <div class="mb-7">
<h3 class="font-semibold text-2xl text-gray-800 text-center">统一身份平台</h3> <h3 class="font-semibold text-2xl text-gray-800 text-center">统一身份平台</h3>
</div> </div>
<form class="form-signin" action="/oauth2/doLogin" method="post"> <form class="form-signin" action="${contextPath}/oauth2/doLogin" method="post">
<div class="space-y-6"> <div class="space-y-6">
<div class=""> <div class="">
<input class=" w-full text-sm px-4 py-3 bg-gray-200 focus:bg-gray-100 border border-gray-200 rounded-lg focus:outline-none focus:border-purple-400" <input class=" w-full text-sm px-4 py-3 bg-gray-200 focus:bg-gray-100 border border-gray-200 rounded-lg focus:outline-none focus:border-purple-400"