package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
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.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.LinkedHashMap;
import java.util.Map;

@SpringBootApplication
@EnableRedisHttpSession
@EnableZuulProxy
@RestController
public class GatewayApplication {

    @RequestMapping("/user")
    public Map<String, Object> user(Principal user) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("name", user.getName());
        map.put("roles", AuthorityUtils.authorityListToSet(((Authentication) user).getAuthorities()));
        return map;
    }

    @Configuration
    @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
    protected static class SecurityConfiguration extends WebSecurityConfigurerAdapter {

        @Autowired
        public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication()
                .withUser("user").password("password").roles("USER")
            .and()
                .withUser("admin").password("admin").roles("USER", "ADMIN", "READER", "WRITER")
            .and()
                .withUser("audit").password("audit").roles("USER", "ADMIN", "READER");
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .httpBasic()
            .and()
                .logout()
            .and()
                .authorizeRequests()
                .antMatchers("/index.html", "/login", "/").permitAll()
                .antMatchers("/admin/**").hasRole("ADMIN")
                .anyRequest().authenticated();
        }
    }

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

@@ -895,202 +895,837 @@
 k.se
-ssion.data.redis.config.annotation.web.http.EnableRedisHttpSession;%0Aimport org.springframework.web.bind.annotation.RequestMapping;%0Aimport org.springframework.web.bind.annotation.RestController;%0A
+curity.web.csrf.CsrfToken;%0Aimport org.springframework.security.web.csrf.CsrfTokenRepository;%0Aimport org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;%0Aimport org.springframework.security.web.session.SessionManagementFilter;%0Aimport org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;%0Aimport org.springframework.web.bind.annotation.RequestMapping;%0Aimport org.springframework.web.bind.annotation.RestController;%0Aimport org.springframework.web.filter.OncePerRequestFilter;%0Aimport org.springframework.web.util.WebUtils;%0A%0Aimport javax.servlet.Filter;%0Aimport javax.servlet.FilterChain;%0Aimport javax.servlet.ServletException;%0Aimport javax.servlet.http.Cookie;%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0Aimport java.io.IOException;
 %0Aimp
@@ -3093,32 +3093,36 @@
                 
+    
 .antMatchers(%22/i
@@ -3180,50 +3180,859 @@
     
-.antMatchers(%22/admin/**%22).hasRole(%22ADMI
+    .anyRequest().authenticated()%0A            .and()%0A                .csrf().csrfTokenRepository(csrfTokenRepository())%0A            .and()%0A                .addFilterAfter(csrfHeaderFilter(), SessionManagementFilter.class);%0A        %7D%0A        %0A        private Filter csrfHeaderFilter() %7B%0A            return new OncePerRequestFilter() %7B%0A                @Override%0A                protected void doFilterInternal(HttpServletRequest request,%0A                                                HttpServletResponse response, FilterChain filterChain)%0A                        throws ServletException, IOException %7B%0A                    CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class%0A                            .getName());%0A                    if (csrf != null) %7B%0A                        Cookie cookie = WebUtils.getCookie(request, %22XSRF-TOKE
 N%22)
+;
 %0A   
@@ -4048,37 +4048,744 @@
     
-.anyRequest().authenticated()
+        String token = csrf.getToken();%0A                        if (cookie == null %7C%7C token != null%0A                                && !token.equals(cookie.getValue())) %7B%0A                            cookie = new Cookie(%22XSRF-TOKEN%22, token);%0A                            cookie.setPath(%22/%22);%0A                            response.addCookie(cookie);%0A                        %7D%0A                    %7D%0A                    filterChain.doFilter(request, response);%0A                %7D%0A            %7D;%0A        %7D%0A%0A        private CsrfTokenRepository csrfTokenRepository() %7B%0A            HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();%0A            repository.setHeaderName(%22X-XSRF-TOKEN%22);%0A            return repository
 ;%0A  

add csrf header filter