// Path: gatling-rest/src/main/java/com/alh/gatling/security/rest/RestAuthenticationAccessDeniedHandler.java
// public class RestAuthenticationAccessDeniedHandler implements AccessDeniedHandler {
// 
//     @Override
//     public void handle(HttpServletRequest request, HttpServletResponse response,
//                        AccessDeniedException ex) throws IOException, ServletException {
//         response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
//     }
// }
// 
// Path: gatling-rest/src/main/java/com/alh/gatling/security/rest/RestAuthenticationEntryPoint.java
// public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
// 
//     @Override
//     public void commence(HttpServletRequest req, HttpServletResponse res,
//                          AuthenticationException ex) throws IOException, ServletException {
//         res.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
//     }
// 
// }
// 
// Path: gatling-rest/src/main/java/com/alh/gatling/service/SimpleCORSFilter.java
// @Component
// public class SimpleCORSFilter implements Filter {
// 
//     private final Logger log = LoggerFactory.getLogger(SimpleCORSFilter.class);
// 
//     public SimpleCORSFilter() {
//         log.info("SimpleCORSFilter init");
//     }
// 
//     @Override
//     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
// 
//         HttpServletRequest request = (HttpServletRequest) req;
//         HttpServletResponse response = (HttpServletResponse) res;
// 
//         // replace with "*" for testing, and request.getHeader("Origin") for production
//         response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
//         response.setHeader("Access-Control-Allow-Credentials", "true");
//         response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
//         response.setHeader("Access-Control-Max-Age", "3600");
//         response.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, Accept, X-Requested-With, remember-me, Cache-Control");
// 
//     	// Just REPLY OK if request method is OPTIONS for CORS (pre-flight)
// 		if ( request.getMethod().equals("OPTIONS") ) {
// 	        response.setStatus(HttpServletResponse.SC_OK);
// 	        return;
// 	    }
// 		
// 		chain.doFilter(req, res);
//     }
// 
//     @Override
//     public void init(FilterConfig filterConfig) {
//     }
// 
//     @Override
//     public void destroy() {
//     }
// 
// }


import org.springframework.beans.factory.annotation.Value;
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.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.http.SessionCreationPolicy;
import org.springframework.security.web.access.channel.ChannelProcessingFilter;
import com.alh.gatling.security.rest.RestAuthenticationAccessDeniedHandler;
import com.alh.gatling.security.rest.RestAuthenticationEntryPoint;
import com.alh.gatling.service.SimpleCORSFilter;
package com.alh.gatling.config;



@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Value("${security.username}") private String USERNAME;
    @Value("${security.password}") private String PASSWORD;
	
    @Override
    public void configure(WebSecurity web) throws Exception {
        // Filters will not get executed for the resources
        web.ignoring().antMatchers("/", "/resources/**", "/static/**", "/public/**", "/webui/**", "/h2-console/**"
            , "/gatling/lib/**", "/configuration/**", "/swagger-ui/**", "/swagger-resources/**", "/api-docs", "/api-docs/**", "/v2/api-docs/**"
            , "/*.html", "/**/*.html" ,"/**/*.css","/**/*.js","/**/*.png","/**/*.jpg", "/**/*.gif", "/**/*.svg", "/**/*.ico", "/**/*.ttf","/**/*.woff","/**/*.otf");
    }

    @Override public void configure(AuthenticationManagerBuilder auth) throws Exception {
    	auth.inMemoryAuthentication()
    	.withUser(USERNAME)
    	.password(PASSWORD)
    	.roles("USER", "ACTUATOR");
    }

    @Override protected void configure(HttpSecurity http) throws Exception {
        http
                .antMatcher("/**").csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
// Path: gatling-rest/src/main/java/com/alh/gatling/security/rest/RestAuthenticationAccessDeniedHandler.java
// public class RestAuthenticationAccessDeniedHandler implements AccessDeniedHandler {
// 
//     @Override
//     public void handle(HttpServletRequest request, HttpServletResponse response,
//                        AccessDeniedException ex) throws IOException, ServletException {
//         response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
//     }
// }
// 
// Path: gatling-rest/src/main/java/com/alh/gatling/security/rest/RestAuthenticationEntryPoint.java
// public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
// 
//     @Override
//     public void commence(HttpServletRequest req, HttpServletResponse res,
//                          AuthenticationException ex) throws IOException, ServletException {
//         res.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
//     }
// 
// }
// 
// Path: gatling-rest/src/main/java/com/alh/gatling/service/SimpleCORSFilter.java
// @Component
// public class SimpleCORSFilter implements Filter {
// 
//     private final Logger log = LoggerFactory.getLogger(SimpleCORSFilter.class);
// 
//     public SimpleCORSFilter() {
//         log.info("SimpleCORSFilter init");
//     }
// 
//     @Override
//     public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
// 
//         HttpServletRequest request = (HttpServletRequest) req;
//         HttpServletResponse response = (HttpServletResponse) res;
// 
//         // replace with "*" for testing, and request.getHeader("Origin") for production
//         response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
//         response.setHeader("Access-Control-Allow-Credentials", "true");
//         response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
//         response.setHeader("Access-Control-Max-Age", "3600");
//         response.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, Accept, X-Requested-With, remember-me, Cache-Control");
// 
//     	// Just REPLY OK if request method is OPTIONS for CORS (pre-flight)
// 		if ( request.getMethod().equals("OPTIONS") ) {
// 	        response.setStatus(HttpServletResponse.SC_OK);
// 	        return;
// 	    }
// 		
// 		chain.doFilter(req, res);
//     }
// 
//     @Override
//     public void init(FilterConfig filterConfig) {
//     }
// 
//     @Override
//     public void destroy() {
//     }
// 
// }


// Path: gatling-rest/src/main/java/com/alh/gatling/config/WebSecurityConfig.java
import org.springframework.beans.factory.annotation.Value;
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.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.http.SessionCreationPolicy;
import org.springframework.security.web.access.channel.ChannelProcessingFilter;
import com.alh.gatling.security.rest.RestAuthenticationAccessDeniedHandler;
import com.alh.gatling.security.rest.RestAuthenticationEntryPoint;
import com.alh.gatling.service.SimpleCORSFilter;

package com.alh.gatling.config;



@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Value("${security.username}") private String USERNAME;
    @Value("${security.password}") private String PASSWORD;
	
    @Override
    public void configure(WebSecurity web) throws Exception {
        // Filters will not get executed for the resources
        web.ignoring().antMatchers("/", "/resources/**", "/static/**", "/public/**", "/webui/**", "/h2-console/**"
            , "/gatling/lib/**", "/configuration/**", "/swagger-ui/**", "/swagger-resources/**", "/api-docs", "/api-docs/**", "/v2/api-docs/**"
            , "/*.html", "/**/*.html" ,"/**/*.css","/**/*.js","/**/*.png","/**/*.jpg", "/**/*.gif", "/**/*.svg", "/**/*.ico", "/**/*.ttf","/**/*.woff","/**/*.otf");
    }

    @Override public void configure(AuthenticationManagerBuilder auth) throws Exception {
    	auth.inMemoryAuthentication()
    	.withUser(USERNAME)
    	.password(PASSWORD)
    	.roles("USER", "ACTUATOR");
    }

    @Override protected void configure(HttpSecurity http) throws Exception {
        http
                .antMatcher("/**").csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()