package com.basicutils.customauth.provider; import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Component; import java.util.Collections; @Component public class CustomAuthenticationProvider implements AuthenticationProvider { @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String username = authentication.getName(); String password = authentication.getCredentials().toString(); if (externalApiAuthenticate(username, password)) { return new CustomAuthenticationToken(username, password, Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER"))); } else { throw new UsernameNotFoundException("Invalid username or password."); } } private boolean externalApiAuthenticate(String username, String password) { return "user".equals(username) && "pass".equals(password); } @Override public boolean supports(Class authentication) { return CustomAuthenticationToken.class.isAssignableFrom(authentication); } }This custom provider will authenticate users by calling the externalApiAuthenticate method, which you can replace with your external system's authentication logic.
package com.basicutils.customauth.provider; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.GrantedAuthority; import java.util.Collection; public class CustomAuthenticationToken extends UsernamePasswordAuthenticationToken { public CustomAuthenticationToken(Object principal, Object credentials) { super(principal, credentials); } public CustomAuthenticationToken(Object principal, Object credentials, Collectionextends GrantedAuthority> authorities) { super(principal, credentials, authorities); } }
package com.basicutils.customauth.config; import com.basicutils.customauth.provider.CustomAuthenticationProvider; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.security.web.SecurityFilterChain; @Configuration @EnableWebSecurity public class SecurityConfig { @Autowired private CustomAuthenticationProvider customAuthenticationProvider; @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .csrf().disable() .authorizeHttpRequests() .requestMatchers("/public/**").permitAll() .anyRequest().authenticated() .and() .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); return http.build(); } @Bean public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception { return configuration.getAuthenticationManager(); } }
simplify and inspire technology
©2024, basicutils.com