UserController.java 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 java.security.Principal;
  18. import java.util.Collections;
  19. import java.util.Map;
  20. import org.springframework.security.core.annotation.AuthenticationPrincipal;
  21. import org.springframework.web.bind.annotation.GetMapping;
  22. import org.springframework.web.bind.annotation.RestController;
  23. import org.springframework.web.server.WebSession;
  24. import reactor.core.publisher.Flux;
  25. import reactor.core.publisher.Mono;
  26. /**
  27. * @author Rob Winch
  28. * @since 5.0
  29. */
  30. @RestController
  31. public class UserController {
  32. private final UserRepository users;
  33. public UserController(UserRepository users) {
  34. this.users = users;
  35. }
  36. @GetMapping("/me")
  37. public Mono<Map<String,String>> me(@AuthenticationPrincipal User user) {
  38. return me(Mono.just(user));
  39. }
  40. @GetMapping("/mono/me")
  41. public Mono<Map<String,String>> me(@AuthenticationPrincipal Mono<User> user) {
  42. return user.flatMap( u -> Mono.just(Collections.singletonMap("username", u.getUsername())));
  43. }
  44. @GetMapping("/mono/session")
  45. public Mono<Map<String,Object>> Session(Mono<WebSession> session) {
  46. return session.flatMap( s -> Mono.just(s.getAttributes()));
  47. }
  48. @GetMapping("/users")
  49. public Flux<User> users() {
  50. return this.users.findAll();
  51. }
  52. @GetMapping("/principal")
  53. public Mono<Map<String,String>> principal(Principal principal) {
  54. return principal(Mono.just(principal));
  55. }
  56. @GetMapping("/mono/principal")
  57. public Mono<Map<String,String>> principal(Mono<Principal> principal) {
  58. return principal.flatMap( p -> Mono.just(Collections.singletonMap("username", p.getName())));
  59. }
  60. @GetMapping("/admin")
  61. public Map<String,String> admin() {
  62. return Collections.singletonMap("isadmin", "true");
  63. }
  64. }