Skip to content

Commit

Permalink
refactor: removing CustomUserDetailService
Browse files Browse the repository at this point in the history
  • Loading branch information
Hardik Singh Behl authored and Hardik Singh Behl committed Sep 28, 2023
1 parent bd09135 commit 95d9b84
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 88 deletions.
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);
}

}

This file was deleted.

This file was deleted.

0 comments on commit 95d9b84

Please sign in to comment.