package com.skr.mdm.config;

import com.skr.mdm.authentication.admin.*;
import com.skr.mdm.filter.AdminTokenFilter;
import com.skr.mdm.filter.PreventReplayFilter;
import com.skr.mdm.filter.VerificationCodeFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;


/**
 * @author nfq
 * @date 2020/6/19 16:23
 */
//@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AdminAuthenticationSuccessHandler authenticationSuccessHandler;
    @Autowired
    private AdminAuthenticationFailureHandler authenticationFailureHandler;
    @Autowired
    private AdminLogoutSuccessHandler logoutSuccessHandler;
    @Autowired
    private AdminAuthenticationEntryPoint authenticationEntryPoint;
    @Autowired
    private AdminTokenFilter tokenFilter;
    @Autowired
    private AdminDetailsServiceImpl userDetailsService;
    @Autowired
    private AccessDeniedAuthenticationHandler accessDeniedAuthenticationHandler;
    @Autowired
    private VerificationCodeFilter verificationCodeFilter;
    @Autowired
    private PreventReplayFilter preventReplayFilter;


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Override
    public void configure(WebSecurity web) throws Exception {

    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 基于token，所以不需要session
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        http.authorizeRequests()
                .antMatchers("/swagger-ui.html").permitAll()
                .antMatchers("/webjars/**").permitAll()
                .antMatchers("/v2/**").permitAll()
                .antMatchers("/swagger-resources/**").permitAll()
                .antMatchers("/druid/**").permitAll()
                .antMatchers("/**/*.js").permitAll()
                .antMatchers("/lang/*.json").permitAll()
                .antMatchers("/**/*.css").permitAll()
                .antMatchers("/doc.html").permitAll()
                .antMatchers("/druid/**").permitAll()
                .antMatchers("/favicon.ico").permitAll()
                .antMatchers("/**/*.jpg").permitAll()
                .antMatchers("/**/*.png").permitAll()
                .antMatchers("/**/*.apk").permitAll()
                .antMatchers( "/verifyCode", "/refresh/token", "websocket/**")
                .permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .successHandler(authenticationSuccessHandler)
                .failureHandler(authenticationFailureHandler)
                .and()
                .exceptionHandling().authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedAuthenticationHandler)
                .and()
                .logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler)
                .and()
                .csrf()
                .ignoringAntMatchers("/verifyCode")
                .ignoringAntMatchers("/violation/list")
                .ignoringAntMatchers("/admin/info")
                .ignoringAntMatchers("/deviceDistribution/distributionList")
                .ignoringAntMatchers("/equipmentInfo/queryEquipmentList")
                .ignoringAntMatchers("/appStore/file/progress")
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
//                .disable();
        http.addFilterBefore(tokenFilter, UsernamePasswordAuthenticationFilter.class);
        http.addFilterBefore(verificationCodeFilter, AdminTokenFilter.class);
        http.addFilterBefore(preventReplayFilter, VerificationCodeFilter.class);
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }


}
