// Path: src/main/java/com/scheible/risingempire/web/security/ConnectedPlayer.java
// public final class ConnectedPlayer implements UserDetails {
// 	
// 	public final static PasswordEncoder PASSWORD_ENCODER = NoOpPasswordEncoder.getInstance();
// 	public final static String DEFAULT_PASSWORD = "yyy";
// 	
// 	private final String username;
// 
// 	/* package */ ConnectedPlayer(String username) {
// 		this.username = username;
// 	}
// 
// 	@Override
// 	public Collection<? extends GrantedAuthority> getAuthorities() {
// 		return AuthorityUtils.createAuthorityList("ROLE_USER");
// 	}
// 
// 	@Override
// 	public String getUsername() {
// 		return username;
// 	}
// 
// 	@Override
// 	public boolean isAccountNonExpired() {
// 		return true;
// 	}
// 
// 	@Override
// 	public boolean isAccountNonLocked() {
// 		return true;
// 	}
// 
// 	@Override
// 	public boolean isCredentialsNonExpired() {
// 		return true;
// 	}
// 
// 	@Override
// 	public boolean isEnabled() {
// 		return true;
// 	}
// 
// 	@Override
// 	public String getPassword() {
// 		return DEFAULT_PASSWORD;
// 	}	
// }


import com.scheible.risingempire.web.security.ConnectedPlayer;
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.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.csrf.CsrfFilter;
import org.springframework.security.web.servlet.support.csrf.CsrfRequestDataValueProcessor;
import org.springframework.security.web.util.matcher.AndRequestMatcher;
import org.springframework.security.web.util.matcher.RegexRequestMatcher;
import org.springframework.web.servlet.support.RequestDataValueProcessor;
package com.scheible.risingempire.web.config.web;


/**
 *
 * @author sj
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests().antMatchers("/css/**", "/js/**", "/typescript/**", "/images/**", "/**/favicon.ico").permitAll().and()
                .authorizeRequests().regexMatchers("/players").permitAll().and()
                .authorizeRequests().regexMatchers("/addAi").permitAll().and()
                .authorizeRequests().regexMatchers("/csrf").permitAll().and()
                .authorizeRequests().regexMatchers("/shutdown").permitAll().and() // NOTE Not production ready! ;-)
                .authorizeRequests().antMatchers("/h2-console/**").permitAll().and() // NOTE Not production ready as well! ;-)
                .authorizeRequests().anyRequest().fullyAuthenticated().and()
                .formLogin().loginProcessingUrl("/login").loginPage("/join.html").failureUrl("/join.html?error").defaultSuccessUrl("/game.html", true).permitAll().and()
                .logout().permitAll();

        http
                .csrf()
                .requireCsrfProtectionMatcher(new AndRequestMatcher(CsrfFilter.DEFAULT_CSRF_MATCHER,
                        new RegexRequestMatcher("^(?!/h2-console/)", null)));
        
        
        http.headers().frameOptions().sameOrigin(); // // NOTE Also needed for h2-console.
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth, UserDetailsService userDetailsService) throws Exception {
// Path: src/main/java/com/scheible/risingempire/web/security/ConnectedPlayer.java
// public final class ConnectedPlayer implements UserDetails {
// 	
// 	public final static PasswordEncoder PASSWORD_ENCODER = NoOpPasswordEncoder.getInstance();
// 	public final static String DEFAULT_PASSWORD = "yyy";
// 	
// 	private final String username;
// 
// 	/* package */ ConnectedPlayer(String username) {
// 		this.username = username;
// 	}
// 
// 	@Override
// 	public Collection<? extends GrantedAuthority> getAuthorities() {
// 		return AuthorityUtils.createAuthorityList("ROLE_USER");
// 	}
// 
// 	@Override
// 	public String getUsername() {
// 		return username;
// 	}
// 
// 	@Override
// 	public boolean isAccountNonExpired() {
// 		return true;
// 	}
// 
// 	@Override
// 	public boolean isAccountNonLocked() {
// 		return true;
// 	}
// 
// 	@Override
// 	public boolean isCredentialsNonExpired() {
// 		return true;
// 	}
// 
// 	@Override
// 	public boolean isEnabled() {
// 		return true;
// 	}
// 
// 	@Override
// 	public String getPassword() {
// 		return DEFAULT_PASSWORD;
// 	}	
// }


// Path: src/main/java/com/scheible/risingempire/web/config/web/WebSecurityConfig.java
import com.scheible.risingempire.web.security.ConnectedPlayer;
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.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.csrf.CsrfFilter;
import org.springframework.security.web.servlet.support.csrf.CsrfRequestDataValueProcessor;
import org.springframework.security.web.util.matcher.AndRequestMatcher;
import org.springframework.security.web.util.matcher.RegexRequestMatcher;
import org.springframework.web.servlet.support.RequestDataValueProcessor;

package com.scheible.risingempire.web.config.web;


/**
 *
 * @author sj
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests().antMatchers("/css/**", "/js/**", "/typescript/**", "/images/**", "/**/favicon.ico").permitAll().and()
                .authorizeRequests().regexMatchers("/players").permitAll().and()
                .authorizeRequests().regexMatchers("/addAi").permitAll().and()
                .authorizeRequests().regexMatchers("/csrf").permitAll().and()
                .authorizeRequests().regexMatchers("/shutdown").permitAll().and() // NOTE Not production ready! ;-)
                .authorizeRequests().antMatchers("/h2-console/**").permitAll().and() // NOTE Not production ready as well! ;-)
                .authorizeRequests().anyRequest().fullyAuthenticated().and()
                .formLogin().loginProcessingUrl("/login").loginPage("/join.html").failureUrl("/join.html?error").defaultSuccessUrl("/game.html", true).permitAll().and()
                .logout().permitAll();

        http
                .csrf()
                .requireCsrfProtectionMatcher(new AndRequestMatcher(CsrfFilter.DEFAULT_CSRF_MATCHER,
                        new RegexRequestMatcher("^(?!/h2-console/)", null)));
        
        
        http.headers().frameOptions().sameOrigin(); // // NOTE Also needed for h2-console.
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth, UserDetailsService userDetailsService) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(ConnectedPlayer.PASSWORD_ENCODER);