Example usage for org.springframework.security.core.context SecurityContextHolder setContext

List of usage examples for org.springframework.security.core.context SecurityContextHolder setContext

Introduction

In this page you can find the example usage for org.springframework.security.core.context SecurityContextHolder setContext.

Prototype

public static void setContext(SecurityContext context) 

Source Link

Document

Associates a new SecurityContext with the current thread of execution.

Usage

From source file:io.pivotal.cla.security.Login.java

public static Authentication loginAs(User user) {
    SecurityContext context = SecurityContextHolder.createEmptyContext();
    UserAuthentication authentication = new UserAuthentication(user);
    context.setAuthentication(authentication);
    SecurityContextHolder.setContext(context);
    return authentication;
}

From source file:com.github.persapiens.jsfboot.security.SpringSecurityMock.java

public void init(Authentication authentication) {
    // Mockito.whens() for your authorization object
    SecurityContext securityContext = Mockito.mock(SecurityContext.class);
    Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
    SecurityContextHolder.setContext(securityContext);
}

From source file:com.application.utils.RequestHolderApplicationServlet.java

@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    SecurityContextHolder.setContext(SecurityContextHolder.createEmptyContext());

    RequestHolder.setRequest(request);/* w w w.  j  a v  a 2  s. c o m*/

    super.service(request, response);

    // We remove the request from the thread local, there's no reason to keep it once the work is done
    RequestHolder.clean();

    SecurityContextHolder.clearContext();
}

From source file:de.zib.gndms.kit.security.SpringSecurityContextInstaller.java

@Override
public void installSecurityContext(final GNDMSSecurityContextHolder contextHolder) {

    if (contextHolder == null || !contextHolder.hasContext())
        return;/*  www .  j  a  v a2 s  . c  o  m*/

    if (SpringSecurityContextHolder.class.isInstance(contextHolder)) {
        SpringSecurityContextHolder holder = SpringSecurityContextHolder.class.cast(contextHolder);

        SecurityContextHolder.setContext(holder.getSecurityContext());
    }
}

From source file:com.apress.prospringintegration.security.SecurityMain.java

public static void login(String username, String password, String... roles) {
    SecurityContext context = createContext(username, password, roles);
    SecurityContextHolder.setContext(context);
}

From source file:org.jtalks.common.security.SecurityContextFacade.java

/**
 * Set {@code SecurityContext} to  {@code SecurityContextHolder}
 *
 * @param securityContext {@code SecurityContext} to set.
 *//*w w w . jav a  2 s  .com*/
public void setContext(SecurityContext securityContext) {
    SecurityContextHolder.setContext(securityContext);
}

From source file:cz.muni.fi.mir.wrappers.SecurityContextHolderFacade.java

@Override
public void setContext(SecurityContext securityContext) {
    SecurityContextHolder.setContext(securityContext);
}

From source file:org.apigw.appmanagement.revision.ApplicationManagementRevisionListenerTest.java

@Test
public void testNewRevisionByAdmin() throws Exception {
    SecurityContextHolder.setContext(createSecurityContext("userA", "ROLE_USER", "ROLE_ADMIN"));
    ApplicationManagementRevisionListener revisionListener = new ApplicationManagementRevisionListener();
    ApplicationManagementRevision revision = new ApplicationManagementRevision();
    assertFalse(revision.isEditorAdmin());
    assertNull(revision.getEditor());/*from   w  w  w.j  av a 2s  .  c  o  m*/
    revisionListener.newRevision(revision);
    assertTrue(revision.isEditorAdmin());
    assertEquals("userA", revision.getEditor());
}

From source file:fi.helsinki.opintoni.security.SecurityUtilsTest.java

@Test
public void testGetCurrentLogin() {
    SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
    securityContext.setAuthentication(new UsernamePasswordAuthenticationToken("admin", "admin"));
    SecurityContextHolder.setContext(securityContext);
    String login = securityUtils.getCurrentLogin();
    assertThat(login).isEqualTo("admin");
}

From source file:com.netflix.spinnaker.fiat.shared.FiatAuthenticationFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    Authentication auth = AuthenticatedRequest.getSpinnakerUser()
            .map(username -> (Authentication) new PreAuthenticatedAuthenticationToken(username, null,
                    new ArrayList<>()))
            .orElseGet(() -> new AnonymousAuthenticationToken("anonymous", "anonymous",
                    AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS")));

    val ctx = SecurityContextHolder.createEmptyContext();
    ctx.setAuthentication(auth);//from w  w  w .  j ava  2 s  .c  o  m
    SecurityContextHolder.setContext(ctx);
    log.debug("Set SecurityContext to user: {}", auth.getPrincipal().toString());
    chain.doFilter(request, response);
}