- 修复jwtUtil解析
This commit is contained in:
yulinling 2025-06-15 13:22:34 +08:00
parent 45da0753f6
commit feb33c0fa3
4 changed files with 26 additions and 2 deletions

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -113,7 +113,7 @@ public class JwtUtil {
Jwts.parserBuilder()
.setSigningKey(key)
.build()
.parseClaimsJws(token);
.parse(token);
return true;
} catch (JwtException e) {
log.error("Token <UNK>: {}", token, e);

View File

@ -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")