2020import org .springframework .context .annotation .Bean ;
2121import org .springframework .context .annotation .Configuration ;
2222import org .springframework .security .authentication .AuthenticationManager ;
23- import org .springframework .security .config .annotation .method .configuration .EnableGlobalMethodSecurity ;
23+ import org .springframework .security .config .annotation .authentication .builders .AuthenticationManagerBuilder ;
24+ import org .springframework .security .config .annotation .method .configuration .EnableMethodSecurity ;
2425import org .springframework .security .config .annotation .web .builders .HttpSecurity ;
2526import org .springframework .security .config .annotation .web .configuration .EnableWebSecurity ;
26- import org .springframework .security .config .annotation .web .configuration .WebSecurityConfigurerAdapter ;
27- import org .springframework .security .core .userdetails .User ;
28- import org .springframework .security .core .userdetails .UserDetailsService ;
2927import org .springframework .security .crypto .bcrypt .BCryptPasswordEncoder ;
3028import org .springframework .security .crypto .password .PasswordEncoder ;
31- import org .springframework .security .provisioning . InMemoryUserDetailsManager ;
29+ import org .springframework .security .web . SecurityFilterChain ;
3230
3331/**
3432 * Security configuration
3533 */
3634@ Configuration
3735@ EnableWebSecurity
38- @ EnableGlobalMethodSecurity (prePostEnabled = true )
39- public class DemoSecurity
40- extends WebSecurityConfigurerAdapter {
36+ @ EnableMethodSecurity
37+ public class CompleteSecurity {
4138
42- @ Override
43- protected void configure (HttpSecurity http ) throws Exception {
44- http
45- .authorizeRequests ()
39+ @ Bean
40+ public SecurityFilterChain filterChain (HttpSecurity http , AuthenticationManager authManager ) throws Exception {
41+ http . authorizeHttpRequests ()
42+ .requestMatchers ( "/ping" ). permitAll ()
4643 .requestMatchers (EndpointRequest .to ("info" )).permitAll ()
47- .requestMatchers (EndpointRequest .toAnyEndpoint ()).hasRole ("ACTUATOR" );
48- }
49-
50- @ Bean ("customAuthManager" )
51- @ Override
52- public AuthenticationManager authenticationManager () throws Exception {
53- return super .authenticationManager ();
44+ .requestMatchers (EndpointRequest .toAnyEndpoint ()).hasRole ("ACTUATOR" )
45+ .and ().authenticationManager (authManager );
46+ return http .build ();
5447 }
5548
5649 @ Bean
@@ -59,14 +52,21 @@ public PasswordEncoder passwordEncoder() {
5952 }
6053
6154 @ Bean
62- public UserDetailsService userDetailsService () {
63- InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager ();
64- PasswordEncoder encoder = passwordEncoder ();
65- manager .createUser (User .withUsername ("user" ).password (encoder .encode ("password" )).roles ("USER" ).build ());
66- manager .createUser (User .withUsername ("actuator" ).password (encoder .encode ("password" )).roles ("ACTUATOR" ).build
67- ());
68- manager .createUser (User .withUsername ("admin" ).password (encoder .encode ("admin" )).roles ("ADMIN" , "ACTUATOR" )
69- .build ());
70- return manager ;
55+ public AuthenticationManager authManager (HttpSecurity http ) throws Exception {
56+ AuthenticationManagerBuilder authenticationManagerBuilder =
57+ http .getSharedObject (AuthenticationManagerBuilder .class );
58+ authenticationManagerBuilder .inMemoryAuthentication ()
59+ .withUser ("user" )
60+ .password (passwordEncoder ().encode ("password" ))
61+ .roles ("USER" )
62+ .and ()
63+ .withUser ("actuator" )
64+ .password (passwordEncoder ().encode ("password" ))
65+ .roles ("ACTUATOR" )
66+ .and ()
67+ .withUser ("admin" )
68+ .password (passwordEncoder ().encode ("admin" ))
69+ .roles ("ADMIN" , "ACTUATOR" );
70+ return authenticationManagerBuilder .build ();
7171 }
7272}
0 commit comments