2
0

CurrentUser.java 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Copyright 2002-2013 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.security;
  17. import java.lang.annotation.Documented;
  18. import java.lang.annotation.ElementType;
  19. import java.lang.annotation.Retention;
  20. import java.lang.annotation.RetentionPolicy;
  21. import java.lang.annotation.Target;
  22. import org.springframework.security.core.Authentication;
  23. import org.springframework.security.core.annotation.AuthenticationPrincipal;
  24. import org.springframework.security.core.context.SecurityContextHolder;
  25. /**
  26. * Annotate Spring MVC method arguments with this annotation to indicate you
  27. * wish to specify the argument with the value of the current
  28. * {@link Authentication#getPrincipal()} found on the
  29. * {@link SecurityContextHolder}.
  30. *
  31. * <p>
  32. * Creating your own annotation that uses {@link AuthenticationPrincipal} as a
  33. * meta annotation creates a layer of indirection between your code and Spring
  34. * Security's. For simplicity, you could instead use the
  35. * {@link AuthenticationPrincipal} directly.
  36. * </p>
  37. *
  38. * @author Rob Winch
  39. *
  40. */
  41. @Target(ElementType.PARAMETER)
  42. @Retention(RetentionPolicy.RUNTIME)
  43. @Documented
  44. @AuthenticationPrincipal
  45. public @interface CurrentUser {
  46. }