package nlib.security;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.security.access.annotation.Secured;
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 org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

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

/**
 * <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/**")
			.antMatchers("/html/**")
		;
	}
	
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.authorizeRequests()
			.antMatchers("/**").permitAll()
		.and().requestCache().requestCache(new CustomRequestCache())
		.and().formLogin()
			  .loginPage(NlibProperty.getString("login.url") + "?login=req")
			  .permitAll()
		.and().csrf()
			  .disable();
		http.logout()
			.logoutSuccessUrl("/login/logout.do")
			.invalidateHttpSession(true).deleteCookies("JSESSIONID")
			;
		
		//--------------------------------------
		// 안내 
		//--------------------------------------
		// 로그인이 필요한 컨에 대하여 컨트롤러의 해당 함수에 
		// @Secured(SecUserVO.ROLE_USER)
		// 선언할 것
		// 
		// (ex)
		//
	    //    @Secured("ROLE_USER")
		//    @RequestMapping(value="/userInfo/getMyHome.do")
	    //    public String getMyHome(HttpServletRequest request, ModelMap model) throws Exception { ... } 
		//--------------------------------------
		
	}
	
	@Bean
	@Override
	public AuthenticationManager authenticationManagerBean() throws Exception {
		return super.authenticationManagerBean();
	}
	
	@Bean
	public UserDetailsService userDetailsService() {
		return new SecUserDetailsService();
	}
	
	@Bean
	public PasswordEncoder getPasswordEncoder() {
		return new NlibPasswordEncoder();
	}

}


