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

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

Introduction

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

Prototype

public static SecurityContext createEmptyContext() 

Source Link

Document

Delegates the creation of a new, empty context to the configured strategy.

Usage

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:org.syncope.core.security.NullSecurityContextRepository.java

@Override
public final SecurityContext loadContext(final HttpRequestResponseHolder requestResponseHolder) {

    return SecurityContextHolder.createEmptyContext();
}

From source file:org.homiefund.test.config.SecurityConextPrincipalFactory.java

@Override
public SecurityContext createSecurityContext(WithUser withUser) {
    SecurityContext context = SecurityContextHolder.createEmptyContext();

    UserDTO user = new UserDTO();
    user.setId(withUser.id());//w  w  w  . j a  va 2s.  c  o m
    user.setHomes(Stream.of(withUser.homes()).map(h -> {
        HomeDTO home = new HomeDTO();
        home.setId(h.id());
        return home;
    }).collect(Collectors.toList()));

    Authentication auth = new UsernamePasswordAuthenticationToken(user, user.getPassword(),
            user.getAuthorities());

    context.setAuthentication(auth);

    return context;
}

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 ww .  ja  va 2s . c o  m
    SecurityContextHolder.setContext(ctx);
    log.debug("Set SecurityContext to user: {}", auth.getPrincipal().toString());
    chain.doFilter(request, response);
}

From source file:com.example.config.WithAccessTokenSecurityContextFactory.java

@Override
public SecurityContext createSecurityContext(final WithAccessToken annotation) {
    final SecurityContext context = SecurityContextHolder.createEmptyContext();
    final GivenAccessToken accessToken = new GivenAccessToken(annotation);
    final OAuth2Authentication oauth2 = accessToken.oauth2();

    final MockingDetails mockingDetails = Mockito.mockingDetails(accessTokenRepository);
    if (mockingDetails.isMock()) {
        when(accessTokenRepository.findByAccessToken(accessToken.accessToken))
                .thenReturn(Optional.of(accessToken.accessTokenEntity()));
    }/*from www  .j  a  v a2  s .  c om*/

    context.setAuthentication(oauth2);
    return context;
}

From source file:de.fau.amos4.test.integration.helper.security.WithMockCustomUserSecurityContextFactory.java

@Override
public SecurityContext createSecurityContext(WithMockCustomUser customUser) {
    SecurityContext context = SecurityContextHolder.createEmptyContext();

    // A empty user that is NOT in the database.
    Client mockClient = createClient(customUser);

    CurrentClient principal = new CurrentClient(mockClient);
    Authentication auth = new UsernamePasswordAuthenticationToken(principal, principal.getPassword(),
            principal.getAuthorities());
    context.setAuthentication(auth);//from   w w  w .j  a va  2  s  . c  o m
    return context;
}

From source file:com.mastercard.test.spring.security.LogPrincipalRuleTests.java

@Test
public void ruleDoesNotBreakWhenUserIsProvided() throws Throwable {
    DefaultStatement statement = new DefaultStatement();
    Description description = Description.createTestDescription(MockWithMockUserTest.class.getName(), "test");

    LogPrincipalRule rule = new LogPrincipalRule();

    Statement actual = rule.apply(statement, description);

    SecurityContextHolder.setContext(SecurityContextHolder.createEmptyContext());
    SecurityContextHolder.getContext()//from   ww w .  jav  a2  s .co m
            .setAuthentication(new UsernamePasswordAuthenticationToken("user", "password"));

    actual.evaluate();

    assertNotSame(statement, actual);
    assertTrue(statement.isEvaluated());
}

From source file:org.openinfinity.core.aspect.MultiTenantAspectIntegrationTest.java

private void injectIdentityBasedSecurityContext() {
    SecurityContext securityContext = SecurityContextHolder.createEmptyContext();
    SecurityContextHolder.setContext(securityContext);
    Identity identity = new Identity();
    Collection<RolePrincipal> rolePrincipals = new ArrayList<RolePrincipal>();
    identity.setAuthenticated(true);//from   w  w  w  . j  av a2  s.c o m
    UserPrincipal userPrincipal = new UserPrincipal("test-name");
    TenantPrincipal<String> tenantPrincipal = new TenantPrincipal<String>(UNIQUE_TENANT_ID);
    RolePrincipal rolePrincipal = new RolePrincipal("test-role");
    rolePrincipals.add(rolePrincipal);
    identity.setUserPrincipal(userPrincipal);
    identity.setRolePrincipals(rolePrincipals);
    identity.setTenantPrincipal(tenantPrincipal);
    SecurityContextHolder.getContext().setAuthentication(identity);
}

From source file:cz.muni.fi.editor.test.service.support.other.TestSecurityContextFactory.java

@Override
@Transactional(readOnly = true)//from   www  .  ja  v a  2 s.c o  m
public SecurityContext createSecurityContext(WithEditorUser withEditorUser) {
    SecurityContext context = SecurityContextHolder.createEmptyContext();

    UserDTO user = new UserDTO();
    user.setId(withEditorUser.id());

    User dao = new User();
    dao.setId(withEditorUser.id());

    List<OrganizationDTO> member = organizationDAO.getOrganizationForUser(dao, true).stream().map(o -> {
        OrganizationDTO dto = new OrganizationDTO();
        dto.setId(o.getId());
        return dto;
    }).collect(Collectors.toList());

    List<OrganizationDTO> owner = organizationDAO.ownedBy(dao).stream().map(o -> {
        OrganizationDTO dto = new OrganizationDTO();
        dto.setId(o.getId());
        return dto;
    }).collect(Collectors.toList());

    user.init(owner, member);

    Authentication auth = new UsernamePasswordAuthenticationToken(user, user.getPassword(),
            user.getAuthorities());

    context.setAuthentication(auth);

    return context;
}

From source file:org.carewebframework.security.spring.DesktopSecurityContextRepository.java

/**
 * Given a servlet request, returns Spring security context object.
 * //from   w ww  . j  av a 2s  .c o m
 * @param request HttpServletRequest
 * @return SecurityContext The Spring security context. First looks for the desktop-based
 *         security context. If not found, then looks for a session-based security context. This
 *         call will convert a session-based security context to desktop-based if a desktop
 *         identifier is found in the request object, a desktop-based security context does not
 *         exist, and a session-based security context does exist.
 * @throws IllegalStateException if session is invalidated
 */
public static SecurityContext getSecurityContext(HttpServletRequest request) {
    final HttpSession session = request.getSession(false);
    boolean ignore = "rmDesktop".equals(request.getParameter("cmd_0"));
    return ignore || session == null ? SecurityContextHolder.createEmptyContext()
            : getSecurityContext(session, request.getParameter("dtid"));
}