package com.ningdatech.carapi.user.security.auth.password;

import com.ningdatech.cache.repository.CachePlusOps;
import com.ningdatech.carapi.user.security.auth.AuthProperties;
import com.ningdatech.carapi.user.service.INdUserAuthService;
import com.ningdatech.carapi.user.service.INdUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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 UsernamePasswordAuthSecurityConfig
        extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

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

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

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

    @Autowired
    private INdUserInfoService ndUserInfoService;

    @Autowired
    private CachePlusOps cachePlusOps;

    @Autowired
    private INdUserAuthService ndUserAuthService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private AuthProperties authProperties;

    private AuthenticationManager authenticationManager;

    @Value("${login.sms-check}")
    private boolean smsChcek;

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

        UsernamePasswordAuthProvider authenticationProvider = new UsernamePasswordAuthProvider();
        authenticationProvider.setUserDetailsService(passwordLoginUserDetailService);
        authenticationProvider.setNdUserInfoService(ndUserInfoService);
        authenticationProvider.setCachePlusOps(cachePlusOps);
        authenticationProvider.setNdUserAuthService(ndUserAuthService);
        authenticationProvider.setSmsCheck(smsChcek);
        // 确保对密码进行加密的encoder和解密的encoder相同
        authenticationProvider.setPasswordEncoder(passwordEncoder);
        http.authenticationProvider(authenticationProvider).addFilterAfter(usernamePasswordAuthFilter,
                UsernamePasswordAuthenticationFilter.class);
    }

    public AuthenticationManager getAuthenticationManager() {
        return authenticationManager;
    }
}
