Simplify UserDetailsService creation in samples

Closes gh-10385
pull/10235/merge
Madhura Bhave 7 years ago
parent 61bbaf140c
commit 7093602753

@ -176,7 +176,7 @@
<spring-plugin.version>1.2.0.RELEASE</spring-plugin.version> <spring-plugin.version>1.2.0.RELEASE</spring-plugin.version>
<spring-restdocs.version>1.2.1.RELEASE</spring-restdocs.version> <spring-restdocs.version>1.2.1.RELEASE</spring-restdocs.version>
<spring-retry.version>1.2.1.RELEASE</spring-retry.version> <spring-retry.version>1.2.1.RELEASE</spring-retry.version>
<spring-security.version>5.0.0.M4</spring-security.version> <spring-security.version>5.0.0.BUILD-SNAPSHOT</spring-security.version>
<spring-session.version>2.0.0.M4</spring-session.version> <spring-session.version>2.0.0.M4</spring-session.version>
<spring-session-data-mongodb.version>2.0.0.M3</spring-session-data-mongodb.version> <spring-session-data-mongodb.version>2.0.0.M3</spring-session-data-mongodb.version>
<spring-social.version>2.0.0.M4</spring-social.version> <spring-social.version>2.0.0.M4</spring-social.version>

@ -20,18 +20,14 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager; import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@SpringBootApplication @SpringBootApplication
public class HelloWebSecurityApplication { public class HelloWebSecurityApplication {
@Bean @Bean
public UserDetailsService userDetailsService() throws Exception { public InMemoryUserDetailsManager inMemoryUserDetailsManager() throws Exception {
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); return new InMemoryUserDetailsManager(User.withUsername("user").password("password").roles("USER").build());
manager.createUser(
User.withUsername("user").password("password").roles("USER").build());
return manager;
} }
public static void main(String[] args) { public static void main(String[] args) {

@ -20,18 +20,14 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager; import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@SpringBootApplication @SpringBootApplication
public class SampleActuatorLog4J2Application { public class SampleActuatorLog4J2Application {
@Bean @Bean
public UserDetailsService userDetailsService() throws Exception { public InMemoryUserDetailsManager inMemoryUserDetailsManager() throws Exception {
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); return new InMemoryUserDetailsManager(User.withUsername("user").password("password").roles("USER").build());
manager.createUser(
User.withUsername("user").password("password").roles("USER").build());
return manager;
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {

@ -23,7 +23,6 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager; import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -34,11 +33,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
public class SampleActuatorUiApplication { public class SampleActuatorUiApplication {
@Bean @Bean
public UserDetailsService userDetailsService() throws Exception { public InMemoryUserDetailsManager inMemoryUserDetailsManager() throws Exception {
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); return new InMemoryUserDetailsManager(User.withUsername("user").password("password").roles("USER").build());
manager.createUser(
User.withUsername("user").password("password").roles("USER").build());
return manager;
} }
@GetMapping("/") @GetMapping("/")

@ -23,7 +23,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager; import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@SpringBootApplication @SpringBootApplication
@ -35,11 +34,8 @@ public class SampleActuatorApplication {
} }
@Bean @Bean
public UserDetailsService userDetailsService() throws Exception { public InMemoryUserDetailsManager inMemoryUserDetailsManager() throws Exception {
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); return new InMemoryUserDetailsManager(User.withUsername("user").password("password").roles("USER").build());
manager.createUser(
User.withUsername("user").password("password").roles("USER").build());
return manager;
} }
@Bean @Bean

@ -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.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager; import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@EnableAutoConfiguration @EnableAutoConfiguration
@ -39,11 +38,8 @@ public class SampleSecureApplication implements CommandLineRunner {
private SampleService service; private SampleService service;
@Bean @Bean
public UserDetailsService userDetailsService() throws Exception { public InMemoryUserDetailsManager inMemoryUserDetailsManager() throws Exception {
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); return new InMemoryUserDetailsManager(User.withUsername("user").password("password").roles("USER").build());
manager.createUser(
User.withUsername("user").password("password").roles("USER").build());
return manager;
} }
@Override @Override

@ -31,7 +31,6 @@ import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager; import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@SpringBootConfiguration @SpringBootConfiguration
@ -39,11 +38,8 @@ import org.springframework.security.provisioning.InMemoryUserDetailsManager;
public class SampleServletApplication extends SpringBootServletInitializer { public class SampleServletApplication extends SpringBootServletInitializer {
@Bean @Bean
public UserDetailsService userDetailsService() throws Exception { public InMemoryUserDetailsManager inMemoryUserDetailsManager() throws Exception {
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); return new InMemoryUserDetailsManager(User.withUsername("user").password("password").roles("USER").build());
manager.createUser(
User.withUsername("user").password("password").roles("USER").build());
return manager;
} }
@SuppressWarnings("serial") @SuppressWarnings("serial")

@ -20,7 +20,6 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager; import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@SpringBootApplication @SpringBootApplication
@ -31,11 +30,8 @@ public class SampleSessionApplication {
} }
@Bean @Bean
public UserDetailsService userDetailsService() { public InMemoryUserDetailsManager inMemoryUserDetailsManager() throws Exception {
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); return new InMemoryUserDetailsManager(User.withUsername("user").password("password").roles("USER").build());
manager.createUser(
User.withUsername("user").password("password").roles("USER").build());
return manager;
} }
} }

@ -21,7 +21,6 @@ import org.springframework.boot.test.autoconfigure.web.servlet.MockMvcSecurityAu
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.security.access.annotation.Secured; import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager; import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -35,11 +34,8 @@ import org.springframework.web.bind.annotation.RestController;
public class SecurityTestApplication { public class SecurityTestApplication {
@Bean @Bean
public UserDetailsService userDetailsService() throws Exception { public InMemoryUserDetailsManager inMemoryUserDetailsManager() throws Exception {
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); return new InMemoryUserDetailsManager(User.withUsername("user").password("password").roles("USER").build());
manager.createUser(
User.withUsername("user").password("secret").roles("USER").build());
return manager;
} }
@RestController @RestController

Loading…
Cancel
Save