带有passwordEncoder的AuthenticationProvider

时间:2018-12-24 11:34:44

标签: spring-boot encoding spring-security oauth-2.0

当我尝试使用密码编码器进行检查时。它没有用,我看到“密码错误”。 我应该如何接收authentication.getCredentials()来检查密码?

如果我保存未编码的用户并尝试登录,则可以。

@Component
public class AuthClient implements AuthenticationProvider {

    @Autowired
    private ClientRepository clientRepository;

    @Autowired
    PasswordEncoder passwordEncoder;


    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {


        String username = authentication.getName();
        String checkPassword = passwordEncoder.encode(authentication.getCredentials().toString());

        Client client = this.clientRepository.findByUsername(username);

        if (client == null) {
            throw new UsernameNotFoundException("Invalid username/password");
        }

        String password = client.getPassword();
        if (!password.equals(checkPassword)) {
            throw new BadCredentialsException("Bad password");
        }

        Collection<? extends GrantedAuthority> authorities = translate();

        return new UsernamePasswordAuthenticationToken(username,password,authorities);

    }

    private Collection<? extends GrantedAuthority> translate() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_CLIENT"));
        return authorities;
    }


    @Override
    public boolean supports(Class<?> authentication) {
        return (UsernamePasswordAuthenticationToken.class
                .isAssignableFrom(authentication));
    }
}

1 个答案:

答案 0 :(得分:0)

如果您要测试两个密码,则您做错了,您需要像这样使用passwordEncoder:

var db = firebase.database().ref();
var path = 'rooms/LUUZxjzhMQ9xf7d2-lU';
db.child(path).orderByChild('seats').equalTo('mike').remove();
相关问题