package com.community.huiju.security;

import com.community.huiju.constant.SecurityParametes;
import com.community.huiju.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * 配置类
 * @author weiximei
 */
//@Configuration
//@EnableWebSecurity
public class WisdomSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private IRoleService iRoleService;

    @Autowired
    private SecurityParametes parametes;

    @Autowired
    private HttpServletRequest request;

    /**
     * 配置预授权
     * @return
     * @throws Exception
     */
    private RestPreAuthenticatedProcessingFilter getPreAuthenticatedProcessingFilter() throws Exception {
        RestPreAuthenticatedProcessingFilter filter = new RestPreAuthenticatedProcessingFilter(parametes.getNoneSercurityPath(),iRoleService);
        filter.setAuthenticationManager(this.authenticationManagerBean());
        return filter;
    }

    /**
     * 安全策略
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable();
        http.authenticationProvider(new RestPreAuthenticateProvider(request, iRoleService))
                .authorizeRequests()
                .antMatchers(parametes.getNoneSercurityPath().toArray(new String[parametes.getNoneSercurityPath().size()])).permitAll() // 不要授权
                .anyRequest()// 任何请求需要授权
                .authenticated() // 需要一个身份
//                .antMatchers(parametes.getAppSercurityPath().get(0).split("=")[1]).hasRole(parametes.getAppSercurityPath().get(0).split("=")[0])
//                .antMatchers(parametes.getAppSercurityPath().get(1).split("=")[1]).hasRole(parametes.getAppSercurityPath().get(1).split("=")[0])
//                .antMatchers(parametes.getAppSercurityPath().get(2).split("=")[1]).hasRole(parametes.getAppSercurityPath().get(2).split("=")[0])
//                .antMatchers("/app-api/tickets/**").hasRole("OWNER")
               .and()
                .sessionManagement().sessionFixation().none()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
               .and()
                .httpBasic().authenticationEntryPoint(new RestAuthenticationEntryPoint())
               .and()
                .addFilter(getPreAuthenticatedProcessingFilter());


    }

    /**
     * web安全链
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        // 忽略
        web.ignoring().antMatchers(HttpMethod.OPTIONS,"/**");
        //       .antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources", "/configuration/security", "/swagger-ui.html", "/webjars/**","/swagger-resources/configuration/ui","/swagge‌​r-ui.html");
        //放过swagger

    }
}
