package hello.config;

import org.springframework.beans.factory.annotation.Autowired;
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.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/**");
	}

	@Autowired
	public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
		auth.inMemoryAuthentication().withUser("Rob").password("Haines").roles(ADMIN_ROLE);
		auth.inMemoryAuthentication().withUser("Caroline").password("Jay").roles(ADMIN_ROLE);
		auth.inMemoryAuthentication().withUser("Markel").password("Vigo").roles(ADMIN_ROLE);
	}
}

@@ -47,21 +47,15 @@
 ork.
-beans.factory
+context
 .ann
@@ -62,25 +62,20 @@
 otation.
-Autowired
+Bean
 ;%0Aimport
@@ -133,116 +133,8 @@
 on;%0A
-import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;%0A
 impo
@@ -293,32 +293,32 @@
 bleWebSecurity;%0A
-
 import org.sprin
@@ -399,24 +399,445 @@
 rerAdapter;%0A
+import org.springframework.security.core.userdetails.User;%0Aimport org.springframework.security.core.userdetails.UserDetails;%0Aimport org.springframework.security.core.userdetails.UserDetailsService;%0Aimport org.springframework.security.crypto.factory.PasswordEncoderFactories;%0Aimport org.springframework.security.crypto.password.PasswordEncoder;%0Aimport org.springframework.security.provisioning.InMemoryUserDetailsManager;%0A
 import org.s
@@ -2236,132 +2236,187 @@
 %0A%0A%09@
-Autowired%0A%09public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception %7B%0A%09%09auth.inMemoryAuthentication()
+Override%0A%09@Bean%0A%09public UserDetailsService userDetailsService() %7B%0A%09%09PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();%0A%0A%09%09UserDetails rob = User
 .wit
@@ -2420,16 +2420,20 @@
 withUser
+name
 (%22Rob%22).
@@ -2441,16 +2441,31 @@
 assword(
+encoder.encode(
 %22Haines%22
@@ -2465,16 +2465,17 @@
 Haines%22)
+)
 .roles(A
@@ -2488,41 +2488,47 @@
 OLE)
-;%0A%09%09auth.inMemoryAuthentication()
+.build();%0A%09%09UserDetails caroline = User
 .wit
@@ -2532,16 +2532,20 @@
 withUser
+name
 (%22Caroli
@@ -2562,14 +2562,30 @@
 ord(
+encoder.encode(
 %22Jay%22)
+)
 .rol
@@ -2602,41 +2602,45 @@
 OLE)
-;%0A%09%09auth.inMemoryAuthentication()
+.build();%0A%09%09UserDetails markel = User
 .wit
@@ -2644,16 +2644,20 @@
 withUser
+name
 (%22Markel
@@ -2672,15 +2672,31 @@
 ord(
+encoder.encode(
 %22Vigo%22)
+)
 .rol
@@ -2709,15 +2709,88 @@
 IN_ROLE)
+.build();%0A%0A%09%09return new InMemoryUserDetailsManager(rob, caroline, markel)
 ;%0A%09%7D%0A%7D%0A

Update the Security configuration.