48
48
import java .util .logging .Logger ;
49
49
50
50
/**
51
- * This filter allows the request in case it contains the correct authentication token
51
+ * This filter allows the request in case it contains the correct authentication bearer token
52
52
* (needs to come in via HTTPS) or it is coming from localhost or its path matches the list
53
53
* of built in paths.
54
+ * If the request does not contain valid token and appears to come from localhost however is proxied
55
+ * (contains either X-Forwarded-For or Forwarded HTTP headers) it is denied.
54
56
*/
55
57
@ Provider
56
58
@ PreMatching
@@ -76,6 +78,8 @@ public class IncomingFilter implements ContainerRequestFilter {
76
78
"127.0.0.1" , "0:0:0:0:0:0:0:1" , "localhost"
77
79
));
78
80
81
+ static final String BEARER = "Bearer " ; // Authorization header value prefix
82
+
79
83
@ PostConstruct
80
84
public void init () {
81
85
try {
@@ -97,11 +101,14 @@ public void filter(final ContainerRequestContext context) {
97
101
String path = context .getUriInfo ().getPath ();
98
102
99
103
if (request .isSecure ()) {
100
- String authHeader ;
101
- if (((authHeader = request .getHeader (HttpHeaders .AUTHORIZATION )) != null ) &&
102
- RuntimeEnvironment .getInstance ().getAuthenticationTokens ().contains (authHeader )) {
103
- logger .log (Level .FINEST , "allowing request to {0} based on authentication header" , path );
104
- return ;
104
+ String authHeaderValue ;
105
+ if ((authHeaderValue = request .getHeader (HttpHeaders .AUTHORIZATION )) != null &&
106
+ authHeaderValue .startsWith (BEARER )) {
107
+ String tokenValue = authHeaderValue .substring (BEARER .length ());
108
+ if (RuntimeEnvironment .getInstance ().getAuthenticationTokens ().contains (tokenValue )) {
109
+ logger .log (Level .FINEST , "allowing request to {0} based on authentication header token" , path );
110
+ return ;
111
+ }
105
112
}
106
113
}
107
114
0 commit comments