11package com .ctrip .framework .apollo .portal .spi .ctrip .filters ;
22
33import com .ctrip .framework .apollo .portal .constant .CatEventType ;
4+ import com .ctrip .framework .apollo .portal .entity .bo .UserInfo ;
45import com .ctrip .framework .apollo .portal .spi .UserInfoHolder ;
56import com .ctrip .framework .apollo .tracer .Tracer ;
7+
68import com .google .common .base .Strings ;
79
810import javax .servlet .*;
911import javax .servlet .http .HttpServletRequest ;
12+
1013import java .io .IOException ;
1114
1215public class UserAccessFilter implements Filter {
@@ -26,12 +29,19 @@ public void init(FilterConfig filterConfig) throws ServletException {
2629
2730 @ Override
2831 public void doFilter (ServletRequest request , ServletResponse response , FilterChain chain )
29- throws IOException , ServletException {
32+ throws IOException , ServletException {
3033
3134 String requestUri = ((HttpServletRequest ) request ).getRequestURI ();
3235
33- if (!isStaticResource (requestUri )) {
34- Tracer .logEvent (CatEventType .USER_ACCESS , userInfoHolder .getUser ().getUserId ());
36+ try {
37+ if (!isOpenAPIRequest (requestUri ) && !isStaticResource (requestUri )) {
38+ UserInfo userInfo = userInfoHolder .getUser ();
39+ if (userInfo != null ) {
40+ Tracer .logEvent (CatEventType .USER_ACCESS , userInfo .getUserId ());
41+ }
42+ }
43+ } catch (Throwable e ) {
44+ Tracer .logError ("Record user access info error." , e );
3545 }
3646
3747 chain .doFilter (request , response );
@@ -42,6 +52,10 @@ public void destroy() {
4252
4353 }
4454
55+ private boolean isOpenAPIRequest (String uri ) {
56+ return !Strings .isNullOrEmpty (uri ) && uri .startsWith ("/openapi" );
57+ }
58+
4559 private boolean isStaticResource (String uri ) {
4660 return !Strings .isNullOrEmpty (uri ) && uri .matches (STATIC_RESOURCE_REGEX );
4761 }
0 commit comments