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

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.

主站蜘蛛池模板: 红桥区| 璧山县| 岱山县| 周宁县| 苏尼特右旗| 凤凰县| 抚宁县| 班戈县| 三台县| 余姚市| 乌兰县| 江西省| 双牌县| 河北省| 葫芦岛市| 临潭县| 桐庐县| 老河口市| 神池县| 松滋市| 鹤庆县| 青神县| 华亭县| 罗城| 上杭县| 辰溪县| 黔江区| 大渡口区| 康定县| 界首市| 奈曼旗| 绥德县| 泾川县| 老河口市| 阜宁县| 阿坝| 宁武县| 万州区| 永清县| 石棉县| 赣榆县|