2
0

SecurityConfig.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright 2002-2017 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package sample;
  17. import org.springframework.context.annotation.Bean;
  18. import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity;
  19. import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
  20. import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
  21. import org.springframework.security.config.web.server.ServerHttpSecurity;
  22. import org.springframework.security.core.userdetails.User;
  23. import org.springframework.security.core.userdetails.UserDetails;
  24. import org.springframework.security.crypto.factory.PasswordEncoderFactories;
  25. import org.springframework.security.web.server.SecurityWebFilterChain;
  26. /**
  27. * @author Rob Winch
  28. * @since 5.0
  29. */
  30. @EnableWebFluxSecurity
  31. @EnableReactiveMethodSecurity
  32. public class SecurityConfig {
  33. @Bean
  34. SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) throws Exception {
  35. return http
  36. // Demonstrate that method security works
  37. // Best practice to use both for defense in depth
  38. .authorizeExchange()
  39. .anyExchange().permitAll()
  40. .and()
  41. .httpBasic().and()
  42. .build();
  43. }
  44. @Bean
  45. public MapReactiveUserDetailsService userDetailsRepository() {
  46. User.UserBuilder userBuilder = User.withDefaultPasswordEncoder();
  47. UserDetails rob = userBuilder.username("rob").password("rob").roles("USER").build();
  48. UserDetails admin = userBuilder.username("admin").password("admin").roles("USER","ADMIN").build();
  49. return new MapReactiveUserDetailsService(rob, admin);
  50. }
  51. }