HTTPBasicAuthFilter.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */

  20. package org.openspcoop2.utils.service.authentication.filter;

  21. import javax.ws.rs.client.ClientRequestContext;
  22. import javax.ws.rs.client.ClientRequestFilter;
  23. import javax.ws.rs.core.MultivaluedMap;

  24. import org.openspcoop2.utils.io.Base64Utilities;
  25. import org.openspcoop2.utils.transport.http.HttpConstants;

  26. /**
  27.  * HTTPBasicAuthFilter
  28.  *
  29.  * @author Giuliano Pintori (pintori@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class HTTPBasicAuthFilter implements ClientRequestFilter {

  34.     private final String user;
  35.     private final String password;

  36.     public HTTPBasicAuthFilter(String user, String password) {
  37.         this.user = user;
  38.         this.password = password;
  39.     }

  40.     @Override
  41.     public void filter(ClientRequestContext requestContext) {
  42.         MultivaluedMap<String, Object> headers = requestContext.getHeaders();
  43.         final String basicAuthentication = getBasicAuthentication();
  44.         headers.add(HttpConstants.AUTHORIZATION, basicAuthentication);

  45.     }

  46.     private String getBasicAuthentication() {
  47.         String token = this.user + ":" + this.password;
  48.         return HttpConstants.AUTHORIZATION_PREFIX_BASIC+ Base64Utilities.encodeAsString(token.getBytes());
  49.     }
  50. }