|
16 | 16 | package org.springframework.data.rest.tests.security; |
17 | 17 |
|
18 | 18 | import org.springframework.beans.factory.annotation.Autowired; |
| 19 | +import org.springframework.context.annotation.Bean; |
19 | 20 | import org.springframework.context.annotation.Configuration; |
20 | 21 | import org.springframework.http.HttpMethod; |
21 | 22 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
22 | | -import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; |
| 23 | +import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; |
23 | 24 | import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
24 | 25 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
25 | | -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 26 | +import org.springframework.security.web.SecurityFilterChain; |
26 | 27 |
|
27 | 28 | // tag::code[] |
28 | 29 | @Configuration // <1> |
29 | 30 | @EnableWebSecurity |
30 | | -@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) // <2> |
31 | | -class SecurityConfiguration extends WebSecurityConfigurerAdapter { // <3> |
32 | | -// end::code[] |
| 31 | +@EnableMethodSecurity(securedEnabled = true, prePostEnabled = true) // <2> |
| 32 | +class SecurityConfiguration { // <3> |
| 33 | + // end::code[] |
33 | 34 | @Autowired |
34 | 35 | void configureAuth(AuthenticationManagerBuilder auth) throws Exception { |
35 | 36 |
|
36 | 37 | auth.inMemoryAuthentication() |
37 | | - .withUser("user").password("user").roles("USER").and() |
38 | | - .withUser("admin").password("admin").roles("USER", "ADMIN"); |
| 38 | + .withUser("user").password("user").roles("USER").and() |
| 39 | + .withUser("admin").password("admin").roles("USER", "ADMIN"); |
39 | 40 | } |
40 | 41 |
|
41 | | - @Override |
42 | | - protected void configure(HttpSecurity http) throws Exception { |
| 42 | + @Bean |
| 43 | + SecurityFilterChain filterChain(HttpSecurity security) throws Exception { |
43 | 44 |
|
44 | | - http. |
45 | | - authorizeRequests() |
46 | | - .antMatchers(HttpMethod.GET, "/").permitAll() // Ignore security at the root URI. |
47 | | - .anyRequest().authenticated() |
48 | | - .and() |
49 | | - .httpBasic() |
50 | | - .and() |
51 | | - .csrf().disable(); // Disable CSRF since it's not critical for the scope of testing. |
| 45 | + return security |
| 46 | + .authorizeHttpRequests(it -> it.requestMatchers(HttpMethod.GET, "/") |
| 47 | + .permitAll().anyRequest().authenticated()) |
| 48 | + .csrf(it -> it.disable()) |
| 49 | + .httpBasic().and() |
| 50 | + .build(); |
52 | 51 | } |
53 | 52 | } |
0 commit comments