2
0

webflux.adoc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. [[jc-webflux]]
  2. = WebFlux Security
  3. Spring Security's WebFlux support relies on a `WebFilter` and works the same for Spring WebFlux and Spring WebFlux.Fn.
  4. A few sample applications demonstrate the code:
  5. * Hello WebFlux {gh-samples-url}/reactive/webflux/java/hello-security[hellowebflux]
  6. * Hello WebFlux.Fn {gh-samples-url}/reactive/webflux-fn/hello-security[hellowebfluxfn]
  7. * Hello WebFlux Method {gh-samples-url}/reactive/webflux/java/method[hellowebflux-method]
  8. == Minimal WebFlux Security Configuration
  9. The following listing shows a minimal WebFlux Security configuration:
  10. .Minimal WebFlux Security Configuration
  11. ====
  12. .Java
  13. [source,java,role="primary"]
  14. -----
  15. @Configuration
  16. @EnableWebFluxSecurity
  17. public class HelloWebfluxSecurityConfig {
  18. @Bean
  19. public MapReactiveUserDetailsService userDetailsService() {
  20. UserDetails user = User.withDefaultPasswordEncoder()
  21. .username("user")
  22. .password("user")
  23. .roles("USER")
  24. .build();
  25. return new MapReactiveUserDetailsService(user);
  26. }
  27. }
  28. -----
  29. .Kotlin
  30. [source,kotlin,role="secondary"]
  31. -----
  32. @Configuration
  33. @EnableWebFluxSecurity
  34. class HelloWebfluxSecurityConfig {
  35. @Bean
  36. fun userDetailsService(): ReactiveUserDetailsService {
  37. val userDetails = User.withDefaultPasswordEncoder()
  38. .username("user")
  39. .password("user")
  40. .roles("USER")
  41. .build()
  42. return MapReactiveUserDetailsService(userDetails)
  43. }
  44. }
  45. -----
  46. ====
  47. This configuration provides form and HTTP basic authentication, sets up authorization to require an authenticated user for accessing any page, sets up a default login page and a default logout page, sets up security related HTTP headers, adds CSRF protection, and more.
  48. == Explicit WebFlux Security Configuration
  49. The following page shows an explicit version of the minimal WebFlux Security configuration:
  50. .Explicit WebFlux Security Configuration
  51. ====
  52. .Java
  53. [source,java,role="primary"]
  54. -----
  55. @Configuration
  56. @EnableWebFluxSecurity
  57. public class HelloWebfluxSecurityConfig {
  58. @Bean
  59. public MapReactiveUserDetailsService userDetailsService() {
  60. UserDetails user = User.withDefaultPasswordEncoder()
  61. .username("user")
  62. .password("user")
  63. .roles("USER")
  64. .build();
  65. return new MapReactiveUserDetailsService(user);
  66. }
  67. @Bean
  68. public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
  69. http
  70. .authorizeExchange(exchanges -> exchanges
  71. .anyExchange().authenticated()
  72. )
  73. .httpBasic(withDefaults())
  74. .formLogin(withDefaults());
  75. return http.build();
  76. }
  77. }
  78. -----
  79. .Kotlin
  80. [source,kotlin,role="secondary"]
  81. -----
  82. import org.springframework.security.config.web.server.invoke
  83. @Configuration
  84. @EnableWebFluxSecurity
  85. class HelloWebfluxSecurityConfig {
  86. @Bean
  87. fun userDetailsService(): ReactiveUserDetailsService {
  88. val userDetails = User.withDefaultPasswordEncoder()
  89. .username("user")
  90. .password("user")
  91. .roles("USER")
  92. .build()
  93. return MapReactiveUserDetailsService(userDetails)
  94. }
  95. @Bean
  96. fun springSecurityFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
  97. return http {
  98. authorizeExchange {
  99. authorize(anyExchange, authenticated)
  100. }
  101. formLogin { }
  102. httpBasic { }
  103. }
  104. }
  105. }
  106. -----
  107. ====
  108. [NOTE]
  109. Make sure that you import the `invoke` function in your Kotlin class, sometimes the IDE will not auto-import it causing compilation issues.
  110. This configuration explicitly sets up all the same things as our minimal configuration.
  111. From here, you can more easily make changes to the defaults.
  112. You can find more examples of explicit configuration in unit tests, by searching for https://github.com/spring-projects/spring-security/search?q=path%3Aconfig%2Fsrc%2Ftest%2F+EnableWebFluxSecurity[`EnableWebFluxSecurity` in the `config/src/test/` directory].
  113. [[jc-webflux-multiple-filter-chains]]
  114. === Multiple Chains Support
  115. You can configure multiple `SecurityWebFilterChain` instances to separate configuration by `RequestMatcher` instances.
  116. For example, you can isolate configuration for URLs that start with `/api`:
  117. ====
  118. .Java
  119. [source,java,role="primary"]
  120. ----
  121. @Configuration
  122. @EnableWebFluxSecurity
  123. static class MultiSecurityHttpConfig {
  124. @Order(Ordered.HIGHEST_PRECEDENCE) <1>
  125. @Bean
  126. SecurityWebFilterChain apiHttpSecurity(ServerHttpSecurity http) {
  127. http
  128. .securityMatcher(new PathPatternParserServerWebExchangeMatcher("/api/**")) <2>
  129. .authorizeExchange((exchanges) -> exchanges
  130. .anyExchange().authenticated()
  131. )
  132. .oauth2ResourceServer(OAuth2ResourceServerSpec::jwt); <3>
  133. return http.build();
  134. }
  135. @Bean
  136. SecurityWebFilterChain webHttpSecurity(ServerHttpSecurity http) { <4>
  137. http
  138. .authorizeExchange((exchanges) -> exchanges
  139. .anyExchange().authenticated()
  140. )
  141. .httpBasic(withDefaults()); <5>
  142. return http.build();
  143. }
  144. @Bean
  145. ReactiveUserDetailsService userDetailsService() {
  146. return new MapReactiveUserDetailsService(
  147. PasswordEncodedUser.user(), PasswordEncodedUser.admin());
  148. }
  149. }
  150. ----
  151. .Kotlin
  152. [source,kotlin,role="secondary"]
  153. ----
  154. import org.springframework.security.config.web.server.invoke
  155. @Configuration
  156. @EnableWebFluxSecurity
  157. open class MultiSecurityHttpConfig {
  158. @Order(Ordered.HIGHEST_PRECEDENCE) <1>
  159. @Bean
  160. open fun apiHttpSecurity(http: ServerHttpSecurity): SecurityWebFilterChain {
  161. return http {
  162. securityMatcher(PathPatternParserServerWebExchangeMatcher("/api/**")) <2>
  163. authorizeExchange {
  164. authorize(anyExchange, authenticated)
  165. }
  166. oauth2ResourceServer {
  167. jwt { } <3>
  168. }
  169. }
  170. }
  171. @Bean
  172. open fun webHttpSecurity(http: ServerHttpSecurity): SecurityWebFilterChain { <4>
  173. return http {
  174. authorizeExchange {
  175. authorize(anyExchange, authenticated)
  176. }
  177. httpBasic { } <5>
  178. }
  179. }
  180. @Bean
  181. open fun userDetailsService(): ReactiveUserDetailsService {
  182. return MapReactiveUserDetailsService(
  183. PasswordEncodedUser.user(), PasswordEncodedUser.admin()
  184. )
  185. }
  186. }
  187. ----
  188. <1> Configure a `SecurityWebFilterChain` with an `@Order` to specify which `SecurityWebFilterChain` Spring Security should consider first
  189. <2> Use `PathPatternParserServerWebExchangeMatcher` to state that this `SecurityWebFilterChain` will only apply to URL paths that start with `/api/`
  190. <3> Specify the authentication mechanisms that will be used for `/api/**` endpoints
  191. <4> Create another instance of `SecurityWebFilterChain` with lower precedence to match all other URLs
  192. <5> Specify the authentication mechanisms that will be used for the rest of the application
  193. ====
  194. Spring Security selects one `SecurityWebFilterChain` `@Bean` for each request.
  195. It matches the requests in order by the `securityMatcher` definition.
  196. In this case, that means that, if the URL path starts with `/api`, Spring Security uses `apiHttpSecurity`.
  197. If the URL does not start with `/api`, Spring Security defaults to `webHttpSecurity`, which has an implied `securityMatcher` that matches any request.