|
|
|
@ -1,9 +1,7 @@
|
|
|
|
package com.mfsys.common.configuration.filter;
|
|
|
|
package com.mfsys.common.configuration.filter;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.mfsys.common.configuration.constant.PropertyConstant;
|
|
|
|
|
|
|
|
import com.mfsys.common.configuration.constant.TokenBypassURI;
|
|
|
|
import com.mfsys.common.configuration.constant.TokenBypassURI;
|
|
|
|
import com.mfsys.common.configuration.service.JwtService;
|
|
|
|
import com.mfsys.common.configuration.service.JwtService;
|
|
|
|
import jakarta.servlet.FilterChain;
|
|
|
|
import jakarta.servlet.FilterChain;
|
|
|
|
@ -23,7 +21,7 @@ import com.mfsys.common.configuration.constant.FilterPriority;
|
|
|
|
@Order(FilterPriority.AUTHENTICATION)
|
|
|
|
@Order(FilterPriority.AUTHENTICATION)
|
|
|
|
public class TokenAuthenticationFilter extends OncePerRequestFilter {
|
|
|
|
public class TokenAuthenticationFilter extends OncePerRequestFilter {
|
|
|
|
|
|
|
|
|
|
|
|
private JwtService jwtService;
|
|
|
|
private final JwtService jwtService;
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
public TokenAuthenticationFilter(JwtService jwtService) {
|
|
|
|
public TokenAuthenticationFilter(JwtService jwtService) {
|
|
|
|
@ -31,40 +29,58 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
|
|
|
protected void doFilterInternal(HttpServletRequest request,
|
|
|
|
throws ServletException, IOException {
|
|
|
|
HttpServletResponse response,
|
|
|
|
// TODO: For porOrga-change we Will removed it later
|
|
|
|
FilterChain filterChain)
|
|
|
|
if (!(request.getMethod().equals("OPTIONS"))) {
|
|
|
|
throws ServletException, IOException {
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println(">> " + request.getRequestURI() + " <<");
|
|
|
|
|
|
|
|
// TODO:
|
|
|
|
|
|
|
|
// important add all mconnect url in tokenbypass uri and remove this if
|
|
|
|
|
|
|
|
// condition or implement jwt in mconnect module
|
|
|
|
|
|
|
|
System.out.println(request.getHeaderNames());
|
|
|
|
|
|
|
|
if (!(TokenBypassURI.URIs.contains(request.getRequestURI()) || request.getRequestURI().startsWith("/MCONNECT/actuator"))) {
|
|
|
|
|
|
|
|
String token = parseJwt(request);
|
|
|
|
|
|
|
|
if (Objects.isNull(token)) {
|
|
|
|
|
|
|
|
response.setStatus(403);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// String porOrgacode = request.getHeader(FormPropertyConst.POR_ORGACODE);
|
|
|
|
|
|
|
|
String userSubject = request.getHeader("userSubject");
|
|
|
|
|
|
|
|
if (!jwtService.validateToken(token,userSubject)) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 1. Always allow OPTIONS (CORS preflight)
|
|
|
|
|
|
|
|
if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
|
|
|
|
filterChain.doFilter(request, response);
|
|
|
|
filterChain.doFilter(request, response);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String requestUri = request.getRequestURI();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 2. Skip authentication for bypass URIs
|
|
|
|
|
|
|
|
if (TokenBypassURI.URIs.contains(requestUri)
|
|
|
|
|
|
|
|
|| requestUri.startsWith("/MCONNECT/actuator")) {
|
|
|
|
|
|
|
|
filterChain.doFilter(request, response);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 3. Extract JWT
|
|
|
|
|
|
|
|
String token = parseJwt(request);
|
|
|
|
|
|
|
|
if (!StringUtils.hasText(token)) {
|
|
|
|
|
|
|
|
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 4. Extract required headers
|
|
|
|
|
|
|
|
String userSubject = request.getHeader("userId");
|
|
|
|
|
|
|
|
String porOrgaCode = request.getHeader("POR_ORGACODE");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 5. Validate header presence
|
|
|
|
|
|
|
|
if (!StringUtils.hasText(userSubject) || !StringUtils.hasText(porOrgaCode)) {
|
|
|
|
|
|
|
|
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 6. Validate token against headers
|
|
|
|
|
|
|
|
if (!jwtService.validateToken(token, userSubject, porOrgaCode)) {
|
|
|
|
|
|
|
|
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 7. Continue request
|
|
|
|
|
|
|
|
filterChain.doFilter(request, response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String parseJwt(HttpServletRequest request) {
|
|
|
|
private String parseJwt(HttpServletRequest request) {
|
|
|
|
String headerAuth = request.getHeader("Authorization");
|
|
|
|
String headerAuth = request.getHeader("Authorization");
|
|
|
|
if (StringUtils.hasText(headerAuth) && headerAuth.startsWith("Bearer ")) {
|
|
|
|
if (StringUtils.hasText(headerAuth) && headerAuth.startsWith("Bearer ")) {
|
|
|
|
return headerAuth.substring(7, headerAuth.length());
|
|
|
|
return headerAuth.substring(7);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|