package com.tsiwebserver.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.config.annotation.web.configurers.FormLoginConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer.AuthorizedUrl;

import com.tsiwebserver.service.impl.LoginServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.security.web.header.writers.frameoptions.WhiteListedAllowFromStrategy;
import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter;

import java.util.Arrays;

@Configuration
@EnableWebSecurity
@AllArgsConstructor
@EnableGlobalMethodSecurity(securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
	private final LoginServiceImpl loginService;
	
	@Override
	protected void configure(HttpSecurity http) throws Exception, RuntimeException {
		http.headers().frameOptions().sameOrigin()
		    .and().authorizeRequests()
				.antMatchers(new String[]{"/", "/showlogin.do", "/login.do", "/denied.do",
						"/info", "/actuator", "/info/**", "/actuator/**", "/gradeFailed.do", "/test"}).permitAll()
				.antMatchers(new String[]{
					"/dashBoard.do",
					"/history.do",
					"/statistics.do",
					"/node/**",
					"/consumer/**",
					"/alarm/**",
				}).hasAnyAuthority(new String[]{"N", "M", "I"})
				.antMatchers(new String[]{"/systemManagement.do", "/home.do", "/base/**", "/upload",
						"/files", "/files/**", "/files/**/**"})
				.hasAnyAuthority(new String[]{"N", "M", "I", "B"})
				.antMatchers(new String[]{
					"/user/**",
				}).hasAnyAuthority(new String[]{"I"})
				.anyRequest().authenticated()
			.and().formLogin()
				  .loginPage("/showlogin.do")
				  .failureUrl("/denied.do")
				  .usernameParameter("userId")
				  .passwordParameter("userPswd")
				  .permitAll()
			.and().logout()
				  .logoutUrl("/logout.do")
				  .invalidateHttpSession(true)
				  .logoutSuccessUrl("/showlogin.do");
		http.csrf().ignoringAntMatchers("/", "/api/**", "/showlogin.do", "/history.do",
				"/login.do", "/denied.do", "/dashBoard.do", "/systemManagement.do", "/upload", "/files", "/files/**", "/files/**/**");
//		http.csrf().ignoringAntMatchers("/api/**");
	}

	@Bean
	public TSIEncoder passwordEncoder() {
		return new TSIEncoder();
	}

	@Override
	public void configure(WebSecurity web) throws Exception, RuntimeException {
		web.ignoring().antMatchers("/css/**", "/js/**", "/image/**");
	}

	public void configure(AuthenticationManagerBuilder auth) throws Exception, RuntimeException {
		auth.userDetailsService(loginService).passwordEncoder(passwordEncoder());
	}
}
