Skip to content

Commit d3b57af

Browse files
authored
feat: 优化授权码登录 (#5914)
2 parents b707205 + f0b8fb9 commit d3b57af

File tree

17 files changed

+102
-102
lines changed

17 files changed

+102
-102
lines changed

laokou-cloud/laokou-gateway/src/main/resources/application.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ server:
4040
key-password: laokou
4141
http2:
4242
enabled: false
43-
forward-headers-strategy: native
43+
forward-headers-strategy: framework
4444
# 优雅停机
4545
shutdown: graceful
4646
netty:
@@ -199,7 +199,6 @@ request-matcher:
199199
- /doc.html=laokou-gateway
200200
- /webjars/**=laokou-gateway
201201
- /ws=laokou-gateway
202-
- /api/login=laokou-gateway
203202
POST:
204203
- /api/v1/captchas/send/mail=laokou-gateway
205204
- /api/v1/captchas/send/mobile=laokou-gateway

laokou-common/laokou-common-oss/src/main/java/org/laokou/common/oss/template/MinIOStorage.java

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,16 @@
1919

2020
import io.minio.BucketExistsArgs;
2121
import io.minio.GetPresignedObjectUrlArgs;
22+
import io.minio.Http;
2223
import io.minio.MakeBucketArgs;
2324
import io.minio.MinioClient;
2425
import io.minio.PutObjectArgs;
25-
import io.minio.errors.ErrorResponseException;
26-
import io.minio.errors.InsufficientDataException;
27-
import io.minio.errors.InternalException;
28-
import io.minio.errors.InvalidResponseException;
29-
import io.minio.errors.ServerException;
30-
import io.minio.errors.XmlParserException;
31-
import io.minio.http.Method;
26+
import io.minio.errors.MinioException;
3227
import org.laokou.common.i18n.common.exception.BizException;
3328
import org.laokou.common.oss.model.BaseOss;
3429
import org.laokou.common.oss.model.FileInfo;
3530
import org.laokou.common.oss.model.MinIO;
3631

37-
import java.io.IOException;
38-
import java.security.InvalidKeyException;
39-
import java.security.NoSuchAlgorithmException;
4032
import java.util.concurrent.TimeUnit;
4133

4234
/**
@@ -61,9 +53,7 @@ protected MinioClient getObj() {
6153
}
6254

6355
@Override
64-
protected void checkBucket(MinioClient minioClient) throws ServerException, InsufficientDataException,
65-
ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException,
66-
InvalidResponseException, XmlParserException, InternalException {
56+
protected void checkBucket(MinioClient minioClient) throws MinioException {
6757
String bucketName = this.minIO.getBucketName();
6858
boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
6959
if (!isExist) {
@@ -73,35 +63,29 @@ protected void checkBucket(MinioClient minioClient) throws ServerException, Insu
7363
}
7464

7565
@Override
76-
protected void upload(MinioClient minioClient) throws ServerException, InsufficientDataException,
77-
ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException,
78-
InvalidResponseException, XmlParserException, InternalException {
66+
protected void upload(MinioClient minioClient) throws MinioException {
7967
PutObjectArgs objectArgs = PutObjectArgs.builder()
8068
.bucket(this.minIO.getBucketName())
8169
.object(fileInfo.name())
82-
.stream(fileInfo.inputStream(), fileInfo.size(), -1)
70+
.stream(fileInfo.inputStream(), fileInfo.size(), -1L)
8371
.contentType(fileInfo.contentType())
8472
.build();
8573
minioClient.putObject(objectArgs);
8674
}
8775

8876
@Override
89-
protected String getUrl(MinioClient minioClient) throws ServerException, InsufficientDataException,
90-
ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException,
91-
InvalidResponseException, XmlParserException, InternalException {
77+
protected String getUrl(MinioClient minioClient) throws MinioException {
9278
GetPresignedObjectUrlArgs objectUrlArgs = GetPresignedObjectUrlArgs.builder()
9379
.bucket(this.minIO.getBucketName())
9480
.object(fileInfo.name())
95-
.method(Method.GET)
81+
.method(Http.Method.GET)
9682
.expiry(5, TimeUnit.DAYS)
9783
.build();
9884
return minioClient.getPresignedObjectUrl(objectUrlArgs);
9985
}
10086

10187
@Override
102-
public void createBucket() throws ServerException, InsufficientDataException, ErrorResponseException, IOException,
103-
NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException,
104-
InternalException {
88+
public void createBucket() throws MinioException {
10589
MinioClient minioClient = getObj();
10690
String bucketName = this.minIO.getBucketName();
10791
boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());

laokou-service/laokou-admin/laokou-admin-start/src/main/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ server:
3434
key-store-password: laokou
3535
http2:
3636
enabled: false
37-
forward-headers-strategy: native
37+
forward-headers-strategy: framework
3838
shutdown: graceful
3939
servlet:
4040
context-path: /api

laokou-service/laokou-auth/laokou-auth-infrastructure/src/main/java/org/laokou/auth/config/OAuth2AuthenticationFailureHandler.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

laokou-service/laokou-auth/laokou-auth-infrastructure/src/main/java/org/laokou/auth/config/OAuth2ResourceServerConfig.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http,
6565
// https://docs.spring.io/spring-security/reference/servlet/authentication/passwords/form.html
6666
// 登录页面 -> DefaultLoginPageGeneratingFilter
6767
.formLogin(form -> form.loginPage("/login")
68-
// .failureHandler(oAuth2AuthenticationFailureHandler)
6968
.permitAll())
7069
// 清除 session
7170
.logout(logout -> logout.clearAuthentication(true).invalidateHttpSession(true))

laokou-service/laokou-auth/laokou-auth-start/src/main/resources/application.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ server:
3434
key-store-password: laokou
3535
http2:
3636
enabled: false
37-
forward-headers-strategy: native
37+
forward-headers-strategy: framework
3838
shutdown: graceful
3939
port: ${SERVER_PORT:1111}
4040
servlet:
@@ -162,8 +162,8 @@ spring:
162162
- /v1/secrets=laokou-auth
163163
- /doc.html=laokou-auth
164164
- /webjars/**=laokou-auth
165-
- /login=laokou-auth
166165
- /img/**=laokou-auth
166+
- /js/**=laokou-auth
167167
POST:
168168
- /v1/captchas/send/mail=laokou-auth
169169
- /v1/captchas/send/mobile=laokou-auth

laokou-service/laokou-auth/laokou-auth-start/src/main/resources/static/js/axios.min.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

laokou-service/laokou-auth/laokou-auth-start/src/main/resources/templates/login.html

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<meta charset="UTF-8"/>
55
<meta name="viewport" content="width=device-width,initial-scale=1"/>
66
<title>老寇IoT云平台统一认证</title>
7+
<script src="js/axios.min.js"></script>
78
<style>
89
html, body {
910
height: 100%;
@@ -13,7 +14,7 @@
1314
overflow-x: hidden;
1415
min-height: 100vh;
1516
font-family: Arial, Helvetica, sans-serif;
16-
background: url('/api/img/FfdJeJRQWjEeGTpqgBKj.png') no-repeat center center;
17+
background: url('img/FfdJeJRQWjEeGTpqgBKj.png') no-repeat center center;
1718
background-size: cover;
1819

1920
/* 更健壮的居中布局:适配不同屏幕尺寸 */
@@ -98,32 +99,56 @@
9899
<h2>老寇IoT云平台统一认证</h2>
99100
</div>
100101

101-
<div class="alert alert-danger" th:if="${session.SPRING_SECURITY_LAST_EXCEPTION != null}">
102+
<div class="alert alert-danger" th:if="${param.error != null and session.SPRING_SECURITY_LAST_EXCEPTION != null}">
102103
<span th:text="${session.SPRING_SECURITY_LAST_EXCEPTION?.message}"></span>
103104
</div>
104-
<form method="post" th:action="@{/login}">
105+
<form method="post" th:action="@{/login}" autocomplete="off">
106+
105107
<div class="form-item row" style="width: 100%;">
106-
<input id="tenant" name="tenant" type="text" placeholder="请输入租户编号" required autofocus/>
108+
<input id="uuid" name="uuid" type="text" autocomplete="new-password" hidden="hidden" placeholder="请输入唯一标识" required/>
107109
</div>
108110

109111
<div class="form-item row" style="width: 100%;">
110-
<input id="username" name="username" type="text" placeholder="请输入用户名" required/>
112+
<input id="tenant_code" name="tenant_code" autocomplete="new-password" value="laokouyun" type="text" placeholder="请输入租户编号" required autofocus/>
111113
</div>
112114

113115
<div class="form-item row" style="width: 100%;">
114-
<input id="password" name="password" type="password" placeholder="请输入密码" required/>
116+
<input id="username" name="username" type="text" autocomplete="new-password" value="admin" placeholder="请输入用户名" required/>
117+
</div>
118+
119+
<div class="form-item row" style="width: 100%;">
120+
<input id="password" name="password" type="password" autocomplete="new-password" value="admin123" placeholder="请输入密码" required/>
115121
</div>
116122

117123
<div class="form-item">
118124
<div class="row">
119-
<input id="captcha" name="captcha" type="text" placeholder="请输入验证码" required/>
120-
<img class="captcha" th:src="@{/captcha}" alt="captcha"
121-
onclick="this.src='/captcha?ts='+Date.now()"/>
125+
<input id="captcha" name="captcha" autocomplete="new-password" type="text" placeholder="请输入验证码" required/>
126+
<img id="captcha-img" class="captcha" alt="验证码" onclick="getCaptcha()"/>
122127
</div>
123128
</div>
124129

125130
<button class="btn" type="submit">登&nbsp;&nbsp;录</button>
126131
</form>
127132
</div>
133+
<script>
134+
function getUuid() {
135+
if (window.crypto && crypto.randomUUID) {
136+
return crypto.randomUUID();
137+
}
138+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
139+
const r = Math.random() * 16 | 0;
140+
const v = c === 'x' ? r : (r & 0x3 | 0x8);
141+
return v.toString(16);
142+
});
143+
}
144+
function getCaptcha() {
145+
const uuid = getUuid()
146+
document.getElementById("uuid").value = uuid
147+
axios.get("v1/authorization-code/captchas/" + uuid).then(res => {
148+
document.getElementById("captcha-img").src = res.data.data
149+
})
150+
}
151+
getCaptcha()
152+
</script>
128153
</body>
129154
</html>

laokou-service/laokou-generator/laokou-generator-start/src/main/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ server:
1717
key-store-password: laokou
1818
http2:
1919
enabled: false
20-
forward-headers-strategy: native
20+
forward-headers-strategy: framework
2121
shutdown: graceful
2222
servlet:
2323
context-path: /api

laokou-service/laokou-logstash/laokou-logstash-start/src/main/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ server:
3434
key-store-password: laokou
3535
http2:
3636
enabled: false
37-
forward-headers-strategy: native
37+
forward-headers-strategy: framework
3838
shutdown: graceful
3939
servlet:
4040
context-path: /api

0 commit comments

Comments
 (0)