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:com.linuxbox.enkive.message.search.AsynchronousSearchThread.java

@Override
public SearchResult call() {

    SearchResult searchResult = null;
    try {//from   w  w  w  .  j  a  va 2 s  . co m
        SecurityContext ctx = new SecurityContextImpl();
        ctx.setAuthentication(searchingUserAuth);
        SecurityContextHolder.setContext(ctx);
        searchResult = searchResultBuilder.getSearchResult(searchResultId);
        try {
            markSearchResultRunning(searchResult);
            SearchResult tmpSearchResult = messageSearchService.search(fields);
            searchResult.setMessageIds(tmpSearchResult.getMessageIds());
            searchResult.setTimestamp(tmpSearchResult.getTimestamp());
            searchResult.setExecutedBy(tmpSearchResult.getExecutedBy());
            searchResult.setStatus(Status.COMPLETE);
            searchResult.saveSearchResult();

        } catch (MessageSearchException e) {
            searchResult.setStatus(Status.UNKNOWN);
            searchResult.saveSearchResult();
            LOGGER.error("Could not complete message search", e);
        }
    } catch (WorkspaceException e) {
        LOGGER.error("Could not complete message search", e);
    } finally {
        SecurityContextHolder.clearContext();
    }

    return searchResult;
}

From source file:org.openmrs.contrib.metadatarepository.service.impl.UserSecurityAdviceTest.java

@After
public void tearDown() {
    SecurityContextHolder.setContext(initialSecurityContext);
}

From source file:org.duracloud.account.app.controller.AmaTestBase.java

protected void intializeAuthManager() {
    Authentication auth = createMock(Authentication.class);

    EasyMock.expect(auth.getName()).andReturn(TEST_USERNAME).anyTimes();
    authenticationManager = createMock(AuthenticationManager.class);

    SecurityContext ctx = new SecurityContextImpl();
    ctx.setAuthentication(auth);//from   w w  w  .j a v  a 2s . c o  m
    EasyMock.expect(auth.getPrincipal()).andReturn(createUser()).anyTimes();
    SecurityContextHolder.setContext(ctx);
}

From source file:test.pl.chilldev.facelets.taglib.spring.security.AuthenticationTagTest.java

@Test(expected = FacesException.class)
public void applyInvalidProperty() throws FacesException {
    String property = "foo";
    String var = "bar";

    Map<String, Object> config = new HashMap<>();
    config.put(AuthenticationTag.ATTRIBUTE_PROPERTY, property);
    config.put(AuthenticationTag.ATTRIBUTE_VAR, var);

    AuthenticationTag tag = new AuthenticationTag(MockTagConfig.factory(config));

    // set up context
    FaceletContext context = new MockFaceletContext();
    SecurityContextHolder.setContext(this.securityContext);

    when(this.securityContext.getAuthentication()).thenReturn(this.auth);

    // run the tag
    tag.apply(context, this.parent);
}

From source file:org.terasoluna.gfw.security.web.logging.UserIdMDCPutFilterTest.java

@Test
public void testGetMDCValue() {
    UserIdMDCPutFilter mdcPutFilter = new UserIdMDCPutFilter();

    // expected data
    String userName = "terasoluna@nttd.co.jp";

    // SecurityContextHolder setting start
    securityContext = mock(SecurityContext.class);
    authentication = mock(Authentication.class);
    user = new User(userName, "yyyy", Arrays.asList(new SimpleGrantedAuthority("user")));
    when(authentication.getPrincipal()).thenReturn(user);
    when(securityContext.getAuthentication()).thenReturn(authentication);
    SecurityContextHolder.setContext(securityContext);
    // setting end

    // run/*  w w w.  j a  v a  2  s .  c  o  m*/
    String mdcValueStr = mdcPutFilter.getMDCValue(request, response);

    // assert
    assertThat(mdcValueStr, is(userName));

}

From source file:org.encos.flydown.test.ContextTest.java

@Test(expected = RateExceededException.class, timeout = DEFAULT_TIMEOUT)
public void testPrincipal() throws Exception {
    DefaultSecurityContext securityContext = new DefaultSecurityContext(
            new DefaultAuthentication(new DefaultPrincipal("enrico")));
    SecurityContextHolder.setContext(securityContext);

    for (int i = 0; i < requestLimit + 1; i++) {
        principalService.principalDoSomething();
        assertNotEquals(i, requestLimit + 1);

    }//from   www . ja  v  a 2 s  . c  o m
}

From source file:org.jasig.schedassist.web.register.delegate.DelegateRegistrationHelperTest.java

@Test
public void testCurrentDelegateIsIneligibleTrue() {
    MockCalendarAccount ownerAccount = new MockCalendarAccount();
    MockScheduleOwner owner = new MockScheduleOwner(ownerAccount, 1L);
    IDelegateCalendarAccount delegate = mock(IDelegateCalendarAccount.class);
    when(delegate.isEligible()).thenReturn(false);
    DelegateCalendarAccountUserDetailsImpl details = new DelegateCalendarAccountUserDetailsImpl(delegate,
            owner);/*w ww.j av a2  s. c  om*/

    SecurityContext context = new SecurityContextImpl();

    context.setAuthentication(new UsernamePasswordAuthenticationToken(details, ""));

    SecurityContextHolder.setContext(context);
    DelegateRegistrationHelper helper = new DelegateRegistrationHelper();
    Assert.assertTrue(helper.currentDelegateIsIneligible());

}

From source file:nl.ctrlaltdev.harbinger.evidence.EvidenceCollectorTest.java

@Test
public void shouldEnhanceWithUser() {
    SecurityContextHolder.setContext(new SecurityContextImpl());
    SecurityContextHolder.getContext().setAuthentication(new AnonymousAuthenticationToken("key", "user",
            Collections.singletonList(new SimpleGrantedAuthority("x"))));
    evidence = collector.enhanceAndStore(evidence);
    assertEquals("user", evidence.getUser());
}

From source file:com.vdenotaris.spring.boot.security.saml.web.CommonTestSupport.java

public MockHttpSession mockAnonymousHttpSession() {
    MockHttpSession mockSession = new MockHttpSession();
    SecurityContext mockSecurityContext = mock(SecurityContext.class);

    AnonymousAuthenticationToken principal = new AnonymousAuthenticationToken(ANONYMOUS_USER_KEY,
            ANONYMOUS_USER_PRINCIPAL, AUTHORITIES);

    when(mockSecurityContext.getAuthentication()).thenReturn(principal);

    SecurityContextHolder.setContext(mockSecurityContext);
    mockSession.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
            mockSecurityContext);/*from  w w w  .ja va 2  s.  c  o  m*/

    return mockSession;
}

From source file:org.ng200.openolympus.TestUtilities.java

public void logInAsAdmin() {
    final SecurityContext context = SecurityContextHolder.createEmptyContext();

    final User principal = this.userService.getUserByUsername("admin");
    final Authentication auth = new UsernamePasswordAuthenticationToken(principal, "admin",
            principal.getAuthorities());
    context.setAuthentication(auth);/*  ww w. j  a va 2  s  . co m*/
    SecurityContextHolder.setContext(context);
}