官术网_书友最值得收藏!

Custom AuthenticationProvider

If needs be, we can write a custom AuthenticationProvider by implementing the AuthenticationProvider interface. We will have to implement two methods, namely authenticate (Authentication) and supports(Class<?> aClass):

@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication) throws
AuthenticationException {
String username = authentication.getName();
String password = authentication.getCredentials().toString();
if ("user".equals(username) && "password".equals(password)) {
return new UsernamePasswordAuthenticationToken
(username, password, Collections.emptyList());
} else {
throw new BadCredentialsException("Authentication failed");
}
}
@Override
public boolean supports(Class<?> aClass) {
return aClass.equals(UsernamePasswordAuthenticationToken.class);
}
}

Our authenticate method is quite simple. We just compare the username and password with a static value. We can write any logic here and authenticate the user. If there is an error, it throws an exception, AuthenticationException.

On the book's GitHub page, navigate to the jetty-in-memory-basic-custom-authentication project to see the full source code of this class.

主站蜘蛛池模板: 巴林右旗| 郑州市| 蓝田县| 合作市| 昌宁县| 安阳市| 尉犁县| 岱山县| 隆化县| 天台县| 平武县| 眉山市| 嘉峪关市| 宜州市| 大冶市| 封开县| 都安| 马关县| 疏勒县| 周至县| 如东县| 东兴市| 沈丘县| 荃湾区| 长子县| 称多县| 漳浦县| 德化县| 华容县| 西畴县| 台前县| 华坪县| 昭苏县| 公安县| 腾冲县| 锡林浩特市| 临沭县| 达尔| 禹城市| 香港 | 慈利县|