HeaderPreAuthFilter.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.preauth.filter;

  21. import javax.servlet.http.HttpServletRequest;

  22. import org.apache.commons.lang.StringUtils;
  23. import org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter;

  24. /**
  25.  * HeaderPreAuthFilter
  26.  *
  27.  * Classe per la lettura del principal dell'utenza da un header.
  28.  * Estendere la classe e ridefinire il metodo getPrincipalHeaderName per leggere il nome dell'header da properties.
  29.  *
  30.  * @author Giuliano Pintori (pintori@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class HeaderPreAuthFilter extends RequestHeaderAuthenticationFilter {

  35.     public HeaderPreAuthFilter() {
  36.         super();
  37.         String headerAuth = this.getPrincipalHeaderName();
  38.        
  39.         // se ho definito un header name allora lo uso per leggere il principal, altrimenti il filtro usa l'header 'SM_USER'
  40.         if(StringUtils.isNotEmpty(headerAuth))
  41.             this.setPrincipalRequestHeader(headerAuth);
  42.     }

  43.     @Override
  44.     protected Object getPreAuthenticatedPrincipal(HttpServletRequest request) {
  45.         return super.getPreAuthenticatedPrincipal(request);
  46.     }

  47.     @Override
  48.     protected Object getPreAuthenticatedCredentials(HttpServletRequest request) {
  49.         return super.getPreAuthenticatedCredentials(request);
  50.     }

  51.     /**
  52.      * Restituisce il nome dell'header dove e' contenuto il principal da usare
  53.      * Ridefinire il metodo per leggere il nome dell'header per esempio da file di properties
  54.      *
  55.      * @return PrincipalHeaderName
  56.      */
  57.     protected String getPrincipalHeaderName() {
  58.         return null;
  59.     }
  60. }