ClientCertificateFilter.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.pdd.services.connector;

  21. import java.io.IOException;

  22. import javax.servlet.Filter;
  23. import javax.servlet.FilterChain;
  24. import javax.servlet.FilterConfig;
  25. import javax.servlet.ServletException;
  26. import javax.servlet.ServletRequest;
  27. import javax.servlet.ServletResponse;
  28. import javax.servlet.http.HttpServletRequest;

  29. import org.openspcoop2.pdd.config.OpenSPCoop2Properties;
  30. import org.openspcoop2.pdd.logger.OpenSPCoop2Logger;
  31. import org.openspcoop2.utils.transport.http.HttpServletCredential;
  32. import org.openspcoop2.utils.transport.http.SSLUtilities;
  33. import org.slf4j.Logger;


  34. /**
  35.  * FormUrlEncodedFilter
  36.  *
  37.  * @author Poli Andrea (apoli@link.it)
  38.  * @author $Author$
  39.  * @version $Rev$, $Date$
  40.  */
  41. public class ClientCertificateFilter implements Filter {

  42.     /*
  43.      * NOTA: da Wilfdly 25 disabilitando l'application-security-domain name="other" non viene più popolato l'attributo dei certificati client
  44.      *
  45.      **/
  46.    
  47.     @Override
  48.     public void destroy() {
  49.     }

  50.     @Override
  51.     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
  52.             throws IOException, ServletException {
  53.        
  54.         boolean doFilter = false;
  55.         Logger log = null;
  56.         try {
  57.             OpenSPCoop2Properties op2PropertieS = OpenSPCoop2Properties.getInstance();
  58.             doFilter = op2PropertieS.isWildflyUndertowClientCertificateFilterEnabled();
  59.             log = OpenSPCoop2Logger.getLoggerOpenSPCoopCore();
  60.         }catch(Throwable t) {}
  61.        
  62.        
  63.         if(doFilter && req instanceof HttpServletRequest) {
  64.             HttpServletRequest httpServletRequest = (HttpServletRequest) req;
  65.             Object o = req.getAttribute(HttpServletCredential.SERVLET_REQUEST_X509CERTIFICATE);
  66.             if(o==null) {
  67.                 java.security.cert.X509Certificate[] certs = SSLUtilities.readCertificatesFromUndertowServlet(httpServletRequest, log);
  68.                 if(certs!=null && certs.length>0) {
  69.                     req.setAttribute(HttpServletCredential.SERVLET_REQUEST_X509CERTIFICATE, certs);
  70.                 }
  71.             }
  72.         }
  73.        
  74.         chain.doFilter(req, res);
  75.     }

  76.     @Override
  77.     public void init(FilterConfig config) throws ServletException {
  78.     }

  79. }