登录鉴权增加字段,放开登录接口不用鉴权,数据库密码加密

This commit is contained in:
weitang 2025-05-30 14:59:18 +08:00
parent a4f3f7510f
commit fa23e953a9
3 changed files with 27 additions and 27 deletions

View File

@ -43,16 +43,16 @@ public class SecurityConfig {
@Value("${spring.security.jwt.enabled:true}") @Value("${spring.security.jwt.enabled:true}")
private boolean jwtEnabled; private boolean jwtEnabled;
@Value("${spring.security.cors.allowed-origins:http://localhost:3000,http://localhost:8080}") @Value("${spring.security.cors.allowed-origins:http://localhost:3000,http://localhost:8080}")
private List<String> allowedOrigins; private List<String> allowedOrigins;
@Value("${spring.security.cors.max-age:3600}") @Value("${spring.security.cors.max-age:3600}")
private Long corsMaxAge; private Long corsMaxAge;
// 公开路径配置 // 公开路径配置
private static final String[] PUBLIC_PATHS = { private static final String[] PUBLIC_PATHS = {
"/api/auth/**", "/auth/**",
"/api/public/**", "/api/public/**",
"/swagger-ui/**", "/swagger-ui/**",
"/v3/api-docs/**", "/v3/api-docs/**",
@ -80,31 +80,31 @@ public class SecurityConfig {
@Bean @Bean
public CorsConfigurationSource corsConfigurationSource() { public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration(); CorsConfiguration configuration = new CorsConfiguration();
// 设置允许的源支持配置化 // 设置允许的源支持配置化
configuration.setAllowedOrigins(allowedOrigins); configuration.setAllowedOrigins(allowedOrigins);
// 设置允许的HTTP方法 // 设置允许的HTTP方法
configuration.setAllowedMethods(Arrays.asList( configuration.setAllowedMethods(Arrays.asList(
"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH" "GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"
)); ));
// 设置允许的请求头 // 设置允许的请求头
configuration.setAllowedHeaders(Arrays.asList( configuration.setAllowedHeaders(Arrays.asList(
"Authorization", "Content-Type", "X-Requested-With", "Authorization", "Content-Type", "X-Requested-With",
"Accept", "Origin", "Access-Control-Request-Method", "Accept", "Origin", "Access-Control-Request-Method",
"Access-Control-Request-Headers", "X-CSRF-TOKEN" "Access-Control-Request-Headers", "X-CSRF-TOKEN"
)); ));
// 设置暴露的响应头 // 设置暴露的响应头
configuration.setExposedHeaders(Arrays.asList( configuration.setExposedHeaders(Arrays.asList(
"Access-Control-Allow-Origin", "Access-Control-Allow-Credentials", "Access-Control-Allow-Origin", "Access-Control-Allow-Credentials",
"Authorization", "Content-Disposition" "Authorization", "Content-Disposition"
)); ));
configuration.setAllowCredentials(true); configuration.setAllowCredentials(true);
configuration.setMaxAge(corsMaxAge); configuration.setMaxAge(corsMaxAge);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration); source.registerCorsConfiguration("/**", configuration);
return source; return source;
@ -119,22 +119,22 @@ public class SecurityConfig {
http http
// CORS配置 // CORS配置
.cors(cors -> cors.configurationSource(corsConfigurationSource())) .cors(cors -> cors.configurationSource(corsConfigurationSource()))
// 禁用CSRF因为使用JWT无状态 // 禁用CSRF因为使用JWT无状态
.csrf(csrf -> csrf.disable()) .csrf(csrf -> csrf.disable())
// 会话管理配置 // 会话管理配置
.sessionManagement(session -> session .sessionManagement(session -> session
.sessionCreationPolicy(SessionCreationPolicy.STATELESS) .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.maximumSessions(1) .maximumSessions(1)
.maxSessionsPreventsLogin(false) .maxSessionsPreventsLogin(false)
) )
// 异常处理配置 // 异常处理配置
.exceptionHandling(ex -> ex .exceptionHandling(ex -> ex
.authenticationEntryPoint(jwtAuthenticationEntryPoint) .authenticationEntryPoint(jwtAuthenticationEntryPoint)
) )
// 安全头配置 // 安全头配置
.headers(headers -> headers .headers(headers -> headers
// 禁用iframe嵌入,防止点击劫持攻击 // 禁用iframe嵌入,防止点击劫持攻击
@ -157,7 +157,7 @@ public class SecurityConfig {
// 其他所有请求都需要认证 // 其他所有请求都需要认证
.anyRequest().authenticated() .anyRequest().authenticated()
); );
// 添加JWT过滤器 // 添加JWT过滤器
http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class); http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
} else { } else {
@ -169,4 +169,4 @@ public class SecurityConfig {
return http.build(); return http.build();
} }
} }

View File

@ -30,9 +30,9 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> impl
@Override @Override
public AppUser findByUsername(String username) { public AppUser findByUsername(String username) {
return getOne(new QueryWrapper<AppUser>().eq("username", username).select( "id", "username", "orgid", "usertype","nickname")); return getOne(new QueryWrapper<AppUser>().eq("username", username).select( "id", "username","password","status", "orgid", "usertype","nickname"));
} }
@Override @Override
public String getCurrentUsername() { public String getCurrentUsername() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
@ -41,7 +41,7 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> impl
} }
return null; return null;
} }
@Override @Override
public AppUser getCurrentUser() { public AppUser getCurrentUser() {
String username = getCurrentUsername(); String username = getCurrentUsername();
@ -71,7 +71,7 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> impl
// 更新新密码 // 更新新密码
user.setPassword(passwordEncoder.encode(newPassword)); user.setPassword(passwordEncoder.encode(newPassword));
user.setPwdresettime(LocalDateTime.now()); user.setPwdresettime(LocalDateTime.now());
return updateById(user); return updateById(user);
} }
} }

View File

@ -44,7 +44,7 @@ spring:
# max-lifetime: 1800000 # max-lifetime: 1800000
# connection-timeout: 30000 # connection-timeout: 30000
# connection-test-query: SELECT 1 # connection-test-query: SELECT 1
cache: cache:
jcache: jcache:
config: classpath:ehcache.xml # 指定Ehcache配置文件路径 config: classpath:ehcache.xml # 指定Ehcache配置文件路径
@ -53,7 +53,7 @@ spring:
allowed-origins: ${CORS_ALLOWED_ORIGINS:http://localhost:3000,http://localhost:8080} allowed-origins: ${CORS_ALLOWED_ORIGINS:http://localhost:3000,http://localhost:8080}
max-age: ${CORS_MAX_AGE:3600} # 预检请求的缓存时间(秒) max-age: ${CORS_MAX_AGE:3600} # 预检请求的缓存时间(秒)
jwt: jwt:
enabled: ${JWT_ENABLED:true} # 控制是否启用JWT认证 enabled: ${JWT_ENABLED:true} # 控制是否启用JWT认证
secret: ${JWT_SECRET:YourJWTSecretKeyForStdProjectBackendApplicationWhichIsVeryLongAndSecure2024!@#$%^&*()} secret: ${JWT_SECRET:YourJWTSecretKeyForStdProjectBackendApplicationWhichIsVeryLongAndSecure2024!@#$%^&*()}
expiration-ms: ${JWT_EXPIRATION:86400000} # Token 过期时间 (例如: 24小时) expiration-ms: ${JWT_EXPIRATION:86400000} # Token 过期时间 (例如: 24小时)
refresh-expiration-ms: ${JWT_REFRESH_EXPIRATION:604800000} # 刷新Token过期时间 (例如: 7天) refresh-expiration-ms: ${JWT_REFRESH_EXPIRATION:604800000} # 刷新Token过期时间 (例如: 7天)
@ -147,7 +147,7 @@ spring:
on-profile: dev on-profile: dev
security: security:
jwt: jwt:
enabled: false enabled: true
logging: logging:
level: level:
com.stdproject: DEBUG com.stdproject: DEBUG
@ -186,4 +186,4 @@ management:
endpoints: endpoints:
web: web:
exposure: exposure:
include: health,info include: health,info