Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CustomAuthenticationProvider implements AuthenticationProvider {
- @Autowired
- AccountService accountService;
- @Override
- public Authentication authenticate(Authentication authentication) throws AuthenticationException {
- String name = authentication.getName();
- String password = authentication.getCredentials().toString();
- Account user;
- try {
- if (name.contains("@")) {
- user = accountService.findAccount(new AccountSpecificationByEmail(name));
- } else {
- user = accountService.findAccount(new AccountSpecificationByUserName(name));
- }
- } catch (AccountNotFoundException e) {
- throw new RuntimeException("Wrong login or password");
- }
- if (user.getPassword().equals(password)){
- if (user.getRoles().contains(`***chosenRole***`)){
- List<GrantedAuthority> grantedAuths = new ArrayList<>();
- grantedAuths.add(new SimpleGrantedAuthority(`***chosenRole***`));
- Authentication auth = new UsernamePasswordAuthenticationToken(name, password, grantedAuths);
- return auth;
- }
- else{
- throw new RuntimeException("Role not supported");
- }
- }
- else {
- throw new RuntimeException("Wrong login or password");
- }
- }
- @Override
- public boolean supports(Class<?> authentication) {
- return authentication.equals(UsernamePasswordAuthenticationToken.class);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement