v0.2.12 登录校验修正

v0.2o
bruce 2025-03-24 11:16:00 +08:00
parent 4c43bb94c4
commit 252ea9f769
10 changed files with 77 additions and 15 deletions

View File

@ -11,6 +11,7 @@
"dependencies": {
"axios": "^1.8.4",
"element-plus": "^2.9.7",
"jwt-decode": "^4.0.0",
"pinia": "^3.0.1",
"vue": "^3.5.13",
"vue-router": "4"

View File

@ -14,6 +14,9 @@ importers:
element-plus:
specifier: ^2.9.7
version: 2.9.7(vue@3.5.13)
jwt-decode:
specifier: ^4.0.0
version: 4.0.0
pinia:
specifier: ^3.0.1
version: 3.0.1(vue@3.5.13)
@ -514,6 +517,10 @@ packages:
resolution: {integrity: sha512-ZhMwEosbFJkA0YhFnNDgTM4ZxDRsS6HqTo7qsZM08fehyRYIYa0yHu5R6mgo1n/8MgaPBXiPimPD77baVFYg+A==}
engines: {node: '>=12.13'}
jwt-decode@4.0.0:
resolution: {integrity: sha512-+KJGIyHgkGuIq3IEBNftfhW/LfWhXUIY6OmyVWjliu5KH1y0fw7VQ8YndE2O4qZdMSd9SqbnC8GOcZEy0Om7sA==}
engines: {node: '>=18'}
lodash-es@4.17.21:
resolution: {integrity: sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==}
@ -1095,6 +1102,8 @@ snapshots:
is-what@4.1.16: {}
jwt-decode@4.0.0: {}
lodash-es@4.17.21: {}
lodash-unified@1.0.3(@types/lodash-es@4.17.12)(lodash-es@4.17.21)(lodash@4.17.21):

View File

@ -23,5 +23,6 @@ export function login(form) {
loginRequest.username = account
}
return request.post('/auth/login', loginRequest).then(res => res.data)
return request.post('/login', loginRequest).then(res => res.data);
}

View File

@ -8,15 +8,21 @@ const service = axios.create({
service.interceptors.request.use(config => {
const token = localStorage.getItem('token')
if (token) config.headers.Authorization = `Bearer ${token}`
// 只给非登录接口加 token
if (token && config.url !== '/login') {
config.headers.Authorization = `Bearer ${token}`
}
return config
})
service.interceptors.response.use(
res => {
if (res.code === 200 || res.status === 200) return res
ElMessage.error(res.message || '请求出错')
return Promise.reject(new Error(res.message || 'Error'))
if (res.data.code === 200) return res.data
ElMessage.error(res.data.message || '请求出错')
return Promise.reject(new Error(res.data.message || 'Error'))
},
error => {
ElMessage.error(error.response?.data?.message || '服务器异常')
@ -25,3 +31,4 @@ service.interceptors.response.use(
)
export default service

View File

@ -31,7 +31,7 @@ public class SecurityConfig {
.csrf(csrf -> csrf.disable())
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(auth -> auth
.requestMatchers("/api/auth/**").permitAll() // 允许访问认证相关接口
.requestMatchers("/login").permitAll() // 允许访问认证相关接口
.requestMatchers("/api/auth/login").permitAll()
.requestMatchers("/api/admin/**").hasAuthority("ADMIN")
.requestMatchers("/api/user/**").hasAuthority("participant")

View File

@ -1,10 +1,20 @@
package com.bruce.sams.common.enums;
import com.baomidou.mybatisplus.annotation.EnumValue;
import lombok.Getter;
/**
*
*/
@Getter
public enum UserStatus {
ACTIVE, // 活跃
INACTIVE, // 禁用
BANNED // 封禁
}
ACTIVE("active"), // 活跃
INACTIVE("inactive"), // 禁用
BANNED("banned"); // 封禁
@EnumValue
private final String value;
UserStatus(String value) {
this.value = value;
}
}

View File

@ -13,7 +13,7 @@ import org.springframework.web.bind.annotation.RestController;
*
*/
@RestController
@RequestMapping("/api/auth")
@RequestMapping()
public class AuthController {
@Autowired

View File

@ -69,6 +69,8 @@ public class User implements UserDetails {
// 不映射到数据库
private List<GrantedAuthority> authorities;
public User() {}
public User(String username, String password, List<GrantedAuthority> authorities) {
this.username = username;
this.password = password;

View File

@ -40,14 +40,13 @@ public class AuthServiceImpl implements AuthService {
User user = null;
// 根据提供的信息查询用户
if (loginRequest.getUsername() != null) {
if (!loginRequest.getUsername().isEmpty()) {
user = userMapper.findByUsername(loginRequest.getUsername());
} else if (loginRequest.getSchoolId() != null) {
} else if (!loginRequest.getSchoolId().isEmpty()) {
user = userMapper.findBySchoolId(loginRequest.getSchoolId());
} else if (loginRequest.getEmail() != null) {
} else if (!loginRequest.getEmail().isEmpty()) {
user = userMapper.findByEmail(loginRequest.getEmail());
}
// 用户不存在
if (user == null) {
throw new UserNotFoundException();

View File

@ -0,0 +1,33 @@
package com.bruce.sams.service.impl;
import com.bruce.sams.common.utils.PasswordUtil;
import com.bruce.sams.domain.entity.LoginRequest;
import com.bruce.sams.service.AuthService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class AuthServiceImplTest {
@Autowired
private AuthService authService;
@Test
public void test() {
LoginRequest loginRequest = new LoginRequest();
loginRequest.setSchoolId("202511110001");
loginRequest.setPassword("123456");
String raw = "123456";
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
String encoded = encoder.encode(raw);
System.out.println("重新加密:" + encoded);
System.out.println("匹配测试:" + encoder.matches(raw, encoded)); // 应该为 true
// authService.authenticate(loginRequest);
}
}