package cn.com.capcloud.core.system.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.ObjectPostProcessor;
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.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.crypto.password.PasswordEncoder;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;

import org.springframework.beans.factory.annotation.Autowired;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  @Autowired
  private JwtTokenProvider jwtTokenProvider;

  @Autowired
  private UrlFilterInvocationSecurityMetadataSource urlFilterInvocationSecurityMetadataSource;

  @Autowired
  private UrlAccessDecisionManager urlAccessDecisionManager;

  @Override
  protected void configure(HttpSecurity http) throws Exception {

    // 关闭跨站请求防护
    http.csrf().disable();
    // 前后端分离采用JWT 不需要session
    http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    // 验证是否需要签名才能访问资源
    http.authorizeRequests()
            .anyRequest().authenticated()
            .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
              public <O extends FilterSecurityInterceptor> O postProcess(
                      O fsi) {
                fsi.setSecurityMetadataSource(urlFilterInvocationSecurityMetadataSource);
                fsi.setAccessDecisionManager(urlAccessDecisionManager);
                return fsi;
              }
            });
    // 如果用户在没有足够权限的情况下尝试访问资源
    // 增加是否登录过滤
    http.apply(new JwtTokenFilterConfigurer(jwtTokenProvider));
    // 如果要从浏览器测试API，则为可选
    //http.httpBasic();

  }

  @Override
  public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/v2/api-docs")//
        .antMatchers("/swagger-resources/**")//
        .antMatchers("/swagger-ui.html")//
        .antMatchers("/configuration/**")//
        .antMatchers("/webjars/**")//
        .antMatchers("/public")
        .and()
        .ignoring()
        .antMatchers("/h2-console/**/**");;
  }

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

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

}

