Application.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.beans.factory.annotation.Autowired;
  18. import org.springframework.beans.factory.annotation.Value;
  19. import org.springframework.context.ApplicationContext;
  20. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  21. import org.springframework.context.annotation.Bean;
  22. import org.springframework.context.annotation.ComponentScan;
  23. import org.springframework.context.annotation.Configuration;
  24. import org.springframework.core.ReactiveAdapterRegistry;
  25. import org.springframework.http.server.reactive.HttpHandler;
  26. import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
  27. import org.springframework.security.authentication.ReactiveAuthenticationManager;
  28. import org.springframework.security.authentication.UserDetailsRepositoryAuthenticationManager;
  29. import org.springframework.security.authorization.AuthorizationDecision;
  30. import org.springframework.security.config.web.server.AuthorizeExchangeBuilder;
  31. import org.springframework.security.config.web.server.HttpSecurity;
  32. import org.springframework.security.core.Authentication;
  33. import org.springframework.security.web.reactive.result.method.annotation.AuthenticationPrincipalArgumentResolver;
  34. import org.springframework.security.web.server.authorization.AuthorizationContext;
  35. import org.springframework.security.web.server.context.WebSessionSecurityContextRepository;
  36. import org.springframework.web.reactive.DispatcherHandler;
  37. import org.springframework.web.reactive.config.EnableWebFlux;
  38. import org.springframework.web.reactive.config.WebFluxConfigurer;
  39. import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;
  40. import org.springframework.web.server.WebFilter;
  41. import reactor.core.publisher.Mono;
  42. import reactor.ipc.netty.NettyContext;
  43. import reactor.ipc.netty.http.server.HttpServer;
  44. import static org.springframework.security.config.web.server.HttpSecurity.http;
  45. /**
  46. * @author Rob Winch
  47. * @since 5.0
  48. */
  49. @Configuration
  50. @EnableWebFlux
  51. @ComponentScan
  52. public class Application implements WebFluxConfigurer {
  53. @Value("${server.port:8080}")
  54. private int port = 8080;
  55. @Autowired
  56. private ReactiveAdapterRegistry adapterRegistry = new ReactiveAdapterRegistry();
  57. public static void main(String[] args) throws Exception {
  58. try(AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Application.class)) {
  59. context.getBean(NettyContext.class).onClose().block();
  60. }
  61. }
  62. @Override
  63. public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) {
  64. configurer.addCustomResolver(authenticationPrincipalArgumentResolver());
  65. }
  66. @Bean
  67. public NettyContext nettyContext(ApplicationContext context) {
  68. HttpHandler handler = DispatcherHandler.toHttpHandler(context);
  69. ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
  70. HttpServer httpServer = HttpServer.create("localhost", port);
  71. return httpServer.newHandler(adapter).block();
  72. }
  73. @Bean
  74. public AuthenticationPrincipalArgumentResolver authenticationPrincipalArgumentResolver() {
  75. return new AuthenticationPrincipalArgumentResolver(adapterRegistry);
  76. }
  77. @Bean
  78. WebFilter springSecurityFilterChain(ReactiveAuthenticationManager manager) throws Exception {
  79. HttpSecurity http = http();
  80. http.securityContextRepository(new WebSessionSecurityContextRepository());
  81. http.authenticationManager(manager);
  82. http.httpBasic();
  83. AuthorizeExchangeBuilder authorize = http.authorizeExchange();
  84. authorize.antMatchers("/admin/**").hasRole("ADMIN");
  85. authorize.antMatchers("/users/{user}/**").access(this::currentUserMatchesPath);
  86. authorize.anyExchange().authenticated();
  87. return http.build();
  88. }
  89. private Mono<AuthorizationDecision> currentUserMatchesPath(Mono<Authentication> authentication, AuthorizationContext context) {
  90. return authentication
  91. .map( a -> context.getVariables().get("user").equals(a.getName()))
  92. .map( granted -> new AuthorizationDecision(granted));
  93. }
  94. @Bean
  95. public ReactiveAuthenticationManager authenticationManager(UserRepositoryUserDetailsRepository udr) {
  96. return new UserDetailsRepositoryAuthenticationManager(udr);
  97. }
  98. }