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.vaadin.spring.security.managed.SecurityContextVaadinRequestListener.java

@Override
public void onRequestEnd(VaadinRequest request, VaadinResponse response, VaadinSession session) {
    try {/*from   w w  w  .  j  a va  2s  . c  om*/
        if (session != null) {
            SecurityContext securityContext = SecurityContextHolder.getContext();
            logger.trace("Storing security context {} in VaadinSession {}", securityContext, session);
            session.lock();
            try {
                session.setAttribute(SECURITY_CONTEXT_SESSION_ATTRIBUTE, securityContext);
            } finally {
                session.unlock();
            }
        } else {
            logger.trace("No VaadinSession available for storing the security context");
        }
    } finally {
        logger.trace("Clearing security context");
        SecurityContextHolder.clearContext();
    }
}

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

@Override
public void onRequestEnd(VaadinRequest request, VaadinResponse response, VaadinSession session) {
    try {//from   w w w . j  a  v a  2s. c om
        if (session != null) {
            SecurityContext securityContext = SecurityContextHolder.getContext();
            logger.debug("Storing security context {} in VaadinSession {}", securityContext, session);
            session.lock();
            try {
                session.setAttribute(SECURITY_CONTEXT_SESSION_ATTRIBUTE, securityContext);
            } finally {
                session.unlock();
            }
        } else {
            logger.debug("No VaadinSession available for storing the security context");
        }
    } finally {
        logger.debug("Clearing security context");
        SecurityContextHolder.clearContext();
    }
}

From source file:io.github.autsia.crowly.controllers.DashboardController.java

@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String logout() {
    SecurityContextHolder.clearContext();
    return "redirect:/";
}

From source file:eu.freme.broker.security.AuthenticationFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest httpRequest = asHttp(request);
    HttpServletResponse httpResponse = asHttp(response);

    Optional<String> username = Optional.fromNullable(httpRequest.getHeader("X-Auth-Username"));
    Optional<String> password = Optional.fromNullable(httpRequest.getHeader("X-Auth-Password"));
    Optional<String> token = Optional.fromNullable(httpRequest.getHeader("X-Auth-Token"));

    if (httpRequest.getParameter("token") != null) {
        token = Optional.fromNullable(httpRequest.getParameter("token"));
    }/*from   w  w w.j a  va2 s . co m*/

    String resourcePath = new UrlPathHelper().getPathWithinApplication(httpRequest);

    try {
        if (postToAuthenticate(httpRequest, resourcePath)) {
            logger.debug("Trying to authenticate user {} by X-Auth-Username method", username);
            processUsernamePasswordAuthentication(httpResponse, username, password);
            return;
        }

        if (token.isPresent()) {
            logger.debug("Trying to authenticate user by X-Auth-Token method. Token: {}", token);
            processTokenAuthentication(token);
        }

        logger.debug("AuthenticationFilter is passing request down the filter chain");
        addSessionContextToLogging();
        chain.doFilter(request, response);
    } catch (InternalAuthenticationServiceException internalAuthenticationServiceException) {
        SecurityContextHolder.clearContext();
        logger.error("Internal authentication service exception", internalAuthenticationServiceException);
        httpResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    } catch (AuthenticationException authenticationException) {
        SecurityContextHolder.clearContext();
        httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, authenticationException.getMessage());
    } finally {
        MDC.remove(TOKEN_SESSION_KEY);
        MDC.remove(USER_SESSION_KEY);
    }
}

From source file:eu.freme.common.security.AuthenticationFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest httpRequest = asHttp(request);
    HttpServletResponse httpResponse = asHttp(response);

    Optional<String> username = Optional.fromNullable(httpRequest.getHeader("X-Auth-Username"));
    Optional<String> password = Optional.fromNullable(httpRequest.getHeader("X-Auth-Password"));
    Optional<String> token = Optional.fromNullable(httpRequest.getHeader("X-Auth-Token"));

    if (httpRequest.getParameter("token") != null) {
        token = Optional.fromNullable(httpRequest.getParameter("token"));
    }/* w w  w  .  jav  a  2s. c  o  m*/

    String resourcePath = new UrlPathHelper().getPathWithinApplication(httpRequest);

    try {
        //            if (postToAuthenticate(httpRequest, resourcePath)) {
        //                logger.debug("Trying to authenticate user {} by X-Auth-Username method", username);
        //                processUsernamePasswordAuthentication(httpResponse, username, password);
        //                return;
        //            }

        if (token.isPresent()) {
            logger.debug("Trying to authenticate user by X-Auth-Token method. Token: {}", token);
            processTokenAuthentication(token);
        }

        logger.debug("AuthenticationFilter is passing request down the filter chain");
        addSessionContextToLogging();
        chain.doFilter(request, response);
    } catch (InternalAuthenticationServiceException internalAuthenticationServiceException) {
        SecurityContextHolder.clearContext();
        logger.error("Internal authentication service exception", internalAuthenticationServiceException);
        httpResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    } catch (AuthenticationException authenticationException) {
        SecurityContextHolder.clearContext();
        httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, authenticationException.getMessage());
    } finally {
        MDC.remove(TOKEN_SESSION_KEY);
        MDC.remove(USER_SESSION_KEY);
    }
}

From source file:business.SelectionControllerTests.java

@Test(groups = "request")
public void createRequest() {
    UserAuthenticationToken requester = getRequester();
    SecurityContext securityContext = SecurityContextHolder.getContext();
    securityContext.setAuthentication(requester);

    RequestRepresentation representation = new RequestRepresentation();
    representation = requestController.start(requester, representation);
    log.info("Started request " + representation.getProcessInstanceId());
    log.info("Status: " + representation.getStatus());
    log.info("Assignee: " + representation.getAssignee());
    assertEquals(RequestStatus.OPEN, representation.getStatus());
    processInstanceId = representation.getProcessInstanceId();

    //testController.clearAll();
    //List<RequestListRepresentation> requestList = requestController.getRequestList(requester);
    //assertEquals(0, requestList.size());

    SecurityContextHolder.clearContext();
}

From source file:cn.org.once.cstack.users.UserControllerTestIT.java

@After
public void teardown() {
    logger.info("teardown");

    SecurityContextHolder.clearContext();
    session.invalidate();
}

From source file:org.orcid.api.t2.server.delegator.T2OrcidApiServiceDelegatorTest.java

@After
public void after() {
    SecurityContextHolder.clearContext();
    orcidProfileManager.clearOrcidProfileCache();
}

From source file:cn.org.once.cstack.logs.LogsControllerTestIT.java

@After
public void teardown() throws Exception {
    logger.info("teardown");
    SecurityContextHolder.clearContext();
    session.invalidate();
}