SecurityConfig.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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.web.server.SecurityWebFilterChain;
  25. /**
  26. * @author Rob Winch
  27. * @since 5.0
  28. */
  29. @EnableWebFluxSecurity
  30. @EnableReactiveMethodSecurity
  31. public class SecurityConfig {
  32. @Bean
  33. SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) throws Exception {
  34. return http
  35. // Demonstrate that method security works
  36. // Best practice to use both for defense in depth
  37. .authorizeExchange()
  38. .anyExchange().permitAll()
  39. .and()
  40. .httpBasic().and()
  41. .build();
  42. }
  43. @Bean
  44. public MapReactiveUserDetailsService userDetailsRepository() {
  45. UserDetails rob = User.withUsername("rob").password("rob").roles("USER").build();
  46. UserDetails admin = User.withUsername("admin").password("admin").roles("USER","ADMIN").build();
  47. return new MapReactiveUserDetailsService(rob, admin);
  48. }
  49. }