package hello.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;

@Configuration
@EnableWebSecurity
public class Security extends WebSecurityConfigurerAdapter {

	public static final String ADMIN_ROLE = "ADMINISTRATOR";

	// List the mappings/methods for which no authorisation is required.
	// We specifically omit '/greeting/new' here so that we require log in
	// before submitting the new greeting.
	private static final RequestMatcher[] NO_AUTH = { new AntPathRequestMatcher("/", "GET"),
			new AntPathRequestMatcher("/api/**", "GET"), new AntPathRequestMatcher("/greeting", "GET"),
			new AntPathRequestMatcher("/greeting/{id:[\\d]+}", "GET"), new AntPathRequestMatcher("/**", "DELETE") };

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		// By default, all requests are authenticated except our specific list.
		http.authorizeRequests().requestMatchers(NO_AUTH).permitAll().anyRequest().hasRole(ADMIN_ROLE);

		// Use form login/logout for the Web.
		http.formLogin().loginPage("/sign-in").permitAll();
		http.logout().logoutUrl("/sign-out").logoutSuccessUrl("/").permitAll();

		// Use HTTP basic for the API.
		http.requestMatcher(new AntPathRequestMatcher("/api/**")).httpBasic();

		// Only use CSRF for Web requests.
		http.antMatcher("/**").csrf().ignoringAntMatchers("/api/**");
	}

	@Override
	@Bean
	public UserDetailsService userDetailsService() {
		PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();

		UserDetails rob = User.withUsername("Rob").password(encoder.encode("Haines")).roles(ADMIN_ROLE).build();
		UserDetails caroline = User.withUsername("Caroline").password(encoder.encode("Jay")).roles(ADMIN_ROLE).build();
		UserDetails markel = User.withUsername("Markel").password(encoder.encode("Vigo")).roles(ADMIN_ROLE).build();

		return new InMemoryUserDetailsManager(rob, caroline, markel);
	}
}

@@ -16,16 +16,92 @@
 onfig;%0A%0A
+import org.springframework.boot.autoconfigure.security.servlet.PathRequest;%0A
 import o
@@ -1275,18 +1275,107 @@
 ed.%0A%09// 
-We
+This includes the paths where static resources, such as bootstrap, are%0A%09// located. We also
  specifi
@@ -1428,19 +1428,19 @@
 uire
+%0A%09//
  log in
-%0A%09//
  bef
@@ -1523,16 +1523,72 @@
 UTH = %7B 
+PathRequest.toStaticResources().atCommonLocations(),%0A%09%09%09
 new AntP
@@ -1609,36 +1609,33 @@
 her(%22/%22, %22GET%22),
-%0A%09%09%09
+ 
 new AntPathReque
@@ -1654,33 +1654,36 @@
 api/**%22, %22GET%22),
- 
+%0A%09%09%09
 new AntPathReque
@@ -1704,36 +1704,33 @@
 eeting%22, %22GET%22),
-%0A%09%09%09
+ 
 new AntPathReque
@@ -1763,33 +1763,36 @@
 %5C%5Cd%5D+%7D%22, %22GET%22),
- 
+%0A%09%09%09
 new AntPathReque

Fix the serving of static assets when not logged in.