2020package com .baidu .hugegraph .filter ;
2121
2222import java .io .IOException ;
23+ import java .io .PrintWriter ;
24+ import java .util .Set ;
25+ import java .util .function .Supplier ;
2326
2427import javax .annotation .Resource ;
2528import javax .servlet .Filter ;
2932import javax .servlet .ServletResponse ;
3033import javax .servlet .annotation .WebFilter ;
3134import javax .servlet .http .HttpServletRequest ;
35+ import javax .ws .rs .core .MediaType ;
3236
3337import org .apache .commons .lang .StringUtils ;
3438import org .apache .http .HttpHeaders ;
39+ import org .springframework .web .bind .annotation .RequestMethod ;
3540
41+ import com .baidu .hugegraph .common .Constant ;
42+ import com .baidu .hugegraph .common .Response ;
3643import com .baidu .hugegraph .driver .HugeClient ;
3744import com .baidu .hugegraph .config .AuthClientConfiguration ;
45+ import com .baidu .hugegraph .util .JsonUtil ;
46+ import com .google .common .collect .ImmutableSet ;
3847
3948import lombok .extern .log4j .Log4j2 ;
4049
4150@ Log4j2
4251@ WebFilter (filterName = "authFilter" , urlPatterns = "/*" )
4352public class AuthFilter implements Filter {
4453
54+ private static final String BEARER_TOKEN_PREFIX = "Bearer " ;
55+
56+ private static final Set <String > WHITE_API = ImmutableSet .of (
57+ buildPath (RequestMethod .POST ,
58+ Constant .API_VERSION + "graph-connections/login" )
59+ );
60+
4561 @ Resource (name = AuthClientConfiguration .AUTH_CLIENT_NAME )
4662 private HugeClient client ;
4763
@@ -51,15 +67,65 @@ public void doFilter(ServletRequest servletRequest,
5167 FilterChain filterChain )
5268 throws IOException , ServletException {
5369 try {
54- String authorization = ((HttpServletRequest ) servletRequest )
55- .getHeader (HttpHeaders .AUTHORIZATION );
56- if (StringUtils .isNotEmpty (authorization )) {
57- this .client .setAuthContext (authorization );
70+ HttpServletRequest request = (HttpServletRequest ) servletRequest ;
71+ String authorization = request .getHeader (HttpHeaders .AUTHORIZATION );
72+
73+ // Missed token and request uri not in white list
74+ if (StringUtils .isEmpty (authorization ) && !isWhiteAPI (request )) {
75+ String msg = "Missed authorization token" ;
76+ writeResponse (servletResponse , () -> {
77+ return Response .builder ()
78+ .status (Constant .STATUS_BAD_REQUEST )
79+ .message (msg )
80+ .build ();
81+ });
82+ return ;
83+ }
84+ // Illegal token format
85+ if (StringUtils .isNotEmpty (authorization ) &&
86+ !authorization .startsWith (BEARER_TOKEN_PREFIX )) {
87+ String msg = "Only HTTP Bearer authentication is supported" ;
88+ writeResponse (servletResponse , () -> {
89+ return Response .builder ()
90+ .status (Constant .STATUS_BAD_REQUEST )
91+ .message (msg )
92+ .build ();
93+ });
94+ return ;
5895 }
5996
97+ this .client .setAuthContext (authorization );
98+
6099 filterChain .doFilter (servletRequest , servletResponse );
61100 } finally {
62101 this .client .resetAuthContext ();
63102 }
64103 }
104+
105+ private static String buildPath (RequestMethod method , String path ) {
106+ return buildPath (method .name (), path );
107+ }
108+
109+ private static String buildPath (String method , String path ) {
110+ return String .join (":" , method , path );
111+ }
112+
113+ private static boolean isWhiteAPI (HttpServletRequest request ) {
114+ String url = request .getRequestURI ();
115+ return WHITE_API .contains (buildPath (request .getMethod (), url ));
116+ }
117+
118+ private void writeResponse (ServletResponse servletResponse ,
119+ Supplier <Response > responseSupplier ) {
120+ Response response = responseSupplier .get ();
121+
122+ servletResponse .setCharacterEncoding ("UTF-8" );
123+ servletResponse .setContentType (MediaType .APPLICATION_JSON );
124+
125+ try (PrintWriter writer = servletResponse .getWriter ()) {
126+ writer .print (JsonUtil .toJson (response ));
127+ } catch (IOException e ) {
128+ log .error ("Response error" ,e );
129+ }
130+ }
65131}
0 commit comments