// Path: src/main/java/security/handler/AuthenticationFailureHandlerImpl.java
// public class AuthenticationFailureHandlerImpl extends
// 		SimpleUrlAuthenticationFailureHandler {
// 
// 	@Override
// 	public void onAuthenticationFailure(HttpServletRequest request,
// 			HttpServletResponse response, AuthenticationException exception)
// 			throws IOException, ServletException {
// 		// TODO Auto-generated method stub
// 		super.onAuthenticationFailure(request, response, exception);
// 	}
// 
// }
// 
// Path: src/main/java/security/handler/AuthenticationSuccessHandlerImpl.java
// public class AuthenticationSuccessHandlerImpl extends
// 		SimpleUrlAuthenticationSuccessHandler {
// 
// 	@Value("${auth.success.url}")
// 	private String defaultTargetUrl;
// 
// 	@Autowired
// 	private AuthTokenGeneratorService authTokenGeneratorService;
// 
// 	@Override
// 	public void onAuthenticationSuccess(HttpServletRequest request,
// 			HttpServletResponse response, Authentication authentication)
// 			throws IOException, ServletException {
// 
// 		final String authToken = authTokenGeneratorService
// 				.generateToken(authentication);
// 		response.addHeader(Constant.HEADER_SECURITY_TOKEN, authToken);
// 		request.getRequestDispatcher(defaultTargetUrl).forward(request,
// 				response);
// 
// 	}
// 
// }
// 
// Path: src/main/java/security/handler/LogoutSuccessHandlerImpl.java
// public class LogoutSuccessHandlerImpl extends
// 		AbstractAuthenticationTargetUrlRequestHandler implements
// 		LogoutSuccessHandler {
// 
// 	@Autowired
// 	private AuthTokenService authTokenService;
// 
// 	@Autowired
// 	private AuthTokenGeneratorService authTokenGeneratorService;
// 
// 	public void onLogoutSuccess(HttpServletRequest arg0,
// 			HttpServletResponse arg1, Authentication arg2) throws IOException,
// 			ServletException {
// 		deleteAuthenticationToken(arg0);
// 		super.handle(arg0, arg1, arg2);
// 
// 	}
// 
// 	private void deleteAuthenticationToken(HttpServletRequest request) {
// 		String token = request.getHeader(Constant.HEADER_SECURITY_TOKEN);
// 		if (null == token || token.trim().length() == 0) {
// 			return;
// 		}
// 
// 		String[] tokens = authTokenGeneratorService.decode(token);
// 		authTokenService.deleteByTokenAndSeries(tokens[0], tokens[1]);
// 
// 	}
// }


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
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.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import security.handler.AuthenticationFailureHandlerImpl;
import security.handler.AuthenticationSuccessHandlerImpl;
import security.handler.LogoutSuccessHandlerImpl;
package security.config;



/**
 * @author abidk
 * 
 */
@Configuration
@EnableWebMvcSecurity
@Order(1)
public class MvcSecurityConfig extends WebSecurityConfigurerAdapter {

	@Autowired
	private UserDetailsService userDetailsService;

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.csrf().disable().authorizeRequests()
				.antMatchers("/", "/index", "/login", "/js/**", "/css/**", "/html/**")
				.permitAll().anyRequest().authenticated().and().formLogin()
				.failureHandler(authenticationFailureHandler())
				.successHandler(authenticationSuccessHandler())
				.loginPage("/login").and().logout()
				.logoutSuccessHandler(logoutSuccessHandler()).permitAll();

	}

	@Override
	protected void configure(AuthenticationManagerBuilder auth)
			throws Exception {
		auth.userDetailsService(userDetailsService).passwordEncoder(
				bCryptPasswordEncoder());
	}

	@Bean
	public BCryptPasswordEncoder bCryptPasswordEncoder() {
		return new BCryptPasswordEncoder();
	}

	@Bean
	public AuthenticationSuccessHandler authenticationSuccessHandler() {
// Path: src/main/java/security/handler/AuthenticationFailureHandlerImpl.java
// public class AuthenticationFailureHandlerImpl extends
// 		SimpleUrlAuthenticationFailureHandler {
// 
// 	@Override
// 	public void onAuthenticationFailure(HttpServletRequest request,
// 			HttpServletResponse response, AuthenticationException exception)
// 			throws IOException, ServletException {
// 		// TODO Auto-generated method stub
// 		super.onAuthenticationFailure(request, response, exception);
// 	}
// 
// }
// 
// Path: src/main/java/security/handler/AuthenticationSuccessHandlerImpl.java
// public class AuthenticationSuccessHandlerImpl extends
// 		SimpleUrlAuthenticationSuccessHandler {
// 
// 	@Value("${auth.success.url}")
// 	private String defaultTargetUrl;
// 
// 	@Autowired
// 	private AuthTokenGeneratorService authTokenGeneratorService;
// 
// 	@Override
// 	public void onAuthenticationSuccess(HttpServletRequest request,
// 			HttpServletResponse response, Authentication authentication)
// 			throws IOException, ServletException {
// 
// 		final String authToken = authTokenGeneratorService
// 				.generateToken(authentication);
// 		response.addHeader(Constant.HEADER_SECURITY_TOKEN, authToken);
// 		request.getRequestDispatcher(defaultTargetUrl).forward(request,
// 				response);
// 
// 	}
// 
// }
// 
// Path: src/main/java/security/handler/LogoutSuccessHandlerImpl.java
// public class LogoutSuccessHandlerImpl extends
// 		AbstractAuthenticationTargetUrlRequestHandler implements
// 		LogoutSuccessHandler {
// 
// 	@Autowired
// 	private AuthTokenService authTokenService;
// 
// 	@Autowired
// 	private AuthTokenGeneratorService authTokenGeneratorService;
// 
// 	public void onLogoutSuccess(HttpServletRequest arg0,
// 			HttpServletResponse arg1, Authentication arg2) throws IOException,
// 			ServletException {
// 		deleteAuthenticationToken(arg0);
// 		super.handle(arg0, arg1, arg2);
// 
// 	}
// 
// 	private void deleteAuthenticationToken(HttpServletRequest request) {
// 		String token = request.getHeader(Constant.HEADER_SECURITY_TOKEN);
// 		if (null == token || token.trim().length() == 0) {
// 			return;
// 		}
// 
// 		String[] tokens = authTokenGeneratorService.decode(token);
// 		authTokenService.deleteByTokenAndSeries(tokens[0], tokens[1]);
// 
// 	}
// }


// Path: src/main/java/security/config/MvcSecurityConfig.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
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.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import security.handler.AuthenticationFailureHandlerImpl;
import security.handler.AuthenticationSuccessHandlerImpl;
import security.handler.LogoutSuccessHandlerImpl;

package security.config;



/**
 * @author abidk
 * 
 */
@Configuration
@EnableWebMvcSecurity
@Order(1)
public class MvcSecurityConfig extends WebSecurityConfigurerAdapter {

	@Autowired
	private UserDetailsService userDetailsService;

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.csrf().disable().authorizeRequests()
				.antMatchers("/", "/index", "/login", "/js/**", "/css/**", "/html/**")
				.permitAll().anyRequest().authenticated().and().formLogin()
				.failureHandler(authenticationFailureHandler())
				.successHandler(authenticationSuccessHandler())
				.loginPage("/login").and().logout()
				.logoutSuccessHandler(logoutSuccessHandler()).permitAll();

	}

	@Override
	protected void configure(AuthenticationManagerBuilder auth)
			throws Exception {
		auth.userDetailsService(userDetailsService).passwordEncoder(
				bCryptPasswordEncoder());
	}

	@Bean
	public BCryptPasswordEncoder bCryptPasswordEncoder() {
		return new BCryptPasswordEncoder();
	}

	@Bean
	public AuthenticationSuccessHandler authenticationSuccessHandler() {
		return new AuthenticationSuccessHandlerImpl();