// Path: src/main/java/me/itzg/mccy/controllers/CsrfHeaderFilter.java
// public class CsrfHeaderFilter extends OncePerRequestFilter {
//     @Override
//     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
//                                     FilterChain filterChain) throws ServletException, IOException {
//         CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class
//                 .getName());
//         if (csrf != null) {
//             Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
//             String token = csrf.getToken();
//             if (cookie==null || token!=null && !token.equals(cookie.getValue())) {
//                 cookie = new Cookie("XSRF-TOKEN", token);
//                 cookie.setPath("/");
//                 response.addCookie(cookie);
//             }
//         }
//         filterChain.doFilter(request, response);
// 
//     }
// }
// 
// Path: src/main/java/me/itzg/mccy/types/MccyConstants.java
// public class MccyConstants {
// 
// 
//     public static final String MCCY_LABEL_PREFIX = MccySwarmApplication.class.getPackage().getName();
//     public static final String MCCY_LABEL = MCCY_LABEL_PREFIX;
//     public static final String MCCY_LABEL_MODPACK_URL = MCCY_LABEL_PREFIX+".modpack-url";
//     public static final String MCCY_LABEL_NAME = MCCY_LABEL_PREFIX+".name";
//     public static final String MCCY_LABEL_PUBLIC = MCCY_LABEL_PREFIX+".public";
//     public static final String MCCY_LABEL_OWNER = MCCY_LABEL_PREFIX+".owner";
// 
//     public static final int SERVER_CONTAINER_PORT_INT = 25565;
//     public static final String SERVER_CONTAINER_PORT = String.valueOf(SERVER_CONTAINER_PORT_INT)+ "/tcp";
//     public static final String IP_ADDR_ALL_IF = "0.0.0.0";
// 
//     public static final String X_XSRF_TOKEN = "X-XSRF-TOKEN";
// 
//     public static final String FILE_MOD_INFO = "mod.info";
//     public static final String FILE_MCMOD_INFO = "mcmod.info";
//     public static final String FILE_PLUGIN_META = "plugin.yml";
// 
//     public static final String TEMP_PREFIX = "temp-";
// 
//     public static final String CATEGORY_WORLDS = "worlds";
//     public static final String CATEGORY_MODS = "mods";
// 
//     public static final ComparableVersion[] FORGE_VERSIONS_SQUASHED = new ComparableVersion[]{
//             ComparableVersion.of("1.8"),
//             ComparableVersion.of("1.8.8")
//     };
//     public static final int FORGE_VERSIONS_SQUASHED_SIZE = 2;
// 
//     public static final String EXT_WORLDS = ".zip";
//     public static final String EXT_MODS = ".jar";
//     public static final String EXT_MOD_PACK = ".zip";
// 
//     public static final String ENV_ICON = "ICON";
//     public static final String ENV_VERSION = "VERSION";
//     public static final String ENV_FORGEVERSION = "FORGEVERSION";
//     public static final String ENV_WORLD = "WORLD";
//     public static final String ENV_MODPACK = "MODPACK";
//     public static final String ENV_TYPE = "TYPE";
//     public static final String ENV_DIFFICULTY = "DIFFICULTY";
//     public static final String ENV_WHITELIST = "WHITELIST";
//     public static final String ENV_OPS = "OPS";
//     public static final String ENV_SEED = "SEED";
//     public static final String ENV_MODE = "MODE";
//     public static final String ENV_MOTD = "MOTD";
//     public static final String ENV_PVP = "PVP";
//     public static final String ENV_LEVEL_TYPE = "LEVEL_TYPE";
//     public static final String ENV_GENERATOR_SETTINGS = "GENERATOR_SETTINGS";
//     public static final String ENV_LEVEL = "LEVEL";
// 
//     public static final String LINK_MCCY = "mccy";
//     public static final String SNAPSHOT_VER_PATTERN = "(?<nYear>\\d+)w(?<nWeek>\\d+)(?<sRel>[a-z]+)";
// 
//     public static final String PROFILE_BASIC_AUTH = "basicAuth";
//     public static final String PROFILE_DEV = "dev";
//     public static final String MF_ATTR_FML_CORE_PLUGIN = "FMLCorePlugin";
// }


import me.itzg.mccy.controllers.CsrfHeaderFilter;
import me.itzg.mccy.types.MccyConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpMethod;
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.web.csrf.CsrfFilter;
import org.springframework.security.web.csrf.CsrfTokenRepository;
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;
import org.springframework.security.web.util.matcher.RequestMatcher;
import java.util.List;
package me.itzg.mccy.config;



/**
 * @author Geoff Bourne
 * @since 12/23/2015
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private SecurityProperties securityProperties;

    @Autowired
    private MccySecuritySettings mccySecuritySettings;

    @Autowired
    private Environment env;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        for (String p : mccySecuritySettings.getAllowAnonymous().getGet()) {
            http
                .authorizeRequests()
                .antMatchers(HttpMethod.GET, p).permitAll();
        }

        http
                .authorizeRequests()
                    .antMatchers("/**").hasRole("USER")
                .and().formLogin()
                    .loginPage("/login")
                    .defaultSuccessUrl("/")
                    .permitAll()
                .and().logout().logoutSuccessUrl("/")
// Path: src/main/java/me/itzg/mccy/controllers/CsrfHeaderFilter.java
// public class CsrfHeaderFilter extends OncePerRequestFilter {
//     @Override
//     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
//                                     FilterChain filterChain) throws ServletException, IOException {
//         CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class
//                 .getName());
//         if (csrf != null) {
//             Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
//             String token = csrf.getToken();
//             if (cookie==null || token!=null && !token.equals(cookie.getValue())) {
//                 cookie = new Cookie("XSRF-TOKEN", token);
//                 cookie.setPath("/");
//                 response.addCookie(cookie);
//             }
//         }
//         filterChain.doFilter(request, response);
// 
//     }
// }
// 
// Path: src/main/java/me/itzg/mccy/types/MccyConstants.java
// public class MccyConstants {
// 
// 
//     public static final String MCCY_LABEL_PREFIX = MccySwarmApplication.class.getPackage().getName();
//     public static final String MCCY_LABEL = MCCY_LABEL_PREFIX;
//     public static final String MCCY_LABEL_MODPACK_URL = MCCY_LABEL_PREFIX+".modpack-url";
//     public static final String MCCY_LABEL_NAME = MCCY_LABEL_PREFIX+".name";
//     public static final String MCCY_LABEL_PUBLIC = MCCY_LABEL_PREFIX+".public";
//     public static final String MCCY_LABEL_OWNER = MCCY_LABEL_PREFIX+".owner";
// 
//     public static final int SERVER_CONTAINER_PORT_INT = 25565;
//     public static final String SERVER_CONTAINER_PORT = String.valueOf(SERVER_CONTAINER_PORT_INT)+ "/tcp";
//     public static final String IP_ADDR_ALL_IF = "0.0.0.0";
// 
//     public static final String X_XSRF_TOKEN = "X-XSRF-TOKEN";
// 
//     public static final String FILE_MOD_INFO = "mod.info";
//     public static final String FILE_MCMOD_INFO = "mcmod.info";
//     public static final String FILE_PLUGIN_META = "plugin.yml";
// 
//     public static final String TEMP_PREFIX = "temp-";
// 
//     public static final String CATEGORY_WORLDS = "worlds";
//     public static final String CATEGORY_MODS = "mods";
// 
//     public static final ComparableVersion[] FORGE_VERSIONS_SQUASHED = new ComparableVersion[]{
//             ComparableVersion.of("1.8"),
//             ComparableVersion.of("1.8.8")
//     };
//     public static final int FORGE_VERSIONS_SQUASHED_SIZE = 2;
// 
//     public static final String EXT_WORLDS = ".zip";
//     public static final String EXT_MODS = ".jar";
//     public static final String EXT_MOD_PACK = ".zip";
// 
//     public static final String ENV_ICON = "ICON";
//     public static final String ENV_VERSION = "VERSION";
//     public static final String ENV_FORGEVERSION = "FORGEVERSION";
//     public static final String ENV_WORLD = "WORLD";
//     public static final String ENV_MODPACK = "MODPACK";
//     public static final String ENV_TYPE = "TYPE";
//     public static final String ENV_DIFFICULTY = "DIFFICULTY";
//     public static final String ENV_WHITELIST = "WHITELIST";
//     public static final String ENV_OPS = "OPS";
//     public static final String ENV_SEED = "SEED";
//     public static final String ENV_MODE = "MODE";
//     public static final String ENV_MOTD = "MOTD";
//     public static final String ENV_PVP = "PVP";
//     public static final String ENV_LEVEL_TYPE = "LEVEL_TYPE";
//     public static final String ENV_GENERATOR_SETTINGS = "GENERATOR_SETTINGS";
//     public static final String ENV_LEVEL = "LEVEL";
// 
//     public static final String LINK_MCCY = "mccy";
//     public static final String SNAPSHOT_VER_PATTERN = "(?<nYear>\\d+)w(?<nWeek>\\d+)(?<sRel>[a-z]+)";
// 
//     public static final String PROFILE_BASIC_AUTH = "basicAuth";
//     public static final String PROFILE_DEV = "dev";
//     public static final String MF_ATTR_FML_CORE_PLUGIN = "FMLCorePlugin";
// }


// Path: src/main/java/me/itzg/mccy/config/SecurityConfig.java
import me.itzg.mccy.controllers.CsrfHeaderFilter;
import me.itzg.mccy.types.MccyConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpMethod;
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.web.csrf.CsrfFilter;
import org.springframework.security.web.csrf.CsrfTokenRepository;
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;
import org.springframework.security.web.util.matcher.RequestMatcher;
import java.util.List;

package me.itzg.mccy.config;



/**
 * @author Geoff Bourne
 * @since 12/23/2015
 */
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private SecurityProperties securityProperties;

    @Autowired
    private MccySecuritySettings mccySecuritySettings;

    @Autowired
    private Environment env;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        for (String p : mccySecuritySettings.getAllowAnonymous().getGet()) {
            http
                .authorizeRequests()
                .antMatchers(HttpMethod.GET, p).permitAll();
        }

        http
                .authorizeRequests()
                    .antMatchers("/**").hasRole("USER")
                .and().formLogin()
                    .loginPage("/login")
                    .defaultSuccessUrl("/")
                    .permitAll()
                .and().logout().logoutSuccessUrl("/")
                .and().addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class)