Skip to content

* Removed redundant code #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse

if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
UserDetails userDetails = context.getBean(MyUserDetailsService.class).loadUserByUsername(username);
if (jwtService.validateToken(token, userDetails)) {
if (jwtService.isTokenExpired(token)) {
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
authToken.setDetails(new WebAuthenticationDetailsSource()
.buildDetails(request));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,21 @@ private Claims extractAllClaims(String token) {
.getPayload();
}

/*

This `userName.equals(userDetails.getUsername()` is redundant because,

* Will always result in true
* The username is retrieved from the token in the JwtFilter
* The UserDetails argument is initialised in the JwtFilter by passing the same username retrieved from the received bearer token

*/
public boolean validateToken(String token, UserDetails userDetails) {
final String userName = extractUserName(token);
return (userName.equals(userDetails.getUsername()) && !isTokenExpired(token));
}

private boolean isTokenExpired(String token) {
public boolean isTokenExpired(String token) {
return extractExpiration(token).before(new Date());
}

Expand Down