UserController.java 2.1 KB

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