fix(后端): 完善 JWT 过滤器和 SecurityConfig 配置
This commit is contained in:
parent
3a6cc4db9c
commit
071b19e42e
@ -1,6 +1,5 @@
|
|||||||
package com.lesingle.edu.common.config;
|
package com.lesingle.edu.common.config;
|
||||||
|
|
||||||
import com.lesingle.edu.common.filter.TraceIdFilter;
|
|
||||||
import com.lesingle.edu.common.security.JwtAuthenticationFilter;
|
import com.lesingle.edu.common.security.JwtAuthenticationFilter;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
@ -31,7 +30,7 @@ import java.util.List;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class SecurityConfig {
|
public class SecurityConfig {
|
||||||
|
|
||||||
private final TraceIdFilter traceIdFilter;
|
private final JwtAuthenticationFilter jwtAuthenticationFilter;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
|
||||||
@ -53,7 +52,8 @@ public class SecurityConfig {
|
|||||||
// All other requests require authentication
|
// All other requests require authentication
|
||||||
.anyRequest().authenticated()
|
.anyRequest().authenticated()
|
||||||
)
|
)
|
||||||
.addFilterBefore(traceIdFilter, UsernamePasswordAuthenticationFilter.class);
|
// 添加 JWT 过滤器到 UsernamePasswordAuthenticationFilter 之前
|
||||||
|
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
|
||||||
|
|
||||||
return http.build();
|
return http.build();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,8 +18,6 @@ import jakarta.servlet.http.HttpServletRequest;
|
|||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.core.Ordered;
|
|
||||||
import org.springframework.core.annotation.Order;
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||||
@ -39,7 +37,6 @@ import java.util.Map;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@Order(Ordered.HIGHEST_PRECEDENCE + 10) // 在 TraceIdFilter 之后执行
|
|
||||||
public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
private final JwtTokenProvider jwtTokenProvider;
|
private final JwtTokenProvider jwtTokenProvider;
|
||||||
@ -52,13 +49,17 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
|||||||
@Override
|
@Override
|
||||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
||||||
FilterChain filterChain) throws ServletException, IOException {
|
FilterChain filterChain) throws ServletException, IOException {
|
||||||
|
log.debug("JwtAuthenticationFilter doFilterInternal called for: {}", request.getRequestURI());
|
||||||
try {
|
try {
|
||||||
String token = resolveToken(request);
|
String token = resolveToken(request);
|
||||||
|
log.debug("Token extracted: {}", token != null ? "present" : "null");
|
||||||
if (StringUtils.hasText(token)) {
|
if (StringUtils.hasText(token)) {
|
||||||
|
log.debug("Token validation starting...");
|
||||||
// 验证 token 并获取错误原因
|
// 验证 token 并获取错误原因
|
||||||
String tokenErrorReason = jwtTokenProvider.validateTokenWithReason(token);
|
String tokenErrorReason = jwtTokenProvider.validateTokenWithReason(token);
|
||||||
if (tokenErrorReason != null) {
|
if (tokenErrorReason != null) {
|
||||||
// token 无效,返回 401 错误
|
// token 无效,返回 401 错误
|
||||||
|
log.debug("Token validation failed: {}", tokenErrorReason);
|
||||||
sendError(response, HttpStatus.UNAUTHORIZED, getErrorMessage(tokenErrorReason));
|
sendError(response, HttpStatus.UNAUTHORIZED, getErrorMessage(tokenErrorReason));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user