package mingzuozhibi.config;

import com.allanditzel.springframework.security.web.csrf.CsrfTokenResponseHeaderBindingFilter;
import mingzuozhibi.security.CustomAccessDeniedHandler;
import mingzuozhibi.security.CustomAuthenticationFilter;
import mingzuozhibi.security.CustomAuthenticationHandler;
import mingzuozhibi.security.CustomLogoutHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.HttpMethod;
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.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.csrf.CsrfFilter;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Locale;

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

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private CustomLogoutHandler customLogoutHandler;

    @Autowired
    private CustomAuthenticationHandler customAuthenticationHandler;

    @Autowired
    private CustomAccessDeniedHandler customAccessDeniedHandler;


    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                    .antMatchers("/api/admin/**").hasRole("ADMIN")
                    .antMatchers("/api/basic/**").hasRole("BASIC")
                    .antMatchers("/api/session").permitAll()
                    .antMatchers(HttpMethod.GET).permitAll()
                    .antMatchers("/api/**").hasRole("BASIC");

            http.formLogin();

            http.logout()
                    .logoutUrl("/api/session/logout")
                    .addLogoutHandler(customLogoutHandler)
                    .logoutSuccessHandler(customLogoutHandler);

            http.exceptionHandling()
                    .accessDeniedHandler(customAccessDeniedHandler)
                    .authenticationEntryPoint(customAccessDeniedHandler);

            http.csrf()
                    .ignoringAntMatchers("/api/session/**")
                    .ignoringAntMatchers("/actuator/**", "/loggers/**", "/jolokia/**");

            http.addFilterBefore(new AcceptHeaderLocaleFilter(), UsernamePasswordAuthenticationFilter.class);

            http.addFilterAt(customAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);

            http.addFilterAfter(new CsrfTokenResponseHeaderBindingFilter(), CsrfFilter.class);

            Logger logger = LoggerFactory.getLogger(SecurityConfig.class);
            logger.info("设置安全策略");
        }

        private CustomAuthenticationFilter customAuthenticationFilter() throws Exception {
            CustomAuthenticationFilter filter = new CustomAuthenticationFilter();
            filter.setAuthenticationSuccessHandler(customAuthenticationHandler);
            filter.setAuthenticationFailureHandler(customAuthenticationHandler);
            filter.setAuthenticationManager(authenticationManager());
            filter.setFilterProcessesUrl("/api/session/login");
            return filter;
        }

    private static class AcceptHeaderLocaleFilter implements Filter {
        private AcceptHeaderLocaleResolver localeResolver;

        public AcceptHeaderLocaleFilter() {
            localeResolver = new AcceptHeaderLocaleResolver();
            localeResolver.setDefaultLocale(Locale.US);
        }

        @Override
        public void init(FilterConfig filterConfig) {
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            Locale locale = localeResolver.resolveLocale((HttpServletRequest) request);
            LocaleContextHolder.setLocale(locale);

            chain.doFilter(request, response);
        }

        @Override
        public void destroy() {
        }
    }
}

@@ -2049,28 +2049,24 @@
 ce);%0A    %7D%0A%0A
-    
     protecte
@@ -2112,36 +2112,32 @@
 ows Exception %7B%0A
-    
         http.aut
@@ -2161,36 +2161,32 @@
                 
-    
 .antMatchers(%22/a
@@ -2216,20 +2216,16 @@
 ADMIN%22)%0A
-    
         
@@ -2287,36 +2287,32 @@
                 
-    
 .antMatchers(%22/a
@@ -2328,36 +2328,32 @@
 n%22).permitAll()%0A
-    
                 
@@ -2401,36 +2401,32 @@
                 
-    
 .antMatchers(%22/a
@@ -2448,28 +2448,24 @@
 (%22BASIC%22);%0A%0A
-    
         http
@@ -2483,28 +2483,24 @@
 );%0A%0A        
-    
 http.logout(
@@ -2501,20 +2501,16 @@
 ogout()%0A
-    
         
@@ -2567,20 +2567,16 @@
         
-    
 .addLogo
@@ -2598,36 +2598,32 @@
 mLogoutHandler)%0A
-    
                 
@@ -2671,28 +2671,24 @@
 );%0A%0A        
-    
 http.excepti
@@ -2700,20 +2700,16 @@
 dling()%0A
-    
         
@@ -2780,20 +2780,16 @@
         
-    
 .authent
@@ -2831,28 +2831,24 @@
 dHandler);%0A%0A
-    
         http
@@ -2863,36 +2863,32 @@
                 
-    
 .ignoringAntMatc
@@ -2911,20 +2911,16 @@
 on/**%22)%0A
-    
         
@@ -2996,36 +2996,32 @@
 /**%22);%0A%0A        
-    
 http.addFilterBe
@@ -3095,36 +3095,32 @@
 Filter.class);%0A%0A
-    
         http.add
@@ -3208,28 +3208,24 @@
 );%0A%0A        
-    
 http.addFilt
@@ -3292,28 +3292,24 @@
 er.class);%0A%0A
-    
         Logg
@@ -3375,20 +3375,16 @@
         
-    
 logger.i
@@ -3398,31 +3398,23 @@
 %E5%AE%89%E5%85%A8%E7%AD%96%E7%95%A5%22);%0A
-    
     %7D%0A%0A
-    
     priv
@@ -3484,36 +3484,32 @@
 ows Exception %7B%0A
-    
         CustomAu
@@ -3570,36 +3570,32 @@
 lter();%0A        
-    
 filter.setAuthen
@@ -3639,36 +3639,32 @@
 cationHandler);%0A
-    
         filter.s
@@ -3724,36 +3724,32 @@
 ndler);%0A        
-    
 filter.setAuthen
@@ -3790,20 +3790,16 @@
 ger());%0A
-    
         
@@ -3858,20 +3858,16 @@
         
-    
 return f
@@ -3873,20 +3873,16 @@
 filter;%0A
-    
     %7D%0A%0A 

Repair indentation