package com.elia.em.config;

//import com.elia.em.security.MyCorsFilter;

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.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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf()
            .ignoringAntMatchers("/register")
            .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
            .and()
            .authorizeRequests()
            .antMatchers("/api/**").authenticated()
            .antMatchers("/**","/index.html").permitAll()
            .anyRequest().authenticated()
            .and()
            .httpBasic()
            .and()
            .formLogin()
            .loginPage("/em/login")
           .permitAll();

    }

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

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth, UserDetailsService userDetailsService) throws Exception {
        auth
            .userDetailsService(userDetailsService)
            .passwordEncoder(new BCryptPasswordEncoder());
    }

}
@@ -26,10 +26,9 @@
 g;%0A%0A
-//
+%0A
 impo
@@ -65,17 +65,16 @@
 Filter;%0A
-%0A
 import o
@@ -868,24 +868,104 @@
 ordEncoder;%0A
+import org.springframework.security.web.access.channel.ChannelProcessingFilter;%0A
 import org.s
@@ -1024,16 +1024,16 @@
 sitory;%0A
-
 %0A@Config
@@ -1181,16 +1181,69 @@
 pter %7B%0A%0A
+    @Autowired%0A    private MyCorsFilter corsFilter;%0A%0A
     @Ove
@@ -1307,32 +1307,32 @@
 ows Exception %7B%0A
-
         http%0A   
@@ -1328,16 +1328,84 @@
    http%0A
+        .addFilterBefore(corsFilter, ChannelProcessingFilter.class)%0A
         

add cors filter