|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2020 the original author or authors. |
| 2 | + * Copyright 2002-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
26 | 26 | import org.springframework.context.annotation.Import;
|
27 | 27 | import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication;
|
28 | 28 | import org.springframework.security.config.annotation.web.WebSecurityConfigurer;
|
| 29 | +import org.springframework.security.web.SecurityFilterChain; |
29 | 30 |
|
30 | 31 | /**
|
31 | 32 | * Add this annotation to an {@code @Configuration} class to have the Spring Security
|
32 |
| - * configuration defined in any {@link WebSecurityConfigurer} or more likely by extending |
33 |
| - * the {@link WebSecurityConfigurerAdapter} base class and overriding individual methods: |
| 33 | + * configuration defined in any {@link WebSecurityConfigurer} or more likely by exposing a |
| 34 | + * {@link SecurityFilterChain} bean: |
34 | 35 | *
|
35 | 36 | * <pre class="code">
|
36 | 37 | * @Configuration
|
37 | 38 | * @EnableWebSecurity
|
38 |
| - * public class MyWebSecurityConfiguration extends WebSecurityConfigurerAdapter { |
| 39 | + * public class MyWebSecurityConfiguration { |
39 | 40 | *
|
40 |
| - * @Override |
41 |
| - * public void configure(WebSecurity web) throws Exception { |
42 |
| - * web.ignoring() |
| 41 | + * @Bean |
| 42 | + * public WebSecurityCustomizer webSecurityCustomizer() { |
| 43 | + * return (web) -> web.ignoring() |
43 | 44 | * // Spring Security should completely ignore URLs starting with /resources/
|
44 | 45 | * .antMatchers("/resources/**");
|
45 | 46 | * }
|
46 | 47 | *
|
47 |
| - * @Override |
48 |
| - * protected void configure(HttpSecurity http) throws Exception { |
| 48 | + * @Bean |
| 49 | + * public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { |
49 | 50 | * http.authorizeRequests().antMatchers("/public/**").permitAll().anyRequest()
|
50 | 51 | * .hasRole("USER").and()
|
51 | 52 | * // Possibly more configuration ...
|
52 | 53 | * .formLogin() // enable form based log in
|
53 | 54 | * // set permitAll for all URLs associated with Form Login
|
54 | 55 | * .permitAll();
|
| 56 | + * return http.build(); |
55 | 57 | * }
|
56 | 58 | *
|
57 |
| - * @Override |
58 |
| - * protected void configure(AuthenticationManagerBuilder auth) throws Exception { |
59 |
| - * auth |
60 |
| - * // enable in memory based authentication with a user named "user" and "admin" |
61 |
| - * .inMemoryAuthentication().withUser("user").password("password").roles("USER") |
62 |
| - * .and().withUser("admin").password("password").roles("USER", "ADMIN"); |
| 59 | + * @Bean |
| 60 | + * public UserDetailsService userDetailsService() { |
| 61 | + * UserDetails user = User.withDefaultPasswordEncoder() |
| 62 | + * .username("user") |
| 63 | + * .password("password") |
| 64 | + * .roles("USER") |
| 65 | + * .build(); |
| 66 | + * UserDetails admin = User.withDefaultPasswordEncoder() |
| 67 | + * .username("admin") |
| 68 | + * .password("password") |
| 69 | + * .roles("ADMIN", "USER") |
| 70 | + * .build(); |
| 71 | + * return new InMemoryUserDetailsManager(user, admin); |
63 | 72 | * }
|
64 | 73 | *
|
65 |
| - * // Possibly more overridden methods ... |
| 74 | + * // Possibly more bean methods ... |
66 | 75 | * }
|
67 | 76 | * </pre>
|
68 | 77 | *
|
69 | 78 | * @see WebSecurityConfigurer
|
70 |
| - * @see WebSecurityConfigurerAdapter |
71 | 79 | *
|
72 | 80 | * @author Rob Winch
|
73 | 81 | * @since 3.2
|
|
0 commit comments