spring custom auth frugalis

Solutions on MaxInterview for spring custom auth frugalis by the best coders in the world

showing results for - "spring custom auth frugalis"
Ignacio
17 Jan 2019
1package com.frugalis;
2 
3import java.util.ArrayList;
4import java.util.List;
5 
6import org.springframework.security.authentication.AuthenticationProvider;
7import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
8import org.springframework.security.core.Authentication;
9import org.springframework.security.core.AuthenticationException;
10import org.springframework.security.core.GrantedAuthority;
11import org.springframework.security.core.authority.SimpleGrantedAuthority;
12import org.springframework.security.core.userdetails.UserDetails;
13import org.springframework.security.core.userdetails.User;
14import org.springframework.stereotype.Component;
15 
16@Component
17public class CustomAuthenticationProvider implements AuthenticationProvider {
18 
19	boolean shouldAuthenticateAgainstThirdPartySystem = true;
20 
21	@Override
22	public Authentication authenticate(Authentication authentication) throws AuthenticationException {
23		String name = authentication.getName();
24		String password = authentication.getCredentials().toString();
25 
26		if (name.equals("admin") && password.equals("password")) {
27			final List<GrantedAuthority> grantedAuths = new ArrayList<>();
28			grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
29			final UserDetails principal = new User(name, password, grantedAuths);
30			final Authentication auth = new UsernamePasswordAuthenticationToken(principal, password, grantedAuths);
31			return auth;
32		} else {
33			return null;
34		}
35 
36	}
37 
38	@Override
39	public boolean supports(Class<?> authentication) {
40 
41		return authentication.equals(UsernamePasswordAuthenticationToken.class);
42	}
43 
44}
45 
46