-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathBasicAuthClientRequestFilter.java
36 lines (28 loc) · 1.66 KB
/
BasicAuthClientRequestFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*******************************************************************************
* Copyright (c) 2020 Composent, Inc. and others. All rights reserved. This
* program and the accompanying materials are made available under the terms of
* the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Composent, Inc. - initial API and implementation
******************************************************************************/
package org.eclipse.ecf.example.jersey.client.basicauth;
import java.io.IOException;
import java.util.Base64;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import org.osgi.service.component.annotations.Component;
@Component(immediate=true,property = {"jaxrs-service-exported-config-target=ecf.jaxrs.jersey.client" })
public class BasicAuthClientRequestFilter implements ClientRequestFilter {
private static final String AUTHORIZATION_PROPERTY = "Authorization";
private static final String AUTHENTICATION_SCHEME = "Basic ";
private static final String testUsername = System.getProperty("rs.basicauth.username", "testusername");
private static final String testPassword = System.getProperty("rs.basicauth.password", "testpassword");
@Override
public void filter(ClientRequestContext clientRequestContext) throws IOException {
System.out.println("ContainerRequestFilter.filter for uris="+clientRequestContext.getUri());
clientRequestContext.getHeaders().add(AUTHORIZATION_PROPERTY, AUTHENTICATION_SCHEME
+ Base64.getEncoder().encodeToString(new String(testUsername + ":" + testPassword).getBytes()));
}
}