Skip to content

Commit 3d50493

Browse files
authored
[release-2.22] Refactor password encryption logic by moving it to common.html for reusability (#8283)
This is an automated cherry-pick of #8266 /assign ruibaby ```release-note None ```
1 parent 37f82a0 commit 3d50493

2 files changed

Lines changed: 23 additions & 21 deletions

File tree

application/src/main/resources/templates/gateway_fragments/common.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@
44
</th:block>
55

66
<th:block th:fragment="basicScriptResources">
7+
<th:block th:if="${not #strings.isEmpty(publicKey)} and ${fragmentTemplateName} == 'login_local'">
8+
<script src="/webjars/jsencrypt/3.5.4/bin/jsencrypt.min.js" defer></script>
9+
<script th:inline="javascript" type="text/javascript">
10+
const publicKey = /*[[${publicKey}]]*/ "";
11+
12+
// Encrypt function
13+
function encryptPassword(password) {
14+
const encrypt = new JSEncrypt();
15+
encrypt.setPublicKey(publicKey);
16+
return encrypt.encrypt(password);
17+
}
18+
19+
document.addEventListener("DOMContentLoaded", function () {
20+
const loginForm = document.getElementById("login-form");
21+
loginForm.addEventListener("submit", function (event) {
22+
const plainPasswordInput = document.getElementById("plainPassword");
23+
const passwordInput = document.getElementById("password");
24+
passwordInput.value = encryptPassword(plainPasswordInput.value);
25+
});
26+
});
27+
</script>
28+
</th:block>
29+
730
<script th:inline="javascript">
831
const i18nResources = {
932
sendVerificationCodeSuccess: `[(#{js.sendVerificationCode.success})]`,

application/src/main/resources/templates/login_local.html

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,4 @@
11
<div th:remove="tag" th:fragment="form">
2-
<script src="/webjars/jsencrypt/3.5.4/bin/jsencrypt.min.js" defer></script>
3-
<script th:inline="javascript" type="text/javascript">
4-
const publicKey = /*[[${publicKey}]]*/ "";
5-
6-
// Encrypt function
7-
function encryptPassword(password) {
8-
const encrypt = new JSEncrypt();
9-
encrypt.setPublicKey(publicKey);
10-
return encrypt.encrypt(password);
11-
}
12-
13-
document.addEventListener("DOMContentLoaded", function () {
14-
const loginForm = document.getElementById("login-form");
15-
loginForm.addEventListener("submit", function (event) {
16-
const plainPasswordInput = document.getElementById("plainPassword");
17-
const passwordInput = document.getElementById("password");
18-
passwordInput.value = encryptPassword(plainPasswordInput.value);
19-
});
20-
});
21-
</script>
22-
232
<input type="hidden" name="password" id="password" />
243

254
<div class="form-item">

0 commit comments

Comments
 (0)