package ink.wgink.login.base.security.user;

import ink.wgink.interfaces.expand.login.ILoginHandlerService;
import ink.wgink.interfaces.user.mongo.IMongoLoginUserService;
import ink.wgink.login.base.authentication.user.UserAuthenticationFilter;
import ink.wgink.login.base.authentication.user.UserAuthenticationProvider;
import ink.wgink.login.base.handler.LoginFailureHandler;
import ink.wgink.login.base.service.user.UserDetailServiceImpl;
import ink.wgink.login.base.service.user.UserLoginService;
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.crypto.password.PasswordEncoder;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.servlet.http.HttpSession;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: UserSecurityConfig
 * @Description: 用户安全
 * @Author: WangGeng
 * @Date: 2020/5/25 10:59 下午
 * @Version: 1.0
 **/
public class UserSecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

    private UserDetailServiceImpl userDetailService;
    private LoginFailureHandler loginFailureHandler;
    private String loginProcessUrl;
    private PasswordEncoder passwordEncoder;
    private UserLoginService userLoginService;
    private ILoginHandlerService loginHandler;
    private HttpSession httpSession;
    private IMongoLoginUserService mongoLoginUserService;

    @Override
    public void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        // 创建过滤器
        UserAuthenticationFilter userAuthenticationFilter = new UserAuthenticationFilter(loginProcessUrl);
        userAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
        userAuthenticationFilter.setAuthenticationFailureHandler(loginFailureHandler);
        // 创建provider，加入UserDetailService
        UserAuthenticationProvider userAuthenticationProvider = new UserAuthenticationProvider();
        userAuthenticationProvider.setPasswordEncoder(passwordEncoder);
        userAuthenticationProvider.setUserDetailsService(userDetailService);
        userAuthenticationProvider.setUserLoginService(userLoginService);
        userAuthenticationProvider.setHttpSession(httpSession);
        userAuthenticationProvider.setLoginHandler(loginHandler);
        userAuthenticationProvider.setMongoLoginUserService(mongoLoginUserService);
        // 加入SpringSecurity的authentication管理的provider集合当中，并且添加到UsernamePasswordAuthenticationFilter之前
        http.authenticationProvider(userAuthenticationProvider).addFilterBefore(userAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
    }

    public void setUserDetailService(UserDetailServiceImpl userDetailService) {
        this.userDetailService = userDetailService;
    }

    public void setLoginFailureHandler(LoginFailureHandler loginFailureHandler) {
        this.loginFailureHandler = loginFailureHandler;
    }

    public void setLoginProcessUrl(String loginProcessUrl) {
        this.loginProcessUrl = loginProcessUrl;
    }

    public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }

    public void setUserLoginService(UserLoginService userLoginService) {
        this.userLoginService = userLoginService;
    }

    public void setLoginHandler(ILoginHandlerService loginHandler) {
        this.loginHandler = loginHandler;
    }


    public void setHttpSession(HttpSession httpSession) {
        this.httpSession = httpSession;
    }

    public void setMongoLoginUserService(IMongoLoginUserService mongoLoginUserService) {
        this.mongoLoginUserService = mongoLoginUserService;
    }
}
