/*
 * Copyright 2002-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.security.samples.config;

import org.springframework.beans.factory.annotation.Autowired;
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.User;

/**
 * @author Joe Grandja
 */
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

	// @formatter:off
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http
				.authorizeRequests(authorizeRequests ->
					authorizeRequests
						.antMatchers("/css/**", "/index").permitAll()
						.antMatchers("/user/**").hasRole("USER")
				)
				.formLogin(formLogin ->
					formLogin
						.loginPage("/login")
						.failureUrl("/login-error")
				);
	}
	// @formatter:on

	// @formatter:off
	@Autowired
	public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
		auth
			.inMemoryAuthentication()
				.withUser(User.withDefaultPasswordEncoder().username("user").password("password").roles("USER"));
	}
	// @formatter:on
}

@@ -700,150 +700,31 @@
 ork.
-beans.factory.annotation.Autowired;%0Aimport org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
+context.annotation.Bean
 ;%0Aim
@@ -1053,16 +1053,232 @@
 ls.User;
+%0Aimport org.springframework.security.core.userdetails.UserDetails;%0Aimport org.springframework.security.core.userdetails.UserDetailsService;%0Aimport org.springframework.security.provisioning.InMemoryUserDetailsManager;
 %0A%0A/**%0A *
@@ -1800,171 +1800,92 @@
 n%0A%0A%09
-// @formatter:off%0A%09@Autowired%0A%09public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception %7B%0A%09%09auth%0A%09%09%09.inMemoryAuthentication()%0A%09%09%09%09.withUser(
+@Bean%0A%09public UserDetailsService userDetailsService() %7B%0A%09%09UserDetails userDetails = 
 User
@@ -1913,16 +1913,21 @@
 ncoder()
+%0A%09%09%09%09
 .usernam
@@ -1935,16 +1935,21 @@
 (%22user%22)
+%0A%09%09%09%09
 .passwor
@@ -1961,16 +1961,21 @@
 ssword%22)
+%0A%09%09%09%09
 .roles(%22
@@ -1980,34 +1980,82 @@
 (%22USER%22)
-);%0A%09%7D%0A%09// @formatter:on
+%0A%09%09%09%09.build();%0A%09%09return new InMemoryUserDetailsManager(userDetails);%0A%09%7D
 %0A%7D%0A

Use UserDetailsService bean in sample app