返回值优化

This commit is contained in:
myh 2023-12-11 00:02:06 +08:00
parent 7c6116f2c5
commit 14e3eda3a9

View File

@ -6,7 +6,6 @@ import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
@ -38,7 +37,7 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
if (StringUtils.hasText(token) && jwtTokenProvider.validateToken(token)) { if (StringUtils.hasText(token) && jwtTokenProvider.validateToken(token)) {
// token 获取 username // token 获取 username
String username = jwtTokenProvider.getUsername(token); String username = jwtTokenProvider.getUsername(token);
// 加载与 token 关联的用户 // 加载与 token 关联的用户
UserDetails userDetails = userDetailsService.loadUserByUsername(username); UserDetails userDetails = userDetailsService.loadUserByUsername(username);
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken( UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
userDetails, userDetails,
@ -53,14 +52,14 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
filterChain.doFilter(request, response); filterChain.doFilter(request, response);
} }
private @Nullable String getTokenFromRequest(@NotNull HttpServletRequest request) { private @NotNull String getTokenFromRequest(@NotNull HttpServletRequest request) {
String bearerToken = request.getHeader("Authorization"); String bearerToken = request.getHeader("Authorization");
if (StringUtils.hasText(bearerToken) && bearerToken.startsWith("Bearer ")) { if (StringUtils.hasText(bearerToken) && bearerToken.startsWith("Bearer ")) {
return bearerToken.substring(7); return bearerToken.substring(7);
} } else {
return "";
return null; }
} }
} }