refactor: OIDC登录时记录登录地址到浏览器本地存储

This commit is contained in:
CaptainB 2023-04-24 16:46:49 +08:00 committed by 刘瑞斌
parent 9ab8c49562
commit d8c8ad8394
4 changed files with 24 additions and 6 deletions

View File

@ -6,6 +6,8 @@ import io.metersphere.commons.user.SessionUser;
import io.metersphere.commons.utils.CodingUtil;
import io.metersphere.gateway.log.annotation.MsAuditLog;
import io.metersphere.gateway.service.SSOService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@ -14,7 +16,6 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.reactive.result.view.Rendering;
import org.springframework.web.server.WebSession;
import jakarta.annotation.Resource;
import java.util.Locale;
import java.util.Optional;
@ -28,16 +29,22 @@ public class SSOController {
@MsAuditLog(module = OperLogModule.AUTH_TITLE, type = OperLogConstants.LOGIN, title = "登录")
public Rendering callbackWithAuthId(@RequestParam("code") String code, @PathVariable("authId") String authId, WebSession session, Locale locale) throws Exception {
Optional<SessionUser> sessionUser = ssoService.exchangeToken(code, authId, session, locale);
return Rendering.redirectTo("/#/?_token=" + CodingUtil.base64Encoding(session.getId()) + "&_csrf=" + sessionUser.get().getCsrfToken())
.build();
String url = "/#/?_token=" + CodingUtil.base64Encoding(session.getId()) + "&_csrf=" + sessionUser.get().getCsrfToken();
if (StringUtils.isNotEmpty(sessionUser.get().getLoginUrl())) {
url += "&oidcLoginUrl=" + sessionUser.get().getLoginUrl();
}
return Rendering.redirectTo(url).build();
}
@GetMapping("callback")
@MsAuditLog(module = OperLogModule.AUTH_TITLE, type = OperLogConstants.LOGIN, title = "登录")
public Rendering callback(@RequestParam("code") String code, @RequestParam("state") String authId, WebSession session, Locale locale) throws Exception {
Optional<SessionUser> sessionUser = ssoService.exchangeToken(code, authId, session, locale);
return Rendering.redirectTo("/#/?_token=" + CodingUtil.base64Encoding(session.getId()) + "&_csrf=" + sessionUser.get().getCsrfToken())
.build();
String url = "/#/?_token=" + CodingUtil.base64Encoding(session.getId()) + "&_csrf=" + sessionUser.get().getCsrfToken();
if (StringUtils.isNotEmpty(sessionUser.get().getLoginUrl())) {
url += "&oidcLoginUrl=" + sessionUser.get().getLoginUrl();
}
return Rendering.redirectTo(url).build();
}
@GetMapping("callback/oauth2")

View File

@ -164,6 +164,9 @@ public class SSOService {
Optional<SessionUser> userOptional = userLoginService.login(loginRequest, session, locale);
session.getAttributes().put("authenticate", authSource.getType());
session.getAttributes().put("authId", authSource.getId());
if (StringUtils.isNotEmpty((String) config.get("loginUrl"))) {
userOptional.get().setLoginUrl((String) config.get("loginUrl"));
}
return userOptional;
}

View File

@ -24,7 +24,7 @@
<script>
import MsDialogFooter from "../MsDialogFooter";
import {removeGoBackListener} from "../../utils";
import {getUrlParams, removeGoBackListener} from "../../utils";
import MsTableOperatorButton from "../MsTableOperatorButton";
import {EMAIL_REGEX, PHONE_REGEX} from "../../utils/regex";
import {useUserStore} from "@/store";
@ -79,6 +79,13 @@ export default {
},
};
},
created() {
let urlParams = getUrlParams(window.location.href);
let oidcLoginUrl = urlParams['oidcLoginUrl']
if (oidcLoginUrl) {
localStorage.setItem('oidcLoginUrl', oidcLoginUrl);
}
},
methods: {
cancel() {
this.$emit("cancel");

View File

@ -21,6 +21,7 @@ public class SessionUser extends UserDTO implements Serializable {
private static final long serialVersionUID = -7149638440406959033L;
private String csrfToken;
private String sessionId;
private String loginUrl; // 三方登录地址如果有值前端跳转到该地址
private SessionUser() {
}