1 package info.ejava.examples.secureping.client; 2 3 import java.io.IOException; 4 import java.util.Base64; 5 6 import javax.ws.rs.client.ClientRequestContext; 7 import javax.ws.rs.client.ClientRequestFilter; 8 import javax.ws.rs.core.MultivaluedMap; 9 10 import org.slf4j.Logger; 11 import org.slf4j.LoggerFactory; 12 13 public class BasicAuthnFilter implements ClientRequestFilter { 14 private static final Logger logger = LoggerFactory.getLogger(BasicAuthnFilter.class); 15 private final String username; 16 private final String password; 17 private final String authn; 18 19 public BasicAuthnFilter(String username, String password) { 20 this.username = username; 21 this.password = password; 22 String credentials = username + ":" + password; 23 authn = "Basic " + Base64.getEncoder().encodeToString(credentials.getBytes()); 24 } 25 26 @Override 27 public void filter(ClientRequestContext requestContext) throws IOException { 28 MultivaluedMap<String, Object> headers = requestContext.getHeaders(); 29 headers.add("Authorization", authn); 30 logger.debug("added BASIC credentials[{}] for {}, {}", authn, username, password); 31 } 32 }