package com.libqa.config;


import com.libqa.application.handler.LoginHandler;
import com.libqa.config.security.CustomAuthenticationProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import javax.sql.DataSource;

/**
 * @Author : yion
 * @Date : 2015. 3. 29.
 * @Description :
 */
@Slf4j
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
@EnableWebSecurity
@EnableWebMvcSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomAuthenticationProvider customAuthenticationProvider;

    @Autowired
    DataSource dataSource;


    @Override
    public void configure(WebSecurity webSecurity) throws Exception {
        webSecurity
                .ignoring()
                .antMatchers("/", "/resource/**");
    }

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

        httpSecurity.csrf().disable();
        log.debug("## configure httpSecurity = {}", httpSecurity);

        httpSecurity
                .authorizeRequests()
                .antMatchers("/index", "/user/**", "/space", "/space/**", "/feed/**", "/qa", "/qa/**", "/qa/save", "/wiki/**", "/common/**").permitAll()
                .antMatchers("/admin/**").hasAuthority("ADMIN")
                .and()
                .formLogin()
                .loginPage("/loginPage").usernameParameter("userEmail").passwordParameter("userPass")
                .successHandler(loginSuccessHandler())
                .failureUrl("/login?error")
                .permitAll()
                .and()
                .rememberMe().tokenRepository(persistentTokenRepository()).tokenValiditySeconds(604800)
                .and()
                .logout().deleteCookies("remember-me").logoutRequestMatcher(new AntPathRequestMatcher("/logoutUser")).logoutSuccessUrl("/index")
                .and()
                .exceptionHandling().accessDeniedPage("/access?error")
                .and()
                .authenticationProvider(customAuthenticationProvider)
        ;

        //.logout().logoutRequestMatcher(new AntPathRequestMatcher("/user/logout"))

    }

    @Bean
    public AuthenticationSuccessHandler loginSuccessHandler() {
        log.debug("#### login Success handler #####");
        return new LoginHandler();
    }

    @Bean
    public PersistentTokenRepository persistentTokenRepository() {
        JdbcTokenRepositoryImpl tokenRepositoryImpl = new JdbcTokenRepositoryImpl();
        tokenRepositoryImpl.setDataSource(dataSource);
        return tokenRepositoryImpl;
    }

}

@@ -20,17 +20,16 @@
 onfig;%0A%0A
-%0A
 import c
@@ -805,110 +805,8 @@
 er;%0A
-import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;%0A
 impo
@@ -1364,30 +1364,8 @@
 ity%0A
-@EnableWebMvcSecurity%0A
 publ
@@ -1544,16 +1544,24 @@
 red%0A    
+private 
 DataSour
@@ -1576,17 +1576,16 @@
 ource;%0A%0A
-%0A
     @Ove
@@ -1683,53 +1683,19 @@
 rity
-%0A                .ignoring()%0A                
+.ignoring()
 .ant
@@ -2890,32 +2890,32 @@
          .and()%0A
+
                 
@@ -2967,25 +2967,16 @@
 rovider)
-%0A        
 ;%0A%0A     

Remove Deprecated Configuration