// Path: src/main/java/com/poseidon/logout/CustomLogoutHandler.java
// public class CustomLogoutHandler implements LogoutHandler {
// 
// 	private Logger logger = Logger.getLogger(CustomLogoutHandler.class);
// 	
// 	@Override
// 	public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
// 		authentication= null;
// 		try {
// 			response.sendRedirect("/loginPage");
// 		} catch (IOException e) {
// 			logger.info("Erro ao tentar redirecionar para a pagina de login",e);
// 		}
// 	}
// }
// 
// Path: src/main/java/com/poseidon/logout/LogoutRequestMatcher.java
// public class LogoutRequestMatcher implements RequestMatcher {
// 
// 	@Override
// 	public boolean matches(HttpServletRequest request) {
// 		String requestURI = request.getRequestURI();
// 		if(requestURI.equals("/logout")){
// 			return true;
// 		}
// 		return false;
// 	}
// }


import com.poseidon.logout.CustomLogoutHandler;
import com.poseidon.logout.LogoutRequestMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.header.writers.*;
import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter;
import javax.sql.DataSource;
package com.poseidon;



@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
	@Autowired
	private DataSource dataSource;

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.authorizeRequests().antMatchers("/createSimpleUser","/createUser").permitAll();
		http.authorizeRequests().antMatchers("/user/*").hasAnyRole("USER","ADMIN");
		http.authorizeRequests().antMatchers("/admin/*").hasRole("ADMIN");
		http.authorizeRequests()
			.antMatchers("/css/**","/jquery/**","/bootstrap/**","/jquery/images/**","/webjars/**").permitAll();
		http.formLogin().loginPage("/loginPage").defaultSuccessUrl("/user/homePage").failureUrl("/login-Error")
		.permitAll().and().logout()
			.addLogoutHandler(new CustomLogoutHandler())
			.logoutRequestMatcher(new LogoutRequestMatcher()).invalidateHttpSession(true);

		http.headers()
				.contentTypeOptions()
				.and().xssProtection()
				.and().cacheControl()
				.and().httpStrictTransportSecurity()
				.and().frameOptions()
				.and().addHeaderWriter(new StaticHeadersWriter(
					"X-Content-Security-Policy","script-src 'self'"))
				.addHeaderWriter(new XXssProtectionHeaderWriter())
				.addHeaderWriter(new XFrameOptionsHeaderWriter(
					XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN))
				.addHeaderWriter(new XContentTypeOptionsHeaderWriter())
// Path: src/main/java/com/poseidon/logout/CustomLogoutHandler.java
// public class CustomLogoutHandler implements LogoutHandler {
// 
// 	private Logger logger = Logger.getLogger(CustomLogoutHandler.class);
// 	
// 	@Override
// 	public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
// 		authentication= null;
// 		try {
// 			response.sendRedirect("/loginPage");
// 		} catch (IOException e) {
// 			logger.info("Erro ao tentar redirecionar para a pagina de login",e);
// 		}
// 	}
// }
// 
// Path: src/main/java/com/poseidon/logout/LogoutRequestMatcher.java
// public class LogoutRequestMatcher implements RequestMatcher {
// 
// 	@Override
// 	public boolean matches(HttpServletRequest request) {
// 		String requestURI = request.getRequestURI();
// 		if(requestURI.equals("/logout")){
// 			return true;
// 		}
// 		return false;
// 	}
// }


// Path: src/main/java/com/poseidon/WebSecurityConfig.java
import com.poseidon.logout.CustomLogoutHandler;
import com.poseidon.logout.LogoutRequestMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.header.writers.*;
import org.springframework.security.web.header.writers.frameoptions.XFrameOptionsHeaderWriter;
import javax.sql.DataSource;

package com.poseidon;



@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
	@Autowired
	private DataSource dataSource;

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.authorizeRequests().antMatchers("/createSimpleUser","/createUser").permitAll();
		http.authorizeRequests().antMatchers("/user/*").hasAnyRole("USER","ADMIN");
		http.authorizeRequests().antMatchers("/admin/*").hasRole("ADMIN");
		http.authorizeRequests()
			.antMatchers("/css/**","/jquery/**","/bootstrap/**","/jquery/images/**","/webjars/**").permitAll();
		http.formLogin().loginPage("/loginPage").defaultSuccessUrl("/user/homePage").failureUrl("/login-Error")
		.permitAll().and().logout()
			.addLogoutHandler(new CustomLogoutHandler())
			.logoutRequestMatcher(new LogoutRequestMatcher()).invalidateHttpSession(true);

		http.headers()
				.contentTypeOptions()
				.and().xssProtection()
				.and().cacheControl()
				.and().httpStrictTransportSecurity()
				.and().frameOptions()
				.and().addHeaderWriter(new StaticHeadersWriter(
					"X-Content-Security-Policy","script-src 'self'"))
				.addHeaderWriter(new XXssProtectionHeaderWriter())
				.addHeaderWriter(new XFrameOptionsHeaderWriter(
					XFrameOptionsHeaderWriter.XFrameOptionsMode.SAMEORIGIN))
				.addHeaderWriter(new XContentTypeOptionsHeaderWriter())
				.addHeaderWriter(new CacheControlHeadersWriter())