fix:
- 修复jwtUtil解析
This commit is contained in:
parent
45da0753f6
commit
feb33c0fa3
@ -38,6 +38,7 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
|||||||
@NotNull HttpServletResponse response,
|
@NotNull HttpServletResponse response,
|
||||||
@NotNull FilterChain filterChain) throws ServletException, IOException {
|
@NotNull FilterChain filterChain) throws ServletException, IOException {
|
||||||
|
|
||||||
|
log.info("request: {}", request.getHeader("Authorization"));
|
||||||
String token = getTokenFromRequest(request);
|
String token = getTokenFromRequest(request);
|
||||||
log.info("token: {}", token);
|
log.info("token: {}", token);
|
||||||
|
|
||||||
@ -62,7 +63,10 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private @Nullable String getTokenFromRequest(HttpServletRequest request) {
|
private @Nullable String getTokenFromRequest(HttpServletRequest request) {
|
||||||
|
// eyJhbGciOiJIUzM4NCJ9.eyJzdWIiOiJhZG1pbiIsImlhdCI6MTc0OTk2MzA1OSwiZXhwIjoxNzQ5OTYzNjU5fQ.QxiZmycBGxfVfooh_T_lo9SibugLZ2bFt752UChHdtpNb6u__iXodQDK_s6hcz0R
|
||||||
|
// eyJhbGciOiJIUzI1NiJ9.e30.7QzwIJVh2WpbwTF5ce4crYy3kK2-4GOs0eYJqrGD8FU
|
||||||
String bearerToken = request.getHeader("Authorization");
|
String bearerToken = request.getHeader("Authorization");
|
||||||
|
|
||||||
if (bearerToken != null && bearerToken.startsWith("Bearer ")) {
|
if (bearerToken != null && bearerToken.startsWith("Bearer ")) {
|
||||||
return bearerToken.substring(7);
|
return bearerToken.substring(7);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import asia.yulinling.workflow.dto.request.LoginRequest;
|
|||||||
import asia.yulinling.workflow.service.AuthService;
|
import asia.yulinling.workflow.service.AuthService;
|
||||||
import asia.yulinling.workflow.utils.JwtUtil;
|
import asia.yulinling.workflow.utils.JwtUtil;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.security.authentication.AuthenticationManager;
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
@ -20,6 +21,7 @@ import org.springframework.stereotype.Service;
|
|||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
|
@Slf4j
|
||||||
public class AuthServiceImpl implements AuthService {
|
public class AuthServiceImpl implements AuthService {
|
||||||
|
|
||||||
private final AuthenticationManager authenticationManager;
|
private final AuthenticationManager authenticationManager;
|
||||||
@ -31,6 +33,8 @@ public class AuthServiceImpl implements AuthService {
|
|||||||
loginRequest.getUsername(), loginRequest.getPassword()
|
loginRequest.getUsername(), loginRequest.getPassword()
|
||||||
));
|
));
|
||||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||||
return jwtUtil.generateToken(authentication, false);
|
String token =jwtUtil.generateToken(authentication, false);
|
||||||
|
log.info("generateToken: {}", token);
|
||||||
|
return token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -113,7 +113,7 @@ public class JwtUtil {
|
|||||||
Jwts.parserBuilder()
|
Jwts.parserBuilder()
|
||||||
.setSigningKey(key)
|
.setSigningKey(key)
|
||||||
.build()
|
.build()
|
||||||
.parseClaimsJws(token);
|
.parse(token);
|
||||||
return true;
|
return true;
|
||||||
} catch (JwtException e) {
|
} catch (JwtException e) {
|
||||||
log.error("Token <UNK>: {}", token, e);
|
log.error("Token <UNK>: {}", token, e);
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package asia.yulinling.workflow.utils;
|
|||||||
import io.jsonwebtoken.Claims;
|
import io.jsonwebtoken.Claims;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||||
@ -24,6 +25,7 @@ import static org.junit.jupiter.api.Assertions.*;
|
|||||||
*/
|
*/
|
||||||
class JwtUtilTest {
|
class JwtUtilTest {
|
||||||
private JwtUtil jwtUtil;
|
private JwtUtil jwtUtil;
|
||||||
|
private AuthenticationManager authenticationManager;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
@ -44,6 +46,20 @@ class JwtUtilTest {
|
|||||||
assertEquals("test", claims.getSubject());
|
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
|
@Test
|
||||||
void testValidateToken() {
|
void testValidateToken() {
|
||||||
UserDetails user = User.withUsername("test")
|
UserDetails user = User.withUsername("test")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user