UserDetailsByNameServiceWrapper.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.provider;

  21. import org.springframework.beans.factory.InitializingBean;
  22. import org.springframework.security.core.Authentication;
  23. import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;
  24. import org.springframework.security.core.userdetails.UserDetails;
  25. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  26. import org.springframework.util.Assert;

  27. /**
  28.  * UserDetailsByNameServiceWrapper
  29.  *
  30.  * Classe da utilizzare per wrappare il bean che accede al db utenti.
  31.  * rispetto a quello di default di spring-security e' stato ridefinito il metodo 'loadUserDetails'
  32.  * per passare al livello sottostante l'intero token di autenticazione invece del solo nome utente.
  33.  *
  34.  * @author Giuliano Pintori (pintori@link.it)
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  */
  38. public class UserDetailsByNameServiceWrapper <T extends Authentication> implements AuthenticationUserDetailsService<T>, InitializingBean {
  39.    
  40.     private AuthenticationUserDetailsService<T> authenticationUserDetailsService = null;

  41.     /**
  42.      * Constructs an empty wrapper for compatibility with Spring Security 2.0.x's method
  43.      * of using a setter.
  44.      */
  45.     public UserDetailsByNameServiceWrapper() {
  46.         // constructor for backwards compatibility with 2.0
  47.     }

  48.     /**
  49.      * Constructs a new wrapper using the supplied
  50.      * {@link org.springframework.security.core.userdetails.UserDetailsService} as the
  51.      * service to delegate to.
  52.      *
  53.      * @param authenticationUserDetailsService the UserDetailsService to delegate to.
  54.      */
  55.     public UserDetailsByNameServiceWrapper(final AuthenticationUserDetailsService<T> authenticationUserDetailsService) {
  56.         Assert.notNull(authenticationUserDetailsService, "authenticationUserDetailsService cannot be null.");
  57.         this.authenticationUserDetailsService = authenticationUserDetailsService;
  58.     }

  59.     /**
  60.      * Check whether all required properties have been set.
  61.      *
  62.      * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
  63.      */
  64.     @Override
  65.     public void afterPropertiesSet() throws Exception {
  66.         Assert.notNull(this.authenticationUserDetailsService, "AuthenticationUserDetailsService must be set");
  67.     }

  68.     /**
  69.      * Get the UserDetails object from the wrapped UserDetailsService implementation
  70.      */
  71.     @Override
  72.     public UserDetails loadUserDetails(T authentication) throws UsernameNotFoundException {
  73.         return this.authenticationUserDetailsService.loadUserDetails(authentication);
  74.     }

  75.     /**
  76.      * Set the wrapped UserDetailsService implementation
  77.      *
  78.      * @param authenticationUserDetailsService The wrapped UserDetailsService to set
  79.      */
  80.     public void setAuthenticationUserDetailsService(AuthenticationUserDetailsService<T> authenticationUserDetailsService) {
  81.         this.authenticationUserDetailsService = authenticationUserDetailsService;
  82.     }

  83. }