package com.sa.ibiz.demo.workflow.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.OrRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;

@Configuration
@Order(1)
public class FlowableSecurityConfig extends WebSecurityConfigurerAdapter {

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

        httpSecurity
        // 禁用 CSRF
        .csrf().disable()
                // 过滤请求
                .authorizeRequests()

              //Flowable增加
                .antMatchers("/**/images/**").permitAll()
                .antMatchers("/modeler/**/*.xml").permitAll()
                .antMatchers("/**/*.png").permitAll()
                .antMatchers("/**/*.json").permitAll()
                .antMatchers("/api/**").permitAll()
                .antMatchers("/**/diagram").permitAll()
                .antMatchers("/**/diagram/**").permitAll()
                .antMatchers("/modeller/**").permitAll()
                .antMatchers("/app/rest/**").permitAll()
                .antMatchers("/repository/**").permitAll()
//                .anyRequest().authenticated() 
                ;
        
        String[] ignoPath = {"/**/images/**","/modeler/**/*.xml","/**/*.png","/**/*.json","/api/**","/**/diagram","/**/diagram/**","/modeller/**","/app/rest/**","/repository/**"} ;
        
        List<RequestMatcher> ants = new ArrayList<RequestMatcher>() ;
        for (String string : ignoPath) {
             AntPathRequestMatcher ant = new AntPathRequestMatcher(string) ;
             ants.add(ant) ;
		}
        OrRequestMatcher or = new OrRequestMatcher(ants) ;
        httpSecurity.requestMatcher(or) ;
    }
}
