package nlib.security;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
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.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;

import nlib.cmm.crypto.NlibPasswordEncoder;
import nlib.cmm.service.NlibProperty;

/**
 * <pre>
 * @Class Name : SpringSecurityConfig.java
 * 
 * @Description : Spring Security 환경설정 클래스 
 * 
 * 
 * @프로젝트명: 지방문화원 통합자료관리시스템 구축사업 (2021)
 *
 * </pre>
 *
 * @ ------------    --------    ---------------------------
 * @ 수정일                     수정자             수정내용
 * @ ------------    --------    ---------------------------
 * @ 2021. 7. 6.    KNKIM       최초 생성
 *
 * 
 * @author 이씨플라자 * DIGITALSHIP KNKIM
 * @since 2021. 7. 6.
 * @version 1.0
 *
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

	@Override
	public void configure(WebSecurity web) throws Exception {
		
		// 권한/인증과 무관한 오픈된 정적 리소트 
		web.ignoring()
			.antMatchers("/resources/**")
			.antMatchers("/css/**")
			.antMatchers("/images/**")
			.antMatchers("/js/**")
			.antMatchers("/temp/**")
			.antMatchers("/favicon/**")
		;
	}
	
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.authorizeRequests()
			.antMatchers("/login/**").permitAll()
			.antMatchers("/member/*.do").permitAll()
			.antMatchers("/member/*.ajax").permitAll()
			.antMatchers("/member/naver/*.do").permitAll()
			.antMatchers("/member/google/*.do").permitAll()
			.antMatchers("/member/kakao/*.do").permitAll()
			.antMatchers("/userInfo/*.do").permitAll()
			.antMatchers("/info/inform/*.do").permitAll()
			.antMatchers("/").permitAll()
			.antMatchers("/index.jsp").permitAll()
			.antMatchers("/index.do").permitAll()
			.antMatchers("/main/header.do").permitAll()
			.antMatchers("/main/menu.do").permitAll()
			.antMatchers("/rent/*.do").permitAll()
			.antMatchers("/collection/*.do").permitAll()
			.antMatchers("/sample/password/getSampleEncoder.do").permitAll()
			.antMatchers("/*/*Ajax.do").permitAll()
			.antMatchers("/fileupload/**").permitAll()
			.antMatchers("/board/**").permitAll()
			.antMatchers("/inform/**").permitAll()
			.antMatchers("/alert/**").permitAll()
			.antMatchers("/code/**").permitAll()
			.antMatchers("/homes/**").permitAll()
			.antMatchers("/homes/**").permitAll()
			.anyRequest().authenticated()
		.and().formLogin()
			  .loginPage(NlibProperty.getString("login.url"))
			  .permitAll()
		.and().csrf()
			  .disable();
		
		http.logout()
			.logoutSuccessUrl("/login/loginForm.do?logout")
			.invalidateHttpSession(true).deleteCookies("JSESSIONID")
			;
	}
	
	@Bean
	@Override
	public AuthenticationManager authenticationManagerBean() throws Exception {
		return super.authenticationManagerBean();
	}
	
	@Bean
	public UserDetailsService userDetailsService() {
		return new SecUserDetailsService();
	}
	
	@Bean
	public PasswordEncoder getPasswordEncoder() {
		return new NlibPasswordEncoder();
	}

}


