/** * Establish a {@link SecurityContext} that has a * {@link UsernamePasswordAuthenticationToken} for the * {@link Authentication#getPrincipal()} and a custom {@link UserDetails} for the * {@link UsernamePasswordAuthenticationToken#getPrincipal()}. All details are * declarative and do not require that the user actually exists. * * <p> * The support works by associating the user to the HttpServletRequest. To associate * the request to the SecurityContextHolder you need to ensure that the * SecurityContextPersistenceFilter is associated with the MockMvc instance. A few * ways to do this are: * </p> * * <ul> * <li>Invoking apply {@link SecurityMockMvcConfigurers#springSecurity()}</li> * <li>Adding Spring Security's FilterChainProxy to MockMvc</li> * <li>Manually adding {@link SecurityContextPersistenceFilter} to the MockMvc * instance may make sense when using MockMvcBuilders standaloneSetup</li> * </ul> * * @param user the UserDetails to populate * @return the {@link RequestPostProcessor} to use */ public static RequestPostProcessor user(UserDetails user) { return new UserDetailsRequestPostProcessor(user); }
@Override public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) { UserDetailsRequestPostProcessor delegate = new UserDetailsRequestPostProcessor( createUser()); return delegate.postProcessRequest(request); }
/** * Establish a {@link SecurityContext} that has a * {@link UsernamePasswordAuthenticationToken} for the * {@link Authentication#getPrincipal()} and a custom {@link UserDetails} for the * {@link UsernamePasswordAuthenticationToken#getPrincipal()}. All details are * declarative and do not require that the user actually exists. * * <p> * The support works by associating the user to the HttpServletRequest. To associate * the request to the SecurityContextHolder you need to ensure that the * SecurityContextPersistenceFilter is associated with the MockMvc instance. A few * ways to do this are: * </p> * * <ul> * <li>Invoking apply {@link SecurityMockMvcConfigurers#springSecurity()}</li> * <li>Adding Spring Security's FilterChainProxy to MockMvc</li> * <li>Manually adding {@link SecurityContextPersistenceFilter} to the MockMvc * instance may make sense when using MockMvcBuilders standaloneSetup</li> * </ul> * * @param user the UserDetails to populate * @return the {@link RequestPostProcessor} to use */ public static RequestPostProcessor user(UserDetails user) { return new UserDetailsRequestPostProcessor(user); }
@Override public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) { UserDetailsRequestPostProcessor delegate = new UserDetailsRequestPostProcessor( createUser()); return delegate.postProcessRequest(request); }