UserController.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.http.MediaType;
  18. import org.springframework.security.core.Authentication;
  19. import org.springframework.security.core.userdetails.User;
  20. import org.springframework.security.web.server.context.SecurityContextRepository;
  21. import org.springframework.security.web.server.context.WebSessionSecurityContextRepository;
  22. import org.springframework.stereotype.Component;
  23. import org.springframework.web.reactive.function.server.ServerRequest;
  24. import org.springframework.web.reactive.function.server.ServerResponse;
  25. import reactor.core.publisher.Flux;
  26. import reactor.core.publisher.Mono;
  27. import java.util.Collections;
  28. import java.util.Map;
  29. /**
  30. * @author Rob Winch
  31. * @since 5.0
  32. */
  33. @Component
  34. public class UserController {
  35. private final SecurityContextRepository repo = new WebSessionSecurityContextRepository();
  36. public Mono<ServerResponse> principal(ServerRequest serverRequest) {
  37. return serverRequest.principal().cast(Authentication.class).flatMap(p ->
  38. ServerResponse.ok()
  39. .contentType(MediaType.APPLICATION_JSON)
  40. .syncBody(p.getPrincipal()));
  41. }
  42. public Mono<ServerResponse> admin(ServerRequest serverRequest) {
  43. return serverRequest.principal().cast(Authentication.class).flatMap(p ->
  44. ServerResponse.ok()
  45. .contentType(MediaType.APPLICATION_JSON)
  46. .syncBody( Collections.singletonMap("isadmin", "true")));
  47. }
  48. }