|
|
@@ -11,6 +11,7 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.filter.OncePerRequestFilter;
|
|
|
+import space.anyi.serve.entity.auth.JwtUserPrincipal;
|
|
|
import space.anyi.serve.handler.security.JwtTokenProvider;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
@@ -34,12 +35,16 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
|
|
|
if (StringUtils.hasText(token) && jwtTokenProvider.validateToken(token)) {
|
|
|
Claims claims = jwtTokenProvider.parseToken(token);
|
|
|
String account = claims.getSubject();
|
|
|
+ Long userId = claims.get("userId", Long.class);
|
|
|
+ String username = claims.get("username", String.class);
|
|
|
String role = claims.get("role", String.class);
|
|
|
|
|
|
+ JwtUserPrincipal principal = new JwtUserPrincipal(userId, account, username, role);
|
|
|
+
|
|
|
List<SimpleGrantedAuthority> authorities = List.of(new SimpleGrantedAuthority("ROLE_" + role));
|
|
|
|
|
|
UsernamePasswordAuthenticationToken authentication =
|
|
|
- new UsernamePasswordAuthenticationToken(account, null, authorities);
|
|
|
+ new UsernamePasswordAuthenticationToken(principal, null, authorities);
|
|
|
SecurityContextHolder.getContext().setAuthentication(authentication);
|
|
|
}
|
|
|
|