-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
276 additions
and
16 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...dmin-system/src/main/java/top/continew/admin/system/mapper/UserPasswordHistoryMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package top.continew.admin.system.mapper; | ||
|
||
import org.apache.ibatis.annotations.Param; | ||
import top.continew.starter.data.mybatis.plus.base.BaseMapper; | ||
import top.continew.admin.system.model.entity.UserPasswordHistoryDO; | ||
|
||
/** | ||
* 用户历史密码 Mapper | ||
* | ||
* @author Charles7c | ||
* @since 2024/5/16 21:58 | ||
*/ | ||
public interface UserPasswordHistoryMapper extends BaseMapper<UserPasswordHistoryDO> { | ||
|
||
/** | ||
* 删除过期历史密码 | ||
* | ||
* @param userId 用户 ID | ||
* @param count 保留 N 个历史 | ||
*/ | ||
void deleteExpired(@Param("userId") Long userId, @Param("count") int count); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...in-system/src/main/java/top/continew/admin/system/model/entity/UserPasswordHistoryDO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package top.continew.admin.system.model.entity; | ||
|
||
import com.baomidou.mybatisplus.annotation.*; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.io.Serial; | ||
import java.io.Serializable; | ||
import java.time.LocalDateTime; | ||
|
||
/** | ||
* 用户历史密码实体 | ||
* | ||
* @author Charles7c | ||
* @since 2024/5/16 21:58 | ||
*/ | ||
@Data | ||
@NoArgsConstructor | ||
@TableName("sys_user_password_history") | ||
public class UserPasswordHistoryDO implements Serializable { | ||
|
||
@Serial | ||
private static final long serialVersionUID = 1L; | ||
|
||
/** | ||
* ID | ||
*/ | ||
@TableId(type = IdType.ASSIGN_ID) | ||
private Long id; | ||
|
||
/** | ||
* 用户 ID | ||
*/ | ||
private Long userId; | ||
|
||
/** | ||
* 密码 | ||
*/ | ||
private String password; | ||
|
||
/** | ||
* 创建时间 | ||
*/ | ||
@TableField(fill = FieldFill.INSERT) | ||
private LocalDateTime createTime; | ||
|
||
public UserPasswordHistoryDO(Long userId, String password) { | ||
this.userId = userId; | ||
this.password = password; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...in-system/src/main/java/top/continew/admin/system/service/UserPasswordHistoryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package top.continew.admin.system.service; | ||
|
||
/** | ||
* 用户历史密码业务接口 | ||
* | ||
* @author Charles7c | ||
* @since 2024/5/16 21:58 | ||
*/ | ||
public interface UserPasswordHistoryService { | ||
|
||
/** | ||
* 新增 | ||
* | ||
* @param userId 用户 ID | ||
* @param password 密码 | ||
* @param count 保留 N 个历史 | ||
*/ | ||
void add(Long userId, String password, int count); | ||
|
||
/** | ||
* 密码是否为重复使用 | ||
* | ||
* @param userId 用户 ID | ||
* @param password 密码 | ||
* @param count 最近 N 次 | ||
* @return 是否为重复使用 | ||
*/ | ||
boolean isPasswordReused(Long userId, String password, int count); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
.../src/main/java/top/continew/admin/system/service/impl/UserPasswordHistoryServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package top.continew.admin.system.service.impl; | ||
|
||
import cn.hutool.core.collection.CollUtil; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import top.continew.admin.system.mapper.UserPasswordHistoryMapper; | ||
import top.continew.admin.system.model.entity.UserPasswordHistoryDO; | ||
import top.continew.admin.system.service.UserPasswordHistoryService; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* 用户历史密码业务实现 | ||
* | ||
* @author Charles7c | ||
* @since 2024/5/16 21:58 | ||
*/ | ||
@Service | ||
@RequiredArgsConstructor | ||
public class UserPasswordHistoryServiceImpl implements UserPasswordHistoryService { | ||
|
||
private final UserPasswordHistoryMapper baseMapper; | ||
private final PasswordEncoder passwordEncoder; | ||
|
||
@Override | ||
@Transactional(rollbackFor = Exception.class) | ||
public void add(Long userId, String password, int count) { | ||
baseMapper.insert(new UserPasswordHistoryDO(userId, password)); | ||
// 删除过期历史密码 | ||
baseMapper.deleteExpired(userId, count); | ||
} | ||
|
||
@Override | ||
public boolean isPasswordReused(Long userId, String password, int count) { | ||
// 查询近 N 个历史密码 | ||
List<UserPasswordHistoryDO> list = baseMapper.lambdaQuery() | ||
.select(UserPasswordHistoryDO::getPassword) | ||
.eq(UserPasswordHistoryDO::getUserId, userId) | ||
.orderByDesc(UserPasswordHistoryDO::getCreateTime) | ||
.last("LIMIT %s".formatted(count)) | ||
.list(); | ||
if (CollUtil.isEmpty(list)) { | ||
return false; | ||
} | ||
// 校验是否重复使用历史密码 | ||
List<String> passwordList = list.stream().map(UserPasswordHistoryDO::getPassword).toList(); | ||
return passwordList.stream().anyMatch(p -> passwordEncoder.matches(password, p)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
continew-admin-system/src/main/resources/mapper/UserPasswordHistoryMapper.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > | ||
<mapper namespace="top.continew.admin.system.mapper.UserPasswordHistoryMapper"> | ||
<delete id="deleteExpired"> | ||
DELETE FROM sys_user_password_history | ||
WHERE id NOT IN ( | ||
SELECT * FROM ( | ||
SELECT id | ||
FROM sys_user_password_history | ||
WHERE user_id = #{userId} | ||
ORDER BY create_time DESC | ||
LIMIT #{count} | ||
) t1 | ||
) | ||
</delete> | ||
</mapper> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.