13
13
import org .springframework .security .oauth2 .server .resource .authentication .JwtAuthenticationToken ;
14
14
import org .springframework .stereotype .Component ;
15
15
import org .springframework .web .servlet .AsyncHandlerInterceptor ;
16
- /*GRAD2-1899, commenting the below line as this import is not in use and showing it as error*/
17
- //import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
18
16
19
17
import java .time .Instant ;
18
+ import java .util .UUID ;
20
19
21
20
@ Component
22
21
public class RequestInterceptor implements AsyncHandlerInterceptor {
23
22
24
- @ Autowired
25
23
GradValidation validation ;
24
+ EducGradStudentGraduationApiConstants constants ;
25
+ LogHelper logHelper ;
26
26
27
27
@ Autowired
28
- EducGradStudentGraduationApiConstants constants ;
28
+ public RequestInterceptor (GradValidation validation , EducGradStudentGraduationApiConstants constants , LogHelper logHelper ) {
29
+ this .validation = validation ;
30
+ this .constants = constants ;
31
+ this .logHelper = logHelper ;
32
+ }
29
33
30
34
@ Override
31
35
public boolean preHandle (HttpServletRequest request , HttpServletResponse response , Object handler ) throws Exception {
@@ -37,21 +41,30 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
37
41
validation .clear ();
38
42
// correlationID
39
43
val correlationID = request .getHeader (EducGradStudentGraduationApiConstants .CORRELATION_ID );
40
- if (correlationID != null ) {
41
- ThreadLocalStateUtil .setCorrelationID (correlationID );
44
+ ThreadLocalStateUtil .setCorrelationID (correlationID != null ? correlationID : UUID .randomUUID ().toString ());
45
+
46
+ //Request Source
47
+ val requestSource = request .getHeader (EducGradStudentGraduationApiConstants .REQUEST_SOURCE );
48
+ if (requestSource != null ) {
49
+ ThreadLocalStateUtil .setRequestSource (requestSource );
42
50
}
43
51
44
- // username
45
- Authentication auth = SecurityContextHolder .getContext ().getAuthentication ();
46
- if (auth instanceof JwtAuthenticationToken ) {
47
- JwtAuthenticationToken authenticationToken = (JwtAuthenticationToken ) auth ;
48
- Jwt jwt = (Jwt ) authenticationToken .getCredentials ();
49
- String username = JwtUtil .getName (jwt );
50
- if (username != null ) {
51
- ThreadLocalStateUtil .setCurrentUser (username );
52
+ // Header userName
53
+ val userName = request .getHeader (EducGradStudentGraduationApiConstants .USER_NAME );
54
+ if (userName != null ) {
55
+ ThreadLocalStateUtil .setCurrentUser (userName );
56
+ }
57
+ else {
58
+ Authentication auth = SecurityContextHolder .getContext ().getAuthentication ();
59
+ if (auth instanceof JwtAuthenticationToken ) {
60
+ JwtAuthenticationToken authenticationToken = (JwtAuthenticationToken ) auth ;
61
+ Jwt jwt = (Jwt ) authenticationToken .getCredentials ();
62
+ String username = JwtUtil .getName (jwt );
63
+ if (username != null ) {
64
+ ThreadLocalStateUtil .setCurrentUser (username );
65
+ }
52
66
}
53
67
}
54
-
55
68
return true ;
56
69
}
57
70
@@ -65,11 +78,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
65
78
*/
66
79
@ Override
67
80
public void afterCompletion (@ NonNull final HttpServletRequest request , final HttpServletResponse response , @ NonNull final Object handler , final Exception ex ) {
68
- LogHelper .logServerHttpReqResponseDetails (request , response , constants .isSplunkLogHelperEnabled ());
69
- val correlationID = request .getHeader (EducGradStudentGraduationApiConstants .CORRELATION_ID );
70
- if (correlationID != null ) {
71
- response .setHeader (EducGradStudentGraduationApiConstants .CORRELATION_ID , request .getHeader (EducGradStudentGraduationApiConstants .CORRELATION_ID ));
72
- }
81
+ logHelper .logServerHttpReqResponseDetails (request , response , constants .isSplunkLogHelperEnabled ());
73
82
// clear
74
83
ThreadLocalStateUtil .clear ();
75
84
}
0 commit comments