|
| 1 | +package com.iemr.hwc.utils; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | +import org.springframework.stereotype.Component; |
| 8 | + |
| 9 | + |
| 10 | +import jakarta.servlet.Filter; |
| 11 | +import jakarta.servlet.FilterChain; |
| 12 | +import jakarta.servlet.ServletException; |
| 13 | +import jakarta.servlet.ServletRequest; |
| 14 | +import jakarta.servlet.ServletResponse; |
| 15 | +import jakarta.servlet.http.Cookie; |
| 16 | +import jakarta.servlet.http.HttpServletRequest; |
| 17 | +import jakarta.servlet.http.HttpServletResponse; |
| 18 | + |
| 19 | +@Component |
| 20 | +public class JwtUserIdValidationFilter implements Filter { |
| 21 | + |
| 22 | + private final JwtAuthenticationUtil jwtAuthenticationUtil; |
| 23 | + private final Logger logger = LoggerFactory.getLogger(this.getClass().getName()); |
| 24 | + |
| 25 | + public JwtUserIdValidationFilter(JwtAuthenticationUtil jwtAuthenticationUtil) { |
| 26 | + this.jwtAuthenticationUtil = jwtAuthenticationUtil; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) |
| 31 | + throws IOException, ServletException { |
| 32 | + HttpServletRequest request = (HttpServletRequest) servletRequest; |
| 33 | + HttpServletResponse response = (HttpServletResponse) servletResponse; |
| 34 | + |
| 35 | + String path = request.getRequestURI(); |
| 36 | + String contextPath = request.getContextPath(); |
| 37 | + logger.info("JwtUserIdValidationFilter invoked for path: " + path); |
| 38 | + |
| 39 | + // Log cookies for debugging |
| 40 | + Cookie[] cookies = request.getCookies(); |
| 41 | + if (cookies != null) { |
| 42 | + for (Cookie cookie : cookies) { |
| 43 | + if ("userId".equals(cookie.getName())) { |
| 44 | + logger.warn("userId found in cookies! Clearing it..."); |
| 45 | + clearUserIdCookie(response); // Explicitly remove userId cookie |
| 46 | + } |
| 47 | + } |
| 48 | + } else { |
| 49 | + logger.info("No cookies found in the request"); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + // Log headers for debugging |
| 54 | + String jwtTokenFromHeader = request.getHeader("Jwttoken"); |
| 55 | + logger.info("JWT token from header: " + jwtTokenFromHeader); |
| 56 | + |
| 57 | + // Skip login and public endpoints |
| 58 | + if (path.equals(contextPath + "/user/userAuthenticate") || |
| 59 | + path.equalsIgnoreCase(contextPath + "/user/logOutUserFromConcurrentSession") || |
| 60 | + path.startsWith(contextPath + "/public")) { |
| 61 | + logger.info("Skipping filter for path: " + path); |
| 62 | + filterChain.doFilter(servletRequest, servletResponse); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + try { |
| 67 | + // Retrieve JWT token from cookies |
| 68 | + String jwtTokenFromCookie = getJwtTokenFromCookies(request); |
| 69 | + logger.info("JWT token from cookie: " + jwtTokenFromCookie); |
| 70 | + |
| 71 | + // Determine which token (cookie or header) to validate |
| 72 | + String jwtToken = jwtTokenFromCookie != null ? jwtTokenFromCookie : jwtTokenFromHeader; |
| 73 | + if (jwtToken == null) { |
| 74 | + response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "JWT token not found in cookies or headers"); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + // Validate JWT token and userId |
| 79 | + boolean isValid = jwtAuthenticationUtil.validateUserIdAndJwtToken(jwtToken); |
| 80 | + |
| 81 | + if (isValid) { |
| 82 | + // If token is valid, allow the request to proceed |
| 83 | + filterChain.doFilter(servletRequest, servletResponse); |
| 84 | + } else { |
| 85 | + response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid JWT token"); |
| 86 | + } |
| 87 | + } catch (Exception e) { |
| 88 | + logger.error("Authorization error: ", e); |
| 89 | + response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Authorization error: " + e.getMessage()); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private String getJwtTokenFromCookies(HttpServletRequest request) { |
| 94 | + Cookie[] cookies = request.getCookies(); |
| 95 | + if (cookies != null) { |
| 96 | + for (Cookie cookie : cookies) { |
| 97 | + if (cookie.getName().equals("Jwttoken")) { |
| 98 | + return cookie.getValue(); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + private void clearUserIdCookie(HttpServletResponse response) { |
| 106 | + Cookie cookie = new Cookie("userId", null); |
| 107 | + cookie.setPath("/"); |
| 108 | + cookie.setMaxAge(0); // Invalidate the cookie |
| 109 | + response.addCookie(cookie); |
| 110 | + } |
| 111 | +} |
0 commit comments