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

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.

主站蜘蛛池模板: 徐州市| 咸宁市| 精河县| 西峡县| 特克斯县| 惠州市| 灌阳县| 岐山县| 宁河县| 临西县| 辽阳县| 明溪县| 周口市| 巫溪县| 白玉县| 浠水县| 德惠市| 潜山县| 车致| 德保县| 吴川市| 繁昌县| 浦城县| 金山区| 定州市| 江孜县| 九龙坡区| 茌平县| 芜湖县| 疏附县| 嘉兴市| 南部县| 乌拉特后旗| 武乡县| 石城县| 广汉市| 彝良县| 唐山市| 襄汾县| 建平县| 北票市|