Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Component
- public class CustomAuthenticationProvider implements AuthenticationProvider {
- @Autowired
- private AuthServiceImpl authService;
- @Override
- public Authentication authenticate(Authentication authentication) throws AuthenticationException {
- String username = authentication.getName();
- String password = (String) authentication.getCredentials();
- AdminUser user = (AdminUser) authService.loadUserByUsername(username);
- if (user == null || !user.getUsername().equalsIgnoreCase(username)) {
- throw new BadCredentialsException("Username not found.");
- }
- BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
- if (!passwordEncoder.matches(password, user.getPassword())) {
- throw new BadCredentialsException("Wrong password.");
- }
- Collection<? extends GrantedAuthority> authorities = user.getAuthorities();
- return new UsernamePasswordAuthenticationToken(user, password, authorities);
- }
- @Override
- public boolean supports(Class<?> aClass) {
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement