package cn.reghao.bnt.web.admin.security.form;

import cn.reghao.bnt.web.admin.service.AccountLoginService;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;

/**
 * @author reghao
 * @date 2022-07-08 14:57:40
 */
@Component
public class AccountAuthProvider implements AuthenticationProvider {
    private final AccountLoginService accountLoginService;

    public AccountAuthProvider(AccountLoginService accountLoginService) {
        this.accountLoginService = accountLoginService;
    }

    /**
     * 对 Authentication 对象进行认证
     * 参照 DaoAuthenticationProvider
     *
     * @param
     * @return
     * @date 2020-05-06 上午11:16
     */
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        AccountAuthToken preAuthToken = (AccountAuthToken) authentication;
        AccountAuthToken authToken = accountLoginService.authByPassword(preAuthToken);;
        return authToken;
    }

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