From 7093602753f37ec9073691550d781d6586557b77 Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Tue, 26 Sep 2017 13:58:34 -0700 Subject: [PATCH] Simplify UserDetailsService creation in samples Closes gh-10385 --- spring-boot-dependencies/pom.xml | 2 +- .../src/main/java/sample/HelloWebSecurityApplication.java | 8 ++------ .../actuator/log4j2/SampleActuatorLog4J2Application.java | 8 ++------ .../sample/actuator/ui/SampleActuatorUiApplication.java | 8 ++------ .../java/sample/actuator/SampleActuatorApplication.java | 8 ++------ .../main/java/sample/secure/SampleSecureApplication.java | 8 ++------ .../java/sample/servlet/SampleServletApplication.java | 8 ++------ .../java/sample/session/SampleSessionApplication.java | 8 ++------ .../autoconfigure/security/SecurityTestApplication.java | 8 ++------ 9 files changed, 17 insertions(+), 49 deletions(-) diff --git a/spring-boot-dependencies/pom.xml b/spring-boot-dependencies/pom.xml index 6256f68a0e..78d710678e 100644 --- a/spring-boot-dependencies/pom.xml +++ b/spring-boot-dependencies/pom.xml @@ -176,7 +176,7 @@ 1.2.0.RELEASE 1.2.1.RELEASE 1.2.1.RELEASE - 5.0.0.M4 + 5.0.0.BUILD-SNAPSHOT 2.0.0.M4 2.0.0.M3 2.0.0.M4 diff --git a/spring-boot-integration-tests/spring-boot-security-tests/spring-boot-security-test-web-helloworld/src/main/java/sample/HelloWebSecurityApplication.java b/spring-boot-integration-tests/spring-boot-security-tests/spring-boot-security-test-web-helloworld/src/main/java/sample/HelloWebSecurityApplication.java index 06b3a4bdcd..ff8c341a39 100644 --- a/spring-boot-integration-tests/spring-boot-security-tests/spring-boot-security-test-web-helloworld/src/main/java/sample/HelloWebSecurityApplication.java +++ b/spring-boot-integration-tests/spring-boot-security-tests/spring-boot-security-test-web-helloworld/src/main/java/sample/HelloWebSecurityApplication.java @@ -20,18 +20,14 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.security.core.userdetails.User; -import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.provisioning.InMemoryUserDetailsManager; @SpringBootApplication public class HelloWebSecurityApplication { @Bean - public UserDetailsService userDetailsService() throws Exception { - InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); - manager.createUser( - User.withUsername("user").password("password").roles("USER").build()); - return manager; + public InMemoryUserDetailsManager inMemoryUserDetailsManager() throws Exception { + return new InMemoryUserDetailsManager(User.withUsername("user").password("password").roles("USER").build()); } public static void main(String[] args) { diff --git a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/SampleActuatorLog4J2Application.java b/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/SampleActuatorLog4J2Application.java index b3135d68d4..8aab61db50 100644 --- a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/SampleActuatorLog4J2Application.java +++ b/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/SampleActuatorLog4J2Application.java @@ -20,18 +20,14 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.security.core.userdetails.User; -import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.provisioning.InMemoryUserDetailsManager; @SpringBootApplication public class SampleActuatorLog4J2Application { @Bean - public UserDetailsService userDetailsService() throws Exception { - InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); - manager.createUser( - User.withUsername("user").password("password").roles("USER").build()); - return manager; + public InMemoryUserDetailsManager inMemoryUserDetailsManager() throws Exception { + return new InMemoryUserDetailsManager(User.withUsername("user").password("password").roles("USER").build()); } public static void main(String[] args) throws Exception { diff --git a/spring-boot-samples/spring-boot-sample-actuator-ui/src/main/java/sample/actuator/ui/SampleActuatorUiApplication.java b/spring-boot-samples/spring-boot-sample-actuator-ui/src/main/java/sample/actuator/ui/SampleActuatorUiApplication.java index 30774fdc0d..6f3a604e66 100644 --- a/spring-boot-samples/spring-boot-sample-actuator-ui/src/main/java/sample/actuator/ui/SampleActuatorUiApplication.java +++ b/spring-boot-samples/spring-boot-sample-actuator-ui/src/main/java/sample/actuator/ui/SampleActuatorUiApplication.java @@ -23,7 +23,6 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.security.core.userdetails.User; -import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.provisioning.InMemoryUserDetailsManager; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @@ -34,11 +33,8 @@ import org.springframework.web.bind.annotation.RequestMapping; public class SampleActuatorUiApplication { @Bean - public UserDetailsService userDetailsService() throws Exception { - InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); - manager.createUser( - User.withUsername("user").password("password").roles("USER").build()); - return manager; + public InMemoryUserDetailsManager inMemoryUserDetailsManager() throws Exception { + return new InMemoryUserDetailsManager(User.withUsername("user").password("password").roles("USER").build()); } @GetMapping("/") diff --git a/spring-boot-samples/spring-boot-sample-actuator/src/main/java/sample/actuator/SampleActuatorApplication.java b/spring-boot-samples/spring-boot-sample-actuator/src/main/java/sample/actuator/SampleActuatorApplication.java index 61c068da70..771236e59a 100644 --- a/spring-boot-samples/spring-boot-sample-actuator/src/main/java/sample/actuator/SampleActuatorApplication.java +++ b/spring-boot-samples/spring-boot-sample-actuator/src/main/java/sample/actuator/SampleActuatorApplication.java @@ -23,7 +23,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.security.core.userdetails.User; -import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.provisioning.InMemoryUserDetailsManager; @SpringBootApplication @@ -35,11 +34,8 @@ public class SampleActuatorApplication { } @Bean - public UserDetailsService userDetailsService() throws Exception { - InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); - manager.createUser( - User.withUsername("user").password("password").roles("USER").build()); - return manager; + public InMemoryUserDetailsManager inMemoryUserDetailsManager() throws Exception { + return new InMemoryUserDetailsManager(User.withUsername("user").password("password").roles("USER").build()); } @Bean diff --git a/spring-boot-samples/spring-boot-sample-secure/src/main/java/sample/secure/SampleSecureApplication.java b/spring-boot-samples/spring-boot-sample-secure/src/main/java/sample/secure/SampleSecureApplication.java index 8c3c05b4ae..e17aa779cd 100644 --- a/spring-boot-samples/spring-boot-sample-secure/src/main/java/sample/secure/SampleSecureApplication.java +++ b/spring-boot-samples/spring-boot-sample-secure/src/main/java/sample/secure/SampleSecureApplication.java @@ -27,7 +27,6 @@ import org.springframework.security.config.annotation.method.configuration.Enabl import org.springframework.security.core.authority.AuthorityUtils; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.User; -import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.provisioning.InMemoryUserDetailsManager; @EnableAutoConfiguration @@ -39,11 +38,8 @@ public class SampleSecureApplication implements CommandLineRunner { private SampleService service; @Bean - public UserDetailsService userDetailsService() throws Exception { - InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); - manager.createUser( - User.withUsername("user").password("password").roles("USER").build()); - return manager; + public InMemoryUserDetailsManager inMemoryUserDetailsManager() throws Exception { + return new InMemoryUserDetailsManager(User.withUsername("user").password("password").roles("USER").build()); } @Override diff --git a/spring-boot-samples/spring-boot-sample-servlet/src/main/java/sample/servlet/SampleServletApplication.java b/spring-boot-samples/spring-boot-sample-servlet/src/main/java/sample/servlet/SampleServletApplication.java index 3a35fa5bc4..23e21812ff 100644 --- a/spring-boot-samples/spring-boot-sample-servlet/src/main/java/sample/servlet/SampleServletApplication.java +++ b/spring-boot-samples/spring-boot-sample-servlet/src/main/java/sample/servlet/SampleServletApplication.java @@ -31,7 +31,6 @@ import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.context.annotation.Bean; import org.springframework.security.core.userdetails.User; -import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.provisioning.InMemoryUserDetailsManager; @SpringBootConfiguration @@ -39,11 +38,8 @@ import org.springframework.security.provisioning.InMemoryUserDetailsManager; public class SampleServletApplication extends SpringBootServletInitializer { @Bean - public UserDetailsService userDetailsService() throws Exception { - InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); - manager.createUser( - User.withUsername("user").password("password").roles("USER").build()); - return manager; + public InMemoryUserDetailsManager inMemoryUserDetailsManager() throws Exception { + return new InMemoryUserDetailsManager(User.withUsername("user").password("password").roles("USER").build()); } @SuppressWarnings("serial") diff --git a/spring-boot-samples/spring-boot-sample-session/src/main/java/sample/session/SampleSessionApplication.java b/spring-boot-samples/spring-boot-sample-session/src/main/java/sample/session/SampleSessionApplication.java index 202468a4d7..f5bb114a6d 100644 --- a/spring-boot-samples/spring-boot-sample-session/src/main/java/sample/session/SampleSessionApplication.java +++ b/spring-boot-samples/spring-boot-sample-session/src/main/java/sample/session/SampleSessionApplication.java @@ -20,7 +20,6 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.security.core.userdetails.User; -import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.provisioning.InMemoryUserDetailsManager; @SpringBootApplication @@ -31,11 +30,8 @@ public class SampleSessionApplication { } @Bean - public UserDetailsService userDetailsService() { - InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); - manager.createUser( - User.withUsername("user").password("password").roles("USER").build()); - return manager; + public InMemoryUserDetailsManager inMemoryUserDetailsManager() throws Exception { + return new InMemoryUserDetailsManager(User.withUsername("user").password("password").roles("USER").build()); } } diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/security/SecurityTestApplication.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/security/SecurityTestApplication.java index 86ba68eacc..8cfe8f5a48 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/security/SecurityTestApplication.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/security/SecurityTestApplication.java @@ -21,7 +21,6 @@ import org.springframework.boot.test.autoconfigure.web.servlet.MockMvcSecurityAu import org.springframework.context.annotation.Bean; import org.springframework.security.access.annotation.Secured; import org.springframework.security.core.userdetails.User; -import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.provisioning.InMemoryUserDetailsManager; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -35,11 +34,8 @@ import org.springframework.web.bind.annotation.RestController; public class SecurityTestApplication { @Bean - public UserDetailsService userDetailsService() throws Exception { - InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); - manager.createUser( - User.withUsername("user").password("secret").roles("USER").build()); - return manager; + public InMemoryUserDetailsManager inMemoryUserDetailsManager() throws Exception { + return new InMemoryUserDetailsManager(User.withUsername("user").password("password").roles("USER").build()); } @RestController