Recabamos por todo el mundo online para así regalarte la respuesta para tu problema, en caso de alguna difcultad deja tu pregunta y responderemos sin falta, porque estamos para ayudarte.
Ejemplo 1: proveedor de autenticación personalizado de Spring Security
packagecom.frugalis;importjava.util.ArrayList;importjava.util.List;importorg.springframework.security.authentication.AuthenticationProvider;importorg.springframework.security.authentication.UsernamePasswordAuthenticationToken;importorg.springframework.security.core.Authentication;importorg.springframework.security.core.AuthenticationException;importorg.springframework.security.core.GrantedAuthority;importorg.springframework.security.core.authority.SimpleGrantedAuthority;importorg.springframework.security.core.userdetails.UserDetails;importorg.springframework.security.core.userdetails.User;importorg.springframework.stereotype.Component;@ComponentpublicclassCustomAuthenticationProviderimplementsAuthenticationProviderboolean shouldAuthenticateAgainstThirdPartySystem =true;@OverridepublicAuthenticationauthenticate(Authentication authentication)throwsAuthenticationExceptionString name = authentication.getName();String password = authentication.getCredentials().toString();if(name.equals("admin")&& password.equals("password"))finalList<GrantedAuthority> grantedAuths =newArrayList<>();
grantedAuths.add(newSimpleGrantedAuthority("ROLE_USER"));finalUserDetails principal =newUser(name, password, grantedAuths);finalAuthentication auth =newUsernamePasswordAuthenticationToken(principal, password, grantedAuths);return auth;elsereturnnull;@Overridepublicbooleansupports(Class<?> authentication)return authentication.equals(UsernamePasswordAuthenticationToken.class);
Ejemplo 2: ejemplo de autenticación de clave de API de Spring Boot
importorg.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter;publicclassAPIKeyAuthFilterextendsAbstractPreAuthenticatedProcessingFilterprivateString principalRequestHeader;publicAPIKeyAuthFilter(String principalRequestHeader)this.principalRequestHeader = principalRequestHeader;@OverrideprotectedObjectgetPreAuthenticatedPrincipal(HttpServletRequest request)return request.getHeader(principalRequestHeader);@OverrideprotectedObjectgetPreAuthenticatedCredentials(HttpServletRequest request)return"N/A";
Ejemplo 3: proveedor de autenticación personalizada de Spring Security
packagecom.frugalis;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.annotation.ComponentScan;importorg.springframework.context.annotation.Configuration;importorg.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;importorg.springframework.security.config.annotation.web.builders.HttpSecurity;importorg.springframework.security.config.annotation.web.configuration.EnableWebSecurity;importorg.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@Configuration@EnableWebSecurity@ComponentScan("com.frugalis")publicclassCustAuthProviderConfigextendsWebSecurityConfigurerAdapter@AutowiredprivateCustomAuthenticationProvider authProvider;@Overrideprotectedvoidconfigure(AuthenticationManagerBuilder auth)throwsException
auth.authenticationProvider(authProvider);@Overrideprotectedvoidconfigure(HttpSecurity http)throwsException
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
Calificaciones y reseñas
Si piensas que ha sido provechoso este artículo, agradeceríamos que lo compartas con otros juniors de esta manera contrubuyes a dar difusión a esta información.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)