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

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

Introduction

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

Prototype

public static void clearContext() 

Source Link

Document

Explicitly clears the context value from the current thread.

Usage

From source file:org.chance.SampleSecureApplicationTests.java

@After
public void close() {
    SecurityContextHolder.clearContext();
}

From source file:org.unidle.social.SignInAdapterImplTest.java

@Before
public void setUp() throws Exception {
    SecurityContextHolder.clearContext();
}

From source file:org.openengsb.opencit.core.projectmanager.internal.CITTask.java

private void authenticate() {
    SecurityContextHolder.clearContext();
    Authentication token = authenticationManager
            .authenticate(new BundleAuthenticationToken("opencit-core-projectmanager", ""));
    SecurityContextHolder.getContext().setAuthentication(token);
}

From source file:com.greglturnquist.payroll.DatabaseLoader.java

@Override
public void run(String... strings) throws Exception {

    Manager greg = this.managers.save(new Manager("greg", "turnquist", "ROLE_MANAGER"));
    Manager oliver = this.managers.save(new Manager("oliver", "gierke", "ROLE_MANAGER"));

    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken("greg",
            "doesn't matter", AuthorityUtils.createAuthorityList("ROLE_MANAGER")));

    this.employees.save(new Employee("Frodo", "Baggins", "ring bearer", greg));
    this.employees.save(new Employee("Bilbo", "Baggins", "burglar", greg));
    this.employees.save(new Employee("Gandalf", "the Grey", "wizard", greg));

    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken("oliver",
            "doesn't matter", AuthorityUtils.createAuthorityList("ROLE_MANAGER")));

    this.employees.save(new Employee("Samwise", "Gamgee", "gardener", oliver));
    this.employees.save(new Employee("Merry", "Brandybuck", "pony rider", oliver));
    this.employees.save(new Employee("Peregrin", "Took", "pipe smoker", oliver));

    SecurityContextHolder.clearContext();
}

From source file:example.company.UrlLevelSecurityTests.java

@Before
public void setUp() {

    this.mvc = webAppContextSetup(context).addFilters(filterChain).build();

    SecurityContextHolder.clearContext();
}

From source file:org.cloudfoundry.identity.uaa.oauth.event.ClientAdminEventPublisherTests.java

@After
public void destroy() {
    SecurityContextHolder.clearContext();
}

From source file:org.cloudfoundry.identity.uaa.authentication.manager.LoginAuthenticationManagerTests.java

@After
public void clean() {
    SecurityContextHolder.clearContext();
}

From source file:example.springdata.rest.security.Application.java

/**
 * Pre-load the system with employees and items.
 *//* www.j ava  2s .  c om*/
public @PostConstruct void init() {

    employeeRepository.save(new Employee("Bilbo", "Baggins", "thief"));
    employeeRepository.save(new Employee("Frodo", "Baggins", "ring bearer"));
    employeeRepository.save(new Employee("Gandalf", "the Wizard", "servant of the Secret Fire"));

    /**
     * Due to method-level protections on {@link example.company.ItemRepository}, the security context must be loaded
     * with an authentication token containing the necessary privileges.
     */
    SecurityUtils.runAs("system", "system", "ROLE_ADMIN");

    itemRepository.save(new Item("Sting"));
    itemRepository.save(new Item("the one ring"));

    SecurityContextHolder.clearContext();
}

From source file:org.vaadin.spring.security.managed.SecurityContextVaadinRequestListener.java

@Override
public void onRequestStart(VaadinRequest request, VaadinResponse response) {
    final WrappedSession wrappedSession = request.getWrappedSession(false);
    VaadinSession session = null;/*from  ww  w .  j  a va  2  s. c  om*/
    if (wrappedSession != null) {
        session = VaadinSession.getForSession(request.getService(), wrappedSession);
    }

    SecurityContextHolder.clearContext();
    if (session != null) {
        logger.trace("Loading security context from VaadinSession {}", session);
        SecurityContext securityContext;
        session.lock();
        try {
            securityContext = (SecurityContext) session.getAttribute(SECURITY_CONTEXT_SESSION_ATTRIBUTE);
        } finally {
            session.unlock();
        }
        if (securityContext == null) {
            logger.trace("No security context found in VaadinSession {}", session);
        } else {
            logger.trace("Setting security context to {}", securityContext);
            SecurityContextHolder.setContext(securityContext);
        }
    } else {
        logger.trace("No VaadinSession available for retrieving the security context");
    }
}

From source file:org.vaadin.spring.security.internal.SecurityContextVaadinRequestListener.java

@Override
public void onRequestStart(VaadinRequest request, VaadinResponse response) {
    final WrappedSession wrappedSession = request.getWrappedSession(false);
    VaadinSession session = null;/*  w w  w  .  ja va 2 s. co  m*/
    if (wrappedSession != null) {
        session = VaadinSession.getForSession(request.getService(), wrappedSession);
    }

    SecurityContextHolder.clearContext();
    if (session != null) {
        logger.debug("Loading security context from VaadinSession {}", session);
        SecurityContext securityContext;
        session.lock();
        try {
            securityContext = (SecurityContext) session.getAttribute(SECURITY_CONTEXT_SESSION_ATTRIBUTE);
        } finally {
            session.unlock();
        }
        if (securityContext == null) {
            logger.debug("No security context found in VaadinSession {}", session);
        } else {
            logger.debug("Setting security context to {}", securityContext);
            SecurityContextHolder.setContext(securityContext);
        }
    } else {
        logger.debug("No VaadinSession available for retrieving the security context");
    }
}