@@ -48,49 +48,60 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
4848 logger .info ("No cookies found in the request" );
4949 }
5050
51- // Log headers for debugging
52- String jwtTokenFromHeader = request .getHeader ("Jwttoken" );
53- logger .info ("JWT token from header: " );
54-
5551 // Skip login and public endpoints
5652 if (path .equals (contextPath + "/user/userAuthenticate" )
5753 || path .equalsIgnoreCase (contextPath + "/user/logOutUserFromConcurrentSession" )
58- || path .startsWith (contextPath + "/swagger-ui" )
59- || path .startsWith (contextPath + "/v3/api-docs" )
60- || path .startsWith (contextPath + "/user/refreshToken" )
61- || path .startsWith (contextPath + "/public" )) {
54+ || path .startsWith (contextPath + "/swagger-ui" ) || path .startsWith (contextPath + "/v3/api-docs" )
55+ || path .startsWith (contextPath + "/user/refreshToken" ) || path .startsWith (contextPath + "/public" )) {
6256 logger .info ("Skipping filter for path: " + path );
6357 filterChain .doFilter (servletRequest , servletResponse );
6458 return ;
6559 }
6660
6761 try {
68- // Retrieve JWT token from cookies
69- String jwtTokenFromCookie = getJwtTokenFromCookies (request );
70- logger .info ("JWT token from cookie: " );
71-
72- // Determine which token (cookie or header) to validate
73- String jwtToken = jwtTokenFromCookie != null ? jwtTokenFromCookie : jwtTokenFromHeader ;
74- if (jwtToken == null ) {
75- response .sendError (HttpServletResponse .SC_UNAUTHORIZED , "JWT token not found in cookies or headers" );
76- return ;
62+ String jwtFromCookie = getJwtTokenFromCookies (request );
63+ String jwtFromHeader = request .getHeader ("JwtToken" );
64+ String authHeader = request .getHeader ("Authorization" );
65+
66+ if (jwtFromCookie != null ) {
67+ logger .info ("Validating JWT token from cookie" );
68+ if (jwtAuthenticationUtil .validateUserIdAndJwtToken (jwtFromCookie )) {
69+ filterChain .doFilter (servletRequest , servletResponse );
70+ return ;
71+ }
7772 }
7873
79- // Validate JWT token and userId
80- boolean isValid = jwtAuthenticationUtil .validateUserIdAndJwtToken (jwtToken );
74+ if (jwtFromHeader != null ) {
75+ logger .info ("Validating JWT token from header" );
76+ if (jwtAuthenticationUtil .validateUserIdAndJwtToken (jwtFromHeader )) {
77+ filterChain .doFilter (servletRequest , servletResponse );
78+ return ;
79+ }
80+ }
81+ String userAgent = request .getHeader ("User-Agent" );
82+ logger .info ("User-Agent: " + userAgent );
8183
82- if (isValid ) {
83- // If token is valid, allow the request to proceed
84+ if (userAgent != null && isMobileClient (userAgent ) && authHeader != null ) {
8485 filterChain .doFilter (servletRequest , servletResponse );
85- } else {
86- response .sendError (HttpServletResponse .SC_UNAUTHORIZED , "Invalid JWT token" );
86+ return ;
8787 }
88+
89+ logger .warn ("No valid authentication token found" );
90+ response .sendError (HttpServletResponse .SC_UNAUTHORIZED , "Unauthorized: Invalid or missing token" );
91+
8892 } catch (Exception e ) {
8993 logger .error ("Authorization error: " , e );
9094 response .sendError (HttpServletResponse .SC_UNAUTHORIZED , "Authorization error: " + e .getMessage ());
9195 }
9296 }
9397
98+ private boolean isMobileClient (String userAgent ) {
99+ if (userAgent == null )
100+ return false ;
101+ userAgent = userAgent .toLowerCase ();
102+ return userAgent .contains ("okhttp" ); // iOS (custom clients)
103+ }
104+
94105 private String getJwtTokenFromCookies (HttpServletRequest request ) {
95106 Cookie [] cookies = request .getCookies ();
96107 if (cookies != null ) {
0 commit comments