Skip to content

Commit ee5bd82

Browse files
committed
Merge branch 'jskils-fix-last-commit' into dev
2 parents 624f5e0 + 3bf3372 commit ee5bd82

8 files changed

Lines changed: 118 additions & 26 deletions

File tree

continew-admin-common/src/main/java/top/continew/admin/common/model/dto/LoginUser.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,24 @@ public class LoginUser implements Serializable {
9999
*/
100100
private LocalDateTime loginTime;
101101

102-
public LoginUser(Set<String> permissions, Set<String> roleCodes, Set<RoleDTO> roles) {
102+
/**
103+
* 最后一次修改密码时间
104+
*/
105+
private LocalDateTime pwdResetTime;
106+
107+
/**
108+
* 登录时系统设置的密码过期天数
109+
*/
110+
private Integer passwordExpirationDays;
111+
112+
public LoginUser(Set<String> permissions,
113+
Set<String> roleCodes,
114+
Set<RoleDTO> roles,
115+
Integer passwordExpirationDays) {
103116
this.permissions = permissions;
104117
this.roleCodes = roleCodes;
105118
this.roles = roles;
119+
this.passwordExpirationDays = passwordExpirationDays;
106120
}
107121

108122
/**
@@ -116,4 +130,21 @@ public boolean isAdmin() {
116130
}
117131
return roleCodes.contains(SysConstants.ADMIN_ROLE_CODE);
118132
}
133+
134+
/**
135+
* 密码是否已过期
136+
*
137+
* @return 是否过期
138+
*/
139+
public boolean isPasswordExpired() {
140+
// 永久有效
141+
if (this.passwordExpirationDays == null || this.passwordExpirationDays <= SysConstants.NO) {
142+
return false;
143+
}
144+
// 初始密码(第三方登录用户)暂不提示修改
145+
if (this.pwdResetTime == null) {
146+
return false;
147+
}
148+
return this.pwdResetTime.plusDays(this.passwordExpirationDays).isBefore(LocalDateTime.now());
149+
}
119150
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package top.continew.admin.auth.config.satoken;
18+
19+
import lombok.Data;
20+
import org.springframework.boot.context.properties.ConfigurationProperties;
21+
import org.springframework.stereotype.Component;
22+
23+
/**
24+
* 密码配置属性
25+
*
26+
* @author Charles7c
27+
* @since 2024/6/15 22:15
28+
*/
29+
@Data
30+
@Component
31+
@ConfigurationProperties(prefix = "auth.password")
32+
public class LoginPasswordProperties {
33+
34+
/**
35+
* 排除(放行)路径配置
36+
*/
37+
private String[] excludes = new String[0];
38+
}

continew-admin-system/src/main/java/top/continew/admin/auth/config/satoken/SaTokenConfiguration.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,17 @@
1616

1717
package top.continew.admin.auth.config.satoken;
1818

19+
import cn.dev33.satoken.interceptor.SaInterceptor;
20+
import cn.dev33.satoken.router.SaRouter;
1921
import cn.dev33.satoken.stp.StpInterface;
22+
import lombok.RequiredArgsConstructor;
2023
import org.springframework.context.annotation.Bean;
2124
import org.springframework.context.annotation.Configuration;
25+
import top.continew.admin.common.model.dto.LoginUser;
26+
import top.continew.admin.common.util.helper.LoginHelper;
27+
import top.continew.starter.auth.satoken.autoconfigure.SaTokenExtensionProperties;
28+
import top.continew.starter.core.constant.StringConstants;
29+
import top.continew.starter.core.util.validate.CheckUtils;
2230

2331
/**
2432
* Sa-Token 配置
@@ -27,13 +35,33 @@
2735
* @since 2022/12/19 22:13
2836
*/
2937
@Configuration
38+
@RequiredArgsConstructor
3039
public class SaTokenConfiguration {
3140

41+
private final SaTokenExtensionProperties properties;
42+
private final LoginPasswordProperties loginPasswordProperties;
43+
3244
/**
3345
* Sa-Token 权限认证配置
3446
*/
3547
@Bean
3648
public StpInterface stpInterface() {
3749
return new SaTokenPermissionImpl();
3850
}
51+
52+
/**
53+
* SaToken 拦截器配置
54+
*/
55+
@Bean
56+
public SaInterceptor saInterceptor() {
57+
return new SaInterceptor(handle -> SaRouter.match(StringConstants.PATH_PATTERN)
58+
.notMatch(properties.getSecurity().getExcludes())
59+
.check(r -> {
60+
LoginUser loginUser = LoginHelper.getLoginUser();
61+
if (SaRouter.isMatchCurrURI(loginPasswordProperties.getExcludes())) {
62+
return;
63+
}
64+
CheckUtils.throwIf(loginUser.isPasswordExpired(), "密码已过期,请修改密码");
65+
}));
66+
}
3967
}

continew-admin-system/src/main/java/top/continew/admin/auth/service/impl/LoginServiceImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
import java.util.*;
6969
import java.util.concurrent.CompletableFuture;
7070

71+
import static top.continew.admin.system.enums.PasswordPolicyEnum.PASSWORD_EXPIRATION_DAYS;
72+
7173
/**
7274
* 登录业务实现
7375
*
@@ -209,8 +211,11 @@ private String login(UserDO user) {
209211
.listRoleCodeByUserId(userId), threadPoolTaskExecutor);
210212
CompletableFuture<Set<RoleDTO>> roleFuture = CompletableFuture.supplyAsync(() -> roleService
211213
.listByUserId(userId), threadPoolTaskExecutor);
214+
CompletableFuture<Integer> passwordExpirationDaysFuture = CompletableFuture.supplyAsync(() -> optionService
215+
.getValueByCode2Int(PASSWORD_EXPIRATION_DAYS.name()));
212216
CompletableFuture.allOf(permissionFuture, roleCodeFuture, roleFuture);
213-
LoginUser loginUser = new LoginUser(permissionFuture.join(), roleCodeFuture.join(), roleFuture.join());
217+
LoginUser loginUser = new LoginUser(permissionFuture.join(), roleCodeFuture.join(), roleFuture
218+
.join(), passwordExpirationDaysFuture.join());
214219
BeanUtil.copyProperties(user, loginUser);
215220
return LoginHelper.login(loginUser);
216221
}

continew-admin-system/src/main/java/top/continew/admin/system/service/UserService.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import top.continew.starter.extension.crud.service.BaseService;
3030

3131
import java.io.IOException;
32-
import java.time.LocalDateTime;
3332
import java.util.List;
3433

3534
/**
@@ -90,14 +89,6 @@ public interface UserService extends BaseService<UserResp, UserDetailResp, UserQ
9089
*/
9190
void updatePassword(String oldPassword, String newPassword, Long id);
9291

93-
/**
94-
* 密码是否已过期
95-
*
96-
* @param pwdResetTime 上次重置密码时间
97-
* @return 是否过期
98-
*/
99-
boolean isPasswordExpired(LocalDateTime pwdResetTime);
100-
10192
/**
10293
* 修改手机号
10394
*

continew-admin-system/src/main/java/top/continew/admin/system/service/impl/UserServiceImpl.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package top.continew.admin.system.service.impl;
1818

19+
import cn.dev33.satoken.stp.StpUtil;
1920
import cn.hutool.core.bean.BeanUtil;
2021
import cn.hutool.core.collection.CollUtil;
2122
import cn.hutool.core.img.ImgUtil;
@@ -210,20 +211,8 @@ public void updatePassword(String oldPassword, String newPassword, Long id) {
210211
baseMapper.updateById(user);
211212
// 保存历史密码
212213
userPasswordHistoryService.add(id, password, passwordRepetitionTimes);
213-
}
214-
215-
@Override
216-
public boolean isPasswordExpired(LocalDateTime pwdResetTime) {
217-
// 永久有效
218-
int passwordExpirationDays = optionService.getValueByCode2Int(PASSWORD_EXPIRATION_DAYS.name());
219-
if (passwordExpirationDays <= SysConstants.NO) {
220-
return false;
221-
}
222-
// 初始密码也提示修改
223-
if (pwdResetTime == null) {
224-
return true;
225-
}
226-
return pwdResetTime.plusDays(passwordExpirationDays).isBefore(LocalDateTime.now());
214+
// 修改后登出
215+
StpUtil.logout();
227216
}
228217

229218
@Override

continew-admin-webapi/src/main/java/top/continew/admin/webapi/auth/AuthController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public R<UserInfoResp> getUserInfo() {
128128
UserInfoResp userInfoResp = BeanUtil.copyProperties(userDetailResp, UserInfoResp.class);
129129
userInfoResp.setPermissions(loginUser.getPermissions());
130130
userInfoResp.setRoles(loginUser.getRoleCodes());
131-
userInfoResp.setPwdExpired(userService.isPasswordExpired(userDetailResp.getPwdResetTime()));
131+
userInfoResp.setPwdExpired(loginUser.isPasswordExpired());
132132
return R.ok(userInfoResp);
133133
}
134134

continew-admin-webapi/src/main/resources/config/application.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,16 @@ cosid:
190190
machine-bit: 3
191191
sequence-bit: 9
192192

193+
--- ### 认证配置
194+
auth:
195+
## 密码配置
196+
password:
197+
excludes:
198+
- /auth/route
199+
- /auth/user/info
200+
- /auth/logout
201+
- /system/user/password
202+
193203
--- ### 服务器配置
194204
server:
195205
servlet:

0 commit comments

Comments
 (0)