custom authentication provider sample
This commit is contained in:
parent
7175867d2d
commit
a2e7741d75
|
@ -0,0 +1,42 @@
|
||||||
|
package org.baeldung.security;
|
||||||
|
|
||||||
|
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.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class CustomAuthenticationProvider implements AuthenticationProvider {
|
||||||
|
|
||||||
|
public CustomAuthenticationProvider() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
// API
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Authentication authenticate(final Authentication authentication) throws AuthenticationException {
|
||||||
|
final String name = authentication.getName();
|
||||||
|
final String password = authentication.getCredentials().toString();
|
||||||
|
if (name.equals("admin") && password.equals("system")) {
|
||||||
|
final List<GrantedAuthority> grantedAuths = new ArrayList<>();
|
||||||
|
grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
|
||||||
|
final Authentication auth = new UsernamePasswordAuthenticationToken(name, password, grantedAuths);
|
||||||
|
return auth;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supports(final Class<?> authentication) {
|
||||||
|
return authentication.equals(UsernamePasswordAuthenticationToken.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -18,12 +18,7 @@
|
||||||
<beans:bean id="mySuccessHandler" class="org.baeldung.security.MySavedRequestAwareAuthenticationSuccessHandler" />
|
<beans:bean id="mySuccessHandler" class="org.baeldung.security.MySavedRequestAwareAuthenticationSuccessHandler" />
|
||||||
|
|
||||||
<authentication-manager alias="authenticationManager">
|
<authentication-manager alias="authenticationManager">
|
||||||
<authentication-provider>
|
<authentication-provider ref="customAuthenticationProvider" />
|
||||||
<user-service>
|
|
||||||
<user name="temporary" password="temporary" authorities="ROLE_ADMIN" />
|
|
||||||
<user name="user" password="userPass" authorities="ROLE_USER" />
|
|
||||||
</user-service>
|
|
||||||
</authentication-provider>
|
|
||||||
</authentication-manager>
|
</authentication-manager>
|
||||||
|
|
||||||
</beans:beans>
|
</beans:beans>
|
Loading…
Reference in New Issue