package com.ningdatech.pmapi.user.security.auth.credential;

import com.ningdatech.pmapi.user.security.auth.AuthProperties;
import com.ningdatech.zwdd.client.ZwddAuthClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Component;

/**
 * 账号密码登陆的认证配置
 */
@Component
public class CredentialAuthSecurityConfig
        extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

    @Autowired
    @Qualifier(value = "defaultLoginSuccessHandler")
    protected AuthenticationSuccessHandler defaultLoginSuccessHandler;

    @Autowired
    @Qualifier(value = "defaultLoginFailureHandler")
    protected AuthenticationFailureHandler defaultLoginFailureHandler;

    @Autowired
    @Qualifier(value = "credentialLoginUserDetailService")
    private UserDetailsService credentialLoginUserDetailService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private AuthProperties authProperties;

    private AuthenticationManager authenticationManager;

    @Autowired
    private ZwddAuthClient zwddAuthClient;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        CredentialAuthFilter credentialAuthFilter =
                new CredentialAuthFilter(authProperties.getPasswordLoginUrl());
        authenticationManager = http.getSharedObject(AuthenticationManager.class);
        credentialAuthFilter.setAuthenticationManager(authenticationManager);
        credentialAuthFilter.setAuthenticationSuccessHandler(defaultLoginSuccessHandler);
        credentialAuthFilter.setAuthenticationFailureHandler(defaultLoginFailureHandler);

        CredentialAuthProvider authenticationProvider = new CredentialAuthProvider();
        authenticationProvider.setUserDetailsService(credentialLoginUserDetailService);
        // 确保对密码进行加密的encoder和解密的encoder相同
        authenticationProvider.setPasswordEncoder(passwordEncoder);
        // 传入浙政钉client
        authenticationProvider.setZwddAuthClient(zwddAuthClient);

        http.authenticationProvider(authenticationProvider).addFilterAfter(credentialAuthFilter,
                UsernamePasswordAuthenticationFilter.class);
    }

    public AuthenticationManager getAuthenticationManager() {
        return authenticationManager;
    }
}
