package com.yfd.platform.config;

import com.yfd.platform.config.bean.LoginProperties;
import com.yfd.platform.exception.AccessDeniedHandExcetion;
import com.yfd.platform.exception.MyAuthenticationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
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;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    // 注入了加密算法
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @ConfigurationProperties(prefix = "login", ignoreUnknownFields = true)
    public LoginProperties loginProperties() {
        return new LoginProperties();
    }


    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Autowired
    private MyAuthenticationException authenticationException;

    @Autowired
    private AccessDeniedHandExcetion accessDeniedHandExcetion;


    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                // 关闭 CSRF
                .csrf().disable()
                //不通过Session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                //对于登录接口，允许匿名访问
                .authorizeRequests()
                .antMatchers("/user/login").anonymous()
                .antMatchers("/user/code").permitAll()
                .antMatchers("/system/user/updatePassword").permitAll()
                .antMatchers("/user/kickOutUser").permitAll()
                .and()
                .authorizeRequests()
                // 放行静态资源
                .antMatchers(
                        HttpMethod.GET,
                        "/*.html",
                        "/**/*.html",
                        "/**/*.css",
                        "/**/*.js",
                        "/webSocket/**"
                ).permitAll()
                // 放行 swagger 文档
                .antMatchers("/swagger-ui.html").permitAll()
                .antMatchers("/swagger-resources/**").permitAll()
                .antMatchers("/webjars/**").permitAll()
                .antMatchers("/*/api-docs").permitAll()

                // 放行 图片预览
                .antMatchers("/report/**").permitAll()
                .antMatchers("/images/**").permitAll()
                .antMatchers("/pageimage/**").permitAll()
                .antMatchers("/avatar/**").permitAll()
                .antMatchers("/menu/**").permitAll()
                .antMatchers("/station/**").permitAll()
                .antMatchers("/temp/**").permitAll()
                .antMatchers("/systemurl/**").permitAll()
                .antMatchers("/api/imageserver/upload").permitAll()
                //测试放行所有访问
                .antMatchers("/**/**").permitAll()
                //除上面外的所有请求全部需要签权认证
                .anyRequest().authenticated();

        //允许跨域
        httpSecurity.cors();

        //将jwt过来器加入到httpSecurity过滤器链中
        httpSecurity.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

        //配置异常处理器
        httpSecurity.exceptionHandling()
                .authenticationEntryPoint(authenticationException)
                .accessDeniedHandler(accessDeniedHandExcetion);
    }
}
