Answers for "Authentication custom spring"

0

spring security custom authentication provider

package com.frugalis;
 
import java.util.ArrayList;
import java.util.List;
 
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
 
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
 
	boolean shouldAuthenticateAgainstThirdPartySystem = true;
 
	@Override
	public Authentication authenticate(Authentication authentication) throws AuthenticationException {
		String name = authentication.getName();
		String password = authentication.getCredentials().toString();
 
		if (name.equals("admin") && password.equals("password")) {
			final List<GrantedAuthority> grantedAuths = new ArrayList<>();
			grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
			final UserDetails principal = new User(name, password, grantedAuths);
			final Authentication auth = new UsernamePasswordAuthenticationToken(principal, password, grantedAuths);
			return auth;
		} else {
			return null;
		}
 
	}
 
	@Override
	public boolean supports(Class<?> authentication) {
 
		return authentication.equals(UsernamePasswordAuthenticationToken.class);
	}
 
}
 
Posted by: Guest on June-19-2020

Code answers related to "Authentication custom spring"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language