Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.netcracker.unc.services;
- import com.netcracker.unc.entity.CustomUserDetails;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.security.authentication.AuthenticationProvider;
- import org.springframework.security.authentication.BadCredentialsException;
- import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
- import org.springframework.security.core.Authentication;
- import org.springframework.security.core.AuthenticationException;
- import org.springframework.security.core.GrantedAuthority;
- import org.springframework.security.core.userdetails.UserDetails;
- import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
- import org.springframework.stereotype.Component;
- import java.util.Collection;
- @Component
- public class CustomAuthenticationProvider implements AuthenticationProvider {
- @Autowired
- private UserService userService;
- @Autowired
- BCryptPasswordEncoder passwordEncoder;
- /**
- * Аутентификация пользователя по введенным паролю и имени
- *
- * @param authentication - Данные переданные пользователем
- * @return UsernamePasswordAuthenticationToken
- * @throws AuthenticationException
- */
- @Override
- public Authentication authenticate(Authentication authentication) throws AuthenticationException {
- String username = authentication.getName();
- String password = (String) authentication.getCredentials();
- UserDetails user = userService.loadUserByUsername(username);
- if ((user == null) || (!passwordEncoder.matches(password, user.getPassword()))) {
- throw new BadCredentialsException("Wrong username or password.");
- }
- //user.setPassword("");
- Collection<? extends GrantedAuthority> authorities = user.getAuthorities();
- return new UsernamePasswordAuthenticationToken(user, null, authorities);
- }
- @Override
- public boolean supports(Class<?> arg0) {
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement