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

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.

主站蜘蛛池模板: 平和县| 龙海市| 阿鲁科尔沁旗| 乌什县| 综艺| 绩溪县| 桂阳县| 宿松县| 深水埗区| 乌鲁木齐县| 柳河县| 鄄城县| 库车县| 长治县| 富平县| 公主岭市| 临猗县| 绥江县| 任丘市| 棋牌| 合水县| 阿勒泰市| 葫芦岛市| 瑞安市| 响水县| 通海县| 叶城县| 平定县| 石城县| 滦南县| 松江区| 安塞县| 吴江市| 武义县| 新和县| 黄冈市| 湘潭市| 军事| 阳泉市| 凤庆县| 阿拉尔市|