MockSecurityContextHolderStrategy.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright 2002-2022 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. * https://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 org.springframework.security;
  17. import org.springframework.security.core.Authentication;
  18. import org.springframework.security.core.context.SecurityContext;
  19. import org.springframework.security.core.context.SecurityContextHolderStrategy;
  20. import org.springframework.security.core.context.SecurityContextImpl;
  21. public class MockSecurityContextHolderStrategy implements SecurityContextHolderStrategy {
  22. private SecurityContext mock;
  23. public MockSecurityContextHolderStrategy() {
  24. }
  25. public MockSecurityContextHolderStrategy(Authentication authentication) {
  26. this.mock = new SecurityContextImpl(authentication);
  27. }
  28. @Override
  29. public void clearContext() {
  30. this.mock = null;
  31. }
  32. @Override
  33. public SecurityContext getContext() {
  34. return this.mock;
  35. }
  36. @Override
  37. public void setContext(SecurityContext context) {
  38. this.mock = context;
  39. }
  40. @Override
  41. public SecurityContext createEmptyContext() {
  42. return new SecurityContextImpl();
  43. }
  44. }