2
2
3
3
import java .io .IOException ;
4
4
import java .io .Serializable ;
5
+ import java .util .Date ;
5
6
import java .util .Map ;
6
7
7
8
import jakarta .servlet .*;
8
9
import jakarta .servlet .http .HttpServletRequest ;
9
10
import jakarta .servlet .http .HttpServletResponse ;
11
+ import java .text .SimpleDateFormat ;
10
12
11
13
import org .alfresco .repo .security .authentication .AuthenticationComponent ;
12
14
import org .alfresco .repo .security .authentication .AuthenticationUtil ;
24
26
import org .edu_sharing .repository .client .tools .CCConstants ;
25
27
import org .edu_sharing .repository .server .AuthenticationToolAPI ;
26
28
import org .edu_sharing .repository .server .tools .ApplicationInfoList ;
29
+ import org .edu_sharing .repository .server .tools .security .HMac ;
27
30
import org .edu_sharing .repository .server .tools .security .SignatureVerifier ;
28
31
import org .edu_sharing .restservices .NodeDao ;
29
32
import org .edu_sharing .restservices .RepositoryDao ;
35
38
import org .edu_sharing .service .usage .Usage ;
36
39
import org .edu_sharing .service .usage .Usage2Service ;
37
40
import org .edu_sharing .service .usage .Usage2Exception ;
41
+ import org .edu_sharing .spring .ApplicationContextFactory ;
38
42
import org .edu_sharing .webservices .util .AuthenticationUtils ;
39
43
40
44
import net .sf .acegisecurity .AuthenticationCredentialsNotFoundException ;
41
45
import org .springframework .context .ApplicationContext ;
46
+ import org .apache .logging .log4j .ThreadContext ;
47
+ import org .edu_sharing .service .version .VersionService ;
42
48
43
49
44
50
public class ContextManagementFilter implements jakarta .servlet .Filter {
@@ -54,13 +60,22 @@ public class ContextManagementFilter implements jakarta.servlet.Filter {
54
60
AuthenticationService authservice = serviceRegistry .getAuthenticationService ();
55
61
AuthenticationComponent authenticationComponent = (AuthenticationComponent )applicationContext .getBean ("authenticationComponent" );
56
62
63
+ HMac hMac = null ;
64
+
65
+ SimpleDateFormat logEventDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss,SSS" );
66
+
67
+ private VersionService versionService ;
68
+
57
69
@ Override
58
70
public void destroy () {
59
71
}
60
72
61
73
@ Override
62
74
public void init (FilterConfig config ) throws ServletException {
63
75
this .context =config .getServletContext ();
76
+ hMac = HMac .getInstance ();
77
+
78
+ versionService = ApplicationContextFactory .getApplicationContext ().getBean (VersionService .class );
64
79
}
65
80
66
81
@ Override
@@ -76,12 +91,37 @@ public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
76
91
ScopeAuthenticationServiceFactory .getScopeAuthenticationService ().setScopeForCurrentThread ();
77
92
78
93
try {
94
+ String user = (String ) ((HttpServletRequest ) req ).getSession ().getAttribute (CCConstants .AUTH_USERNAME );
79
95
// Run as System because there is yet no session opened
80
96
Map <String , Serializable > info = AuthenticationUtil .runAsSystem (() ->
81
97
AuthorityServiceFactory .getLocalService ().getUserInfo (
82
- ( String ) (( HttpServletRequest ) req ). getSession (). getAttribute ( CCConstants . AUTH_USERNAME ) )
98
+ user )
83
99
);
84
100
QueryUtils .setUserInfo (info );
101
+
102
+ String remoteAdress = ((HttpServletRequest ) req ).getHeader ("x-forwarded-for" );
103
+ if (remoteAdress == null ){
104
+ remoteAdress = req .getRemoteAddr ();
105
+ }
106
+ if (remoteAdress != null ) {
107
+ ThreadContext .put ("RemoteAddr" , remoteAdress );
108
+ }
109
+
110
+ String ua = ((HttpServletRequest ) req ).getHeader ("user-agent" );
111
+ if (ua != null ){
112
+ ThreadContext .put ("UserAgent" ,ua );
113
+ }
114
+
115
+ if (user != null ){
116
+ String hmac = hMac .calculateHmac (user .trim ());
117
+ ThreadContext .put ("UserPlain" ,user );
118
+ ThreadContext .put ("User" ,hmac );
119
+ }
120
+
121
+ ThreadContext .put ("Url" ,((HttpServletRequest )req ).getRequestURL ().toString ());
122
+ ThreadContext .put ("EduVersion" , versionService .getVersionNoException (VersionService .Type .REPOSITORY ));
123
+ ThreadContext .put ("LogEventDate" ,logEventDateFormat .format (new Date ()));
124
+
85
125
}catch (Exception e ){
86
126
logger .info ("Could not set user info: " +e .getMessage ());
87
127
}
@@ -146,6 +186,8 @@ public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
146
186
//for soap api
147
187
AuthenticationUtils .setAuthenticationDetails (null );
148
188
189
+ ThreadContext .clearAll ();
190
+
149
191
}
150
192
151
193
}
0 commit comments