Populate test security context with authentication
Add setAuthentication method on TestSecurityContextHolder.
This commit is contained in:
committed by
Rob Winch
parent
44ee15cb4d
commit
12050404ad
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,6 +17,7 @@ package org.springframework.security.test.context;
|
||||
|
||||
import javax.servlet.FilterChain;
|
||||
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContext;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors;
|
||||
@@ -51,6 +52,7 @@ import org.springframework.util.Assert;
|
||||
* </ul>
|
||||
*
|
||||
* @author Rob Winch
|
||||
* @author Tadaya Tsuyukubo
|
||||
* @since 4.0
|
||||
*
|
||||
*/
|
||||
@@ -94,6 +96,21 @@ public final class TestSecurityContextHolder {
|
||||
SecurityContextHolder.setContext(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link SecurityContext} with the given {@link Authentication}.
|
||||
* The {@link SecurityContext} is set on {@link TestSecurityContextHolder} and
|
||||
* {@link SecurityContextHolder}.
|
||||
*
|
||||
* @param authentication the {@link Authentication} to use
|
||||
* @since 5.1.1
|
||||
*/
|
||||
public static void setAuthentication(Authentication authentication) {
|
||||
Assert.notNull(authentication, "Only non-null Authentication instances are permitted");
|
||||
SecurityContext context = SecurityContextHolder.createEmptyContext();
|
||||
context.setAuthentication(authentication);
|
||||
setContext(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default {@link SecurityContext} by delegating to the
|
||||
* {@link SecurityContextHolder}
|
||||
|
||||
Reference in New Issue
Block a user