private boolean isAuthenticated() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null || authentication instanceof AnonymousAuthenticationToken) { return false; } return true; }
protected void resetPreviousAuthentication(Authentication previousAuthentication) { SecurityContextHolder.getContext().setAuthentication(previousAuthentication); }
/** * Explicitly clears the context value from the current thread. */ public static void clearContext() { strategy.clearContext(); }
/** * Creates a new {@link DelegatingSecurityContextCallable} with the * {@link SecurityContext} from the {@link SecurityContextHolder}. * @param delegate the delegate {@link Callable} to run under the current * {@link SecurityContext}. Cannot be null. */ public DelegatingSecurityContextCallable(Callable<V> delegate) { this(delegate, SecurityContextHolder.getContext()); }
Authentication authentication = Mockito.mock(Authentication.class); // Mockito.whens() for your authorization object SecurityContext securityContext = Mockito.mock(SecurityContext.class); Mockito.when(securityContext.getAuthentication()).thenReturn(authentication); SecurityContextHolder.setContext(securityContext);
/** * A shortcut for {@link #withSecurityContext(Mono)} * @param authentication the {@link Authentication} to be used * @return a Reactor {@link Context} that contains the {@code Mono<SecurityContext>} */ public static Context withAuthentication(Authentication authentication) { return withSecurityContext(Mono.just(new SecurityContextImpl(authentication))); } }
public SecurityContext getContext() { if (contextHolder == null) { contextHolder = new SecurityContextImpl(); } return contextHolder; }
/** * Obtain the current <code>SecurityContext</code>. * * @return the security context (never <code>null</code>) */ public static SecurityContext getContext() { return strategy.getContext(); }
/** * Changes the preferred strategy. Do <em>NOT</em> call this method more than once for * a given JVM, as it will re-initialize the strategy and adversely affect any * existing threads using the old strategy. * * @param strategyName the fully qualified class name of the strategy that should be * used. */ public static void setStrategyName(String strategyName) { SecurityContextHolder.strategyName = strategyName; initialize(); }
/** * Delegates the creation of a new, empty context to the configured strategy. */ public static SecurityContext createEmptyContext() { return strategy.createEmptyContext(); }
/** * Associates a new <code>SecurityContext</code> with the current thread of execution. * * @param context the new <code>SecurityContext</code> (may not be <code>null</code>) */ public static void setContext(SecurityContext context) { strategy.setContext(context); }
private Authentication getAuthentication() { if (this.authentication != null) { return this.authentication; } SecurityContext context = SecurityContextHolder.getContext(); return context.getAuthentication(); } }
private void setAuthenticationPrincipal(Object principal) { this.expectedPrincipal = principal; SecurityContextHolder.getContext() .setAuthentication( new TestingAuthenticationToken(expectedPrincipal, "password", "ROLE_USER")); } }
/** * Creates a new {@link DelegatingSecurityContextRunnable} with the * {@link SecurityContext} from the {@link SecurityContextHolder}. * @param delegate the delegate {@link Runnable} to run under the current * {@link SecurityContext}. Cannot be null. */ public DelegatingSecurityContextRunnable(Runnable delegate) { this(delegate, SecurityContextHolder.getContext()); }
private void populateDefaultAuthentication(Map<String, Object> attrs) { if (attrs.containsKey(AUTHENTICATION_ATTR_NAME)) { return; } Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); attrs.putIfAbsent(AUTHENTICATION_ATTR_NAME, authentication); }
private void setup(String role) { TestingAuthenticationToken user = new TestingAuthenticationToken("user", "password", role); SecurityContextHolder.getContext().setAuthentication(user); } }
private Authentication getUserAuthentication() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null) { return null; } if (authentication instanceof OAuth2Authentication) { OAuth2Authentication oauth = (OAuth2Authentication) authentication; return oauth.getUserAuthentication(); } return authentication; }
@Override public UmsMember getCurrentMember() { SecurityContext ctx = SecurityContextHolder.getContext(); Authentication auth = ctx.getAuthentication(); MemberDetails memberDetails = (MemberDetails) auth.getPrincipal(); return memberDetails.getUmsMember(); }
@Override public Authentication runAs() { return SecurityContextHolder.getContext().getAuthentication(); }
private String principal() { if ( SecurityContextHolder.getContext().getAuthentication() != null ) { return SecurityContextHolder.getContext().getAuthentication().getName(); } return null; } }