package com.fjs.scenic.config;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import com.fjs.scenic.handler.MyAuthenticationFailureHandler;
import com.fjs.scenic.handler.MyAuthenticationSuccessHandler;
import com.fjs.scenic.handler.MyLogoutSuccessHandler;
import com.fjs.scenic.handler.MyUserDetailsService;
import com.fjs.scenic.handler.RestAuthenticationAccessDeniedHandler;
import com.fjs.scenic.service.system.PermissionService;
@EnableWebSecurity
@Component
public class SecurityConfig extends WebSecurityConfigurerAdapter {
	
	@Autowired
	private MyUserDetailsService userDetailsService;
	@Autowired
	private MyAuthenticationFailureHandler authenticationFailureHandler;
	@Autowired
	private MyAuthenticationSuccessHandler authenticationSuccessHandler;
	@Autowired
	private RestAuthenticationAccessDeniedHandler accessDeniedHandler;
	@Autowired
	private MyLogoutSuccessHandler logoutSuccessHandler;
	@Autowired
	private PermissionService permissionService;
	
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry authorizeRequests = http.authorizeRequests();
		authorizeRequests
		.antMatchers("/adminlte/**").permitAll()
		.antMatchers("/images/**").permitAll()
		.antMatchers("/login","/error/*","/logout").permitAll()
		//.antMatchers("/stop/*").permitAll()
		.antMatchers("/index").hasAnyAuthority("/index");
		List<String> urls = permissionService.getPermissionAllNotParent();
		for (String url : urls) {
			authorizeRequests.antMatchers(url).hasAnyAuthority(url);
		}
		/*.antMatchers("/index").hasAnyAuthority("index")
		.antMatchers("/manage/index").hasAnyAuthority("manage")
		.antMatchers("/permission/index").hasAnyAuthority("permission")
		.antMatchers("/role/index").hasAnyAuthority("role")
		.antMatchers("/user/list").hasAnyAuthority("list")
		.antMatchers("/user/add").hasAnyAuthority("add")
		.antMatchers("/user/update").hasAnyAuthority("update")
		.antMatchers("/user/delete").hasAnyAuthority("delete")*/
		authorizeRequests.antMatchers("/**").fullyAuthenticated()
		.and().formLogin().loginPage("/login")
		.successHandler(authenticationSuccessHandler)
		.failureHandler(authenticationFailureHandler)
		.and().exceptionHandling().accessDeniedHandler(accessDeniedHandler)
		.and().logout().logoutUrl("/logout").permitAll().invalidateHttpSession(true)
        .deleteCookies("JSESSIONID").logoutSuccessHandler(logoutSuccessHandler)
		.and().csrf().disable();
	}

	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
	}
	
	@Bean
	public BCryptPasswordEncoder passwordEncoder(){
		return new BCryptPasswordEncoder();
	}

}
