-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: removing CustomUserDetailService
- Loading branch information
Hardik Singh Behl
authored and
Hardik Singh Behl
committed
Sep 28, 2023
1 parent
bd09135
commit 95d9b84
Showing
3 changed files
with
54 additions
and
88 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
src/main/java/com/behl/cerberus/filter/JwtAuthenticationFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.behl.cerberus.filter; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import com.behl.cerberus.repository.UserRepository; | ||
import com.behl.cerberus.utility.JwtUtility; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.SneakyThrows; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class JwtAuthenticationFilter extends OncePerRequestFilter { | ||
|
||
private final JwtUtility jwtUtils; | ||
private final UserRepository userRepository; | ||
|
||
private static final String AUTHORIZATION_HEADER = "Authorization"; | ||
private static final String BEARER_PREFIX = "Bearer "; | ||
|
||
@Override | ||
@SneakyThrows | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) { | ||
final var authorizationHeader = request.getHeader(AUTHORIZATION_HEADER); | ||
|
||
if (StringUtils.isNotEmpty(authorizationHeader)) { | ||
if (authorizationHeader.startsWith(BEARER_PREFIX)) { | ||
final var token = authorizationHeader.replace(BEARER_PREFIX, StringUtils.EMPTY); | ||
final var userId = jwtUtils.extractUserId(token); | ||
final var isTokenValid = jwtUtils.validateToken(token, userId); | ||
|
||
if (Boolean.TRUE.equals(isTokenValid)) { | ||
final var user = userRepository.findById(userId).orElseThrow(IllegalStateException::new); | ||
final var authentication = new UsernamePasswordAuthenticationToken( | ||
user.getEmailId(), user.getPassword(), List.of()); | ||
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
} | ||
} | ||
} | ||
filterChain.doFilter(request, response); | ||
} | ||
|
||
} |
32 changes: 0 additions & 32 deletions
32
src/main/java/com/behl/cerberus/security/CustomUserDetailService.java
This file was deleted.
Oops, something went wrong.
56 changes: 0 additions & 56 deletions
56
src/main/java/com/behl/cerberus/security/filter/JwtAuthenticationFilter.java
This file was deleted.
Oops, something went wrong.