feat(security): 重构安全配置并添加用户认证功能

refactor: 将ResponseMessage移动到config包并增强功能
feat: 添加用户管理相关功能及密码加密配置
fix: 修复HelpController中README文件路径问题
docs: 更新application.properties配置注释
style: 清理无用导入和日志文件
This commit is contained in:
qingfeng1121
2025-10-28 12:47:02 +08:00
parent 9132feb870
commit 5803080352
38 changed files with 2733 additions and 9062 deletions

View File

@@ -1,21 +1,47 @@
package com.qf.myafterprojecy.config;
import javax.ws.rs.HttpMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
/**
* Spring Security配置类
* 用于关闭默认的登录验证功能
* 配置权限管理功能
*/
@Configuration
@EnableWebSecurity
// 启用方法级别的安全控制
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
public class SecurityConfig {
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private PasswordEncoder passwordEncoder;
/**
* 配置安全过滤器链,允许所有请求通过
* 配置AuthenticationManager Bean
* 使用AuthenticationConfiguration来获取认证管理器这是更现代的方式
*/
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
/**
* 配置安全过滤器链
* @param http HttpSecurity对象用于配置HTTP安全策略
* @return 配置好的SecurityFilterChain对象
* @throws Exception 配置过程中可能出现的异常
@@ -25,16 +51,26 @@ public class SecurityConfig {
http
// 禁用CSRF保护对于API服务通常不需要
.csrf().disable()
// 允许所有请求通过,不需要认证
// 配置URL访问权限
.authorizeRequests()
.anyRequest().permitAll()
// 允许公开访问的路径
// 公开get请求
.antMatchers(HttpMethod.GET,"/api/auth/**").permitAll()
.antMatchers(HttpMethod.GET,"/api/help/**").permitAll()
.antMatchers(HttpMethod.GET,"/api/category-attributes/**").permitAll()
.antMatchers(HttpMethod.GET,"/api/markdowns/**").permitAll()
.antMatchers(HttpMethod.GET,"/api/articles/**").permitAll()
.antMatchers(HttpMethod.GET,"/api/messages/**").permitAll()
// 公开post请求
.antMatchers(HttpMethod.POST,"/api/messages/**").permitAll()
// 管理员才能访问的路径
.antMatchers("/api/admin/**").hasRole("ADMIN")
// 其他所有请求都需要认证
.anyRequest().authenticated()
.and()
// 禁用表单登录
.formLogin().disable()
// 禁用HTTP基本认证
.httpBasic().disable()
// 禁用会话管理对于无状态API服务
.sessionManagement().disable();
// 配置会话管理,使用无状态会话
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
return http.build();
}