package com.gxwebsoft.common.core.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
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.configuration.EnableWebSecurity;
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.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

/**
 * Spring Security配置
 *
 * @author WebSoft
 * @since 2020-03-23 18:04:52
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Resource
    private JwtAccessDeniedHandler jwtAccessDeniedHandler;
    @Resource
    private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
    @Resource
    private JwtAuthenticationFilter jwtAuthenticationFilter;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS, "/**")
                .permitAll()
                .antMatchers(HttpMethod.GET, "/api/file/**","/**", "/api/captcha", "/")
                .permitAll()
                .antMatchers(
                        "/api/login",
                        "/api/qr-login/**",
                        "/api/loginByUserId",
                        "/api/loginByEmail",
                        "/api/register",
                        "/api/grantCmsPermission",
                        "/api/superAdminRegister",
                        "/api/findAccountByPhone",
                        "/api/resetPassword",
                        "/api/checkPhoneRegistered",
                        "/api/existence",
                        "/api/oss/upload",
                        "/druid/**",
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/hxz/v1/**",
                        "/api/sendSmsCaptcha",
                        "/api/sendSmsCaptchaByAdmin",
                        "/api/sendEmailCaptcha",
                        "/api/loginBySms",
                        "/api/loginBySuperAdminSms",
                        "/api/loginBySelectTenant",
                        "/api/loginByDeveloperSms",
                        "/api/system/user/regByPhone",
                        "/api/parseToken/*",
                        "/api/login-alipay/*",
                        "/api/wx-login/loginByMpWxPhone",
                        "/api/wx-login/getAccessToken",
                        "/api/wx-login/loginByOpenId",
                        "/api/wx-login/getOpenId",
                        "/api/wx-login/getWxOpenIdOnly",
                        "/api/system/wx-native-pay/**",
                        "/api/system/wx-pay/**",
                        "/api/wxWorkQrConnect",
                        "/api/sys/user-plan-log/wx-pay/**",
                        "/api/wx-official/**",
                        "/api/system/user/loginByPhoneForTest",
                        "/api/system/user/updateUserBalanceWithoutLogin",
                        "/api/system/user/addUserBalanceWithoutLogin",
                        "/api/system/user/getUserWithoutLogin",
                        "/api/system/user/batchBackUserId",
                        "/api/system/user/getByPhone/**",
                        "/api/system/user/getByUserId/**",
                        "/api/system/user/getByUnionid/**",
                        "/api/system/user/updateUserOfficeOpenidWithoutLogin",
                        "/api/system/user/updateWithoutLogin",
                        "/api/system/user-referee/getReferee/**",
                        "/api/system/dict-data/page",
                        "/api/system/organization",
                        "/api/system/tenant/saveByPhone",
                        "/api/system/user-referee/getRefereeNum",
                        "/api/system/user-referee/getRefereeNumByUidList",
                        "/api/system/setting/getByKey/**",
                        "/api/system/setting/updateByKey/**",
                        "/lvQ4EoivKJ.txt",
                        "/api/wechat-cert-test",
                        "/MP_verify_joj96VBHPtL9YROj.txt"
                  )
                .permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .csrf()
                .disable()
                .cors()
                .and()
                .logout()
                .disable()
                .headers()
                .frameOptions()
                .disable()
                .and()
                .exceptionHandling()
                .accessDeniedHandler(jwtAccessDeniedHandler)
                .authenticationEntryPoint(jwtAuthenticationEntryPoint)
                .and()
                .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
    }

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

}
