- Hands-On Spring Security 5 for Reactive Applications
- Tomcy John
- 140字
- 2021-07-23 18:59:19
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.
推薦閱讀
- CTF實(shí)戰(zhàn):技術(shù)、解題與進(jìn)階
- Getting Started with FortiGate
- 計(jì)算機(jī)病毒原理與防范(第2版)
- Computer Forensics with FTK
- 從實(shí)踐中學(xué)習(xí)Kali Linux滲透測(cè)試
- 學(xué)電腦安全與病毒防范
- CTF特訓(xùn)營(yíng):技術(shù)詳解、解題方法與競(jìng)賽技巧
- 信息內(nèi)容安全管理及應(yīng)用
- 計(jì)算機(jī)系統(tǒng)與網(wǎng)絡(luò)安全研究
- Practical Mobile Forensics
- 社會(huì)工程:防范釣魚(yú)欺詐(卷3)
- ATT&CK與威脅獵殺實(shí)戰(zhàn)
- BeagleBone for Secret Agents
- 大話數(shù)據(jù)恢復(fù)
- Hands-On Spring Security 5 for Reactive Applications