SHARE
TWEET

Untitled

a guest Aug 18th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.security;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.security.authentication.ReactiveAuthenticationManager;
  7. import org.springframework.security.config.web.server.ServerHttpSecurity;
  8. import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
  9. import org.springframework.security.core.userdetails.User;
  10. import org.springframework.security.web.server.SecurityWebFilterChain;
  11. import org.springframework.web.reactive.function.server.HandlerFunction;
  12. import org.springframework.web.reactive.function.server.RouterFunction;
  13. import org.springframework.web.reactive.function.server.ServerRequest;
  14. import org.springframework.web.reactive.function.server.ServerResponse;
  15. import reactor.core.publisher.Mono;
  16.  
  17. import java.util.Collection;
  18. import java.util.Collections;
  19. import java.util.Map;
  20.  
  21. import static org.springframework.web.reactive.function.server.RouterFunctions.route;
  22. import static org.springframework.web.reactive.function.server.ServerResponse.*;
  23.  
  24. @SpringBootApplication
  25. public class SecurityApplication {
  26.  
  27.     private final static String GREETING_RESOURCE = "/greeting";
  28.  
  29.     @Bean
  30.     RouterFunction<ServerResponse> routes() {
  31.         return route()
  32.             .GET(GREETING_RESOURCE, r -> ok().body(Collections.singletonMap("greeting", "Hello world!")))
  33.             .build();
  34.     }
  35.  
  36.     @Bean
  37.     MapReactiveUserDetailsService userDetailsService() {
  38.         return new MapReactiveUserDetailsService(User.withDefaultPasswordEncoder().username("jlong").password("pw").roles("USER").build());
  39.     }
  40.  
  41.     @Bean
  42.     SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
  43.         return http
  44.             .httpBasic()
  45.             .and()
  46.             .authorizeExchange(spec -> spec
  47.                 .pathMatchers(GREETING_RESOURCE).authenticated()
  48.                 .anyExchange().permitAll()
  49.             )
  50.             .csrf(ServerHttpSecurity.CsrfSpec::disable)
  51.             .build();
  52.     }
  53.  
  54.     public static void main(String[] args) {
  55.         SpringApplication.run(SecurityApplication.class, args);
  56.     }
  57.  
  58. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top