完成作业

This commit is contained in:
liyitian 2025-12-14 14:51:18 +08:00
parent 4a830003a7
commit bd14cc66fb
2 changed files with 3 additions and 2 deletions

View File

@ -66,9 +66,9 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
.orElse(null); .orElse(null);
if (user != null && jwtService.isTokenValid(jwt, user)) { if (user != null && jwtService.isTokenValid(jwt, user)) {
// 设置用户角色当前只有默认角色 // 从用户实体获取角色
List<SimpleGrantedAuthority> authorities = Collections.singletonList( List<SimpleGrantedAuthority> authorities = Collections.singletonList(
new SimpleGrantedAuthority("ROLE_USER") new SimpleGrantedAuthority(user.getRole())
); );
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(

View File

@ -69,6 +69,7 @@ public class JwtService {
return username.equals(extractedUsername) && expirationDate.after(now); return username.equals(extractedUsername) && expirationDate.after(now);
} catch (Exception e) { } catch (Exception e) {
// 任何异常都表示 token 无效
return false; return false;
} }
} }