SecurityConfiguration.java 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright 2020 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. * https://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 example;
  17. import org.springframework.context.annotation.Bean;
  18. import org.springframework.context.annotation.Configuration;
  19. import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
  20. import org.springframework.security.config.web.server.ServerHttpSecurity;
  21. import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
  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. import static org.springframework.security.config.Customizer.withDefaults;
  26. /**
  27. * Example of security configuration for oauth client usage.
  28. *
  29. * @author Rob Winch
  30. */
  31. @Configuration
  32. @EnableWebFluxSecurity
  33. public class SecurityConfiguration {
  34. @Bean
  35. SecurityWebFilterChain configure(ServerHttpSecurity http) {
  36. // @formatter:off
  37. http
  38. .authorizeExchange((authorize) -> authorize
  39. .pathMatchers("/", "/public/**").permitAll()
  40. .anyExchange().authenticated()
  41. )
  42. .oauth2Login(withDefaults())
  43. .formLogin(withDefaults())
  44. .oauth2Client(withDefaults());
  45. // @formatter:on
  46. return http.build();
  47. }
  48. @Bean
  49. MapReactiveUserDetailsService userDetailsService() {
  50. // @formatter:off
  51. UserDetails userDetails = User.withDefaultPasswordEncoder()
  52. .username("user")
  53. .password("password")
  54. .roles("USER")
  55. .build();
  56. // @formatter:on
  57. return new MapReactiveUserDetailsService(userDetails);
  58. }
  59. }