From feb33c0fa32b9fa0a4429ad1a4629812de5ec23f Mon Sep 17 00:00:00 2001 From: yulinling <2712495353@qq.com> Date: Sun, 15 Jun 2025 13:22:34 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20-=20=E4=BF=AE=E5=A4=8DjwtUtil=E8=A7=A3?= =?UTF-8?q?=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../security/JwtAuthenticationFilter.java | 4 ++++ .../workflow/service/impl/AuthServiceImpl.java | 6 +++++- .../asia/yulinling/workflow/utils/JwtUtil.java | 2 +- .../yulinling/workflow/utils/JwtUtilTest.java | 16 ++++++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/main/java/asia/yulinling/workflow/security/JwtAuthenticationFilter.java b/src/main/java/asia/yulinling/workflow/security/JwtAuthenticationFilter.java index 54b8894..9800f4a 100644 --- a/src/main/java/asia/yulinling/workflow/security/JwtAuthenticationFilter.java +++ b/src/main/java/asia/yulinling/workflow/security/JwtAuthenticationFilter.java @@ -38,6 +38,7 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter { @NotNull HttpServletResponse response, @NotNull FilterChain filterChain) throws ServletException, IOException { + log.info("request: {}", request.getHeader("Authorization")); String token = getTokenFromRequest(request); log.info("token: {}", token); @@ -62,7 +63,10 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter { } private @Nullable String getTokenFromRequest(HttpServletRequest request) { +// eyJhbGciOiJIUzM4NCJ9.eyJzdWIiOiJhZG1pbiIsImlhdCI6MTc0OTk2MzA1OSwiZXhwIjoxNzQ5OTYzNjU5fQ.QxiZmycBGxfVfooh_T_lo9SibugLZ2bFt752UChHdtpNb6u__iXodQDK_s6hcz0R +// eyJhbGciOiJIUzI1NiJ9.e30.7QzwIJVh2WpbwTF5ce4crYy3kK2-4GOs0eYJqrGD8FU String bearerToken = request.getHeader("Authorization"); + if (bearerToken != null && bearerToken.startsWith("Bearer ")) { return bearerToken.substring(7); } diff --git a/src/main/java/asia/yulinling/workflow/service/impl/AuthServiceImpl.java b/src/main/java/asia/yulinling/workflow/service/impl/AuthServiceImpl.java index d106128..dc894a6 100644 --- a/src/main/java/asia/yulinling/workflow/service/impl/AuthServiceImpl.java +++ b/src/main/java/asia/yulinling/workflow/service/impl/AuthServiceImpl.java @@ -4,6 +4,7 @@ import asia.yulinling.workflow.dto.request.LoginRequest; import asia.yulinling.workflow.service.AuthService; import asia.yulinling.workflow.utils.JwtUtil; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; @@ -20,6 +21,7 @@ import org.springframework.stereotype.Service; */ @Service @RequiredArgsConstructor +@Slf4j public class AuthServiceImpl implements AuthService { private final AuthenticationManager authenticationManager; @@ -31,6 +33,8 @@ public class AuthServiceImpl implements AuthService { loginRequest.getUsername(), loginRequest.getPassword() )); SecurityContextHolder.getContext().setAuthentication(authentication); - return jwtUtil.generateToken(authentication, false); + String token =jwtUtil.generateToken(authentication, false); + log.info("generateToken: {}", token); + return token; } } diff --git a/src/main/java/asia/yulinling/workflow/utils/JwtUtil.java b/src/main/java/asia/yulinling/workflow/utils/JwtUtil.java index eff707d..3abfe49 100644 --- a/src/main/java/asia/yulinling/workflow/utils/JwtUtil.java +++ b/src/main/java/asia/yulinling/workflow/utils/JwtUtil.java @@ -113,7 +113,7 @@ public class JwtUtil { Jwts.parserBuilder() .setSigningKey(key) .build() - .parseClaimsJws(token); + .parse(token); return true; } catch (JwtException e) { log.error("Token : {}", token, e); diff --git a/src/test/java/asia/yulinling/workflow/utils/JwtUtilTest.java b/src/test/java/asia/yulinling/workflow/utils/JwtUtilTest.java index fa8bb32..5bdbb9e 100644 --- a/src/test/java/asia/yulinling/workflow/utils/JwtUtilTest.java +++ b/src/test/java/asia/yulinling/workflow/utils/JwtUtilTest.java @@ -3,6 +3,7 @@ package asia.yulinling.workflow.utils; import io.jsonwebtoken.Claims; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.BeforeEach; +import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.authority.SimpleGrantedAuthority; @@ -24,6 +25,7 @@ import static org.junit.jupiter.api.Assertions.*; */ class JwtUtilTest { private JwtUtil jwtUtil; + private AuthenticationManager authenticationManager; @BeforeEach void setUp() { @@ -44,6 +46,20 @@ class JwtUtilTest { assertEquals("test", claims.getSubject()); } + @Test + void testGenerateTokenAndValidateToken() { + Authentication authentication = new UsernamePasswordAuthenticationToken( + new MockUserPrincipal(100L, "admin", List.of("ADMIN")), + null, + List.of(new SimpleGrantedAuthority("ROLE_ADMIN")) + ); + + String token = jwtUtil.generateToken(authentication, false); + System.out.println(token); + assertNotNull(token); + assertTrue(jwtUtil.validateToken(token)); + } + @Test void testValidateToken() { UserDetails user = User.withUsername("test")