當前位置: 首頁>>代碼示例>>Java>>正文


Java SecurityContextHolder.clearContext方法代碼示例

本文整理匯總了Java中org.springframework.security.core.context.SecurityContextHolder.clearContext方法的典型用法代碼示例。如果您正苦於以下問題:Java SecurityContextHolder.clearContext方法的具體用法?Java SecurityContextHolder.clearContext怎麽用?Java SecurityContextHolder.clearContext使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.springframework.security.core.context.SecurityContextHolder的用法示例。


在下文中一共展示了SecurityContextHolder.clearContext方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: handleRedirectAnonymous

import org.springframework.security.core.context.SecurityContextHolder; //導入方法依賴的package包/類
@Test
public void handleRedirectAnonymous() throws URISyntaxException {
	SecurityContextHolder.clearContext();

	final SystemUserSetting setting = new SystemUserSetting();
	setting.setLogin(DEFAULT_USER);
	setting.setName(RedirectResource.PREFERRED_HASH);
	setting.setValue("hash");
	userSettingRepository.save(setting);
	final SystemUserSetting setting2 = new SystemUserSetting();
	setting2.setLogin(DEFAULT_USER);
	setting2.setName(RedirectResource.PREFERRED_URL);
	setting2.setValue("http://localhost:1/any");
	userSettingRepository.save(setting2);
	em.flush();
	em.clear();

	final Response response = resource.handleRedirect(DEFAULT_USER + "|hash");
	Assert.assertNull(response.getCookies().get(RedirectResource.PREFERRED_COOKIE_HASH));
	Assert.assertEquals("http://localhost:1/any", response.getHeaderString("location"));
}
 
開發者ID:ligoj,項目名稱:plugin-redirect,代碼行數:22,代碼來源:RedirectResourceTest.java

示例2: doFilterInternal

import org.springframework.security.core.context.SecurityContextHolder; //導入方法依賴的package包/類
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                                FilterChain filterChain) throws ServletException, IOException {

    String authorizationHeader = request.getHeader(HttpHeaders.AUTHORIZATION);
    if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {

        try {

            String authenticationToken = authorizationHeader.substring(7);
            Authentication authenticationRequest = new JwtAuthenticationToken(authenticationToken);
            Authentication authenticationResult = authenticationManager.authenticate(authenticationRequest);

            SecurityContext context = SecurityContextHolder.createEmptyContext();
            context.setAuthentication(authenticationResult);
            SecurityContextHolder.setContext(context);

        } catch (AuthenticationException e) {
            SecurityContextHolder.clearContext();
            authenticationEntryPoint.commence(request, response, e);
            return;
        }
    }

    filterChain.doFilter(request, response);
}
 
開發者ID:cassiomolin,項目名稱:jersey-jwt-springsecurity,代碼行數:27,代碼來源:JwtAuthenticationTokenFilter.java

示例3: handleRedirectAnonymousNoCookie

import org.springframework.security.core.context.SecurityContextHolder; //導入方法依賴的package包/類
@Test
public void handleRedirectAnonymousNoCookie() throws URISyntaxException {
	SecurityContextHolder.clearContext();

	final Response response = resource.handleRedirect(null);
	Assert.assertNull(response.getCookies().get(RedirectResource.PREFERRED_COOKIE_HASH));
	Assert.assertEquals("http://localhost:8081/external", response.getHeaderString("location"));
}
 
開發者ID:ligoj,項目名稱:plugin-redirect,代碼行數:9,代碼來源:RedirectResourceTest.java

示例4: unsuccessfulAuthentication

import org.springframework.security.core.context.SecurityContextHolder; //導入方法依賴的package包/類
@Override
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException, ServletException {
    this.logger.debug("Authentication request failed: " + failed.toString(), failed);

    SecurityContextHolder.clearContext();
    this.logger.debug("Updated SecurityContextHolder to contain null Authentication");

    // populate the response
    if (StringUtils.isNotBlank(request.getHeader(ProxiedEntitiesUtils.PROXY_ENTITIES_CHAIN))) {
        response.setHeader(ProxiedEntitiesUtils.PROXY_ENTITIES_DETAILS, failed.getMessage());
    }

    // set the response status
    response.setContentType("text/plain");

    // write the response message
    PrintWriter out = response.getWriter();

    // use the type of authentication exception to determine the response code
    if (failed instanceof InvalidAuthenticationException) {
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        out.println(failed.getMessage());
    } else if (failed instanceof UntrustedProxyException) { // thrown in X509IdentityProviderAuthenticationProvider
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        out.println(failed.getMessage());
    } else if (failed instanceof AuthenticationServiceException) {
        logger.error(String.format("Unable to authorize: %s", failed.getMessage()), failed);
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        out.println(String.format("Unable to authorize: %s", failed.getMessage()));
    } else {
        logger.error(String.format("Unable to authorize: %s", failed.getMessage()), failed);
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        out.println("Access is denied.");
    }

    // log the failure
    logger.warn(String.format("Rejecting access to web api: %s", failed.getMessage()));
    logger.debug(StringUtils.EMPTY, failed);
}
 
開發者ID:apache,項目名稱:nifi-registry,代碼行數:40,代碼來源:IdentityAuthenticationFilter.java

示例5: handleRedirectAnonymousCookieNoSetting

import org.springframework.security.core.context.SecurityContextHolder; //導入方法依賴的package包/類
@Test
public void handleRedirectAnonymousCookieNoSetting() throws URISyntaxException {
	SecurityContextHolder.clearContext();

	final Response response = resource.handleRedirect(DEFAULT_USER + "|hash");
	Assert.assertNull(response.getCookies().get(RedirectResource.PREFERRED_COOKIE_HASH));
	Assert.assertEquals("http://localhost:8081/external", response.getHeaderString("location"));
}
 
開發者ID:ligoj,項目名稱:plugin-redirect,代碼行數:9,代碼來源:RedirectResourceTest.java

示例6: interceptCall

import org.springframework.security.core.context.SecurityContextHolder; //導入方法依賴的package包/類
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
        ServerCall<ReqT, RespT> call,
        Metadata headers,
        ServerCallHandler<ReqT, RespT> next) {
    String authHeader = nullToEmpty(headers.get(Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER)));
    if (!(authHeader.startsWith("Bearer ") || authHeader.startsWith("bearer "))) {
        return next.startCall(call, headers);
    }

    try {
        String token = authHeader.substring(7);

        log.debug("Bearer Token Authorization header found");

        if (authenticationIsRequired()) {
            Authentication authResult = tokenServices.loadAuthentication(token);

            log.debug("Authentication success: {}", authResult);

            SecurityContextHolder.getContext().setAuthentication(authResult);
        }
    } catch (AuthenticationException | OAuth2Exception e) {
        SecurityContextHolder.clearContext();

        log.debug("Authentication request failed: {}", e.getMessage());

        throw Status.UNAUTHENTICATED.withDescription(e.getMessage()).withCause(e).asRuntimeException();
    }

    return next.startCall(call, headers);
}
 
開發者ID:revinate,項目名稱:grpc-spring-security-demo,代碼行數:33,代碼來源:Oauth2AuthenticationInterceptor.java

示例7: teardown

import org.springframework.security.core.context.SecurityContextHolder; //導入方法依賴的package包/類
@After
public void teardown() throws Exception {
    logger.info("teardown");

    logger.info("Delete application : " + applicationName);

    mockMvc.perform(delete("/application/" + applicationName)
            .session(session)
            .contentType(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk());

    SecurityContextHolder.clearContext();
    session.invalidate();
}
 
開發者ID:oncecloud,項目名稱:devops-cstack,代碼行數:15,代碼來源:AbstractModuleControllerTestIT.java

示例8: teardown

import org.springframework.security.core.context.SecurityContextHolder; //導入方法依賴的package包/類
@After
public void teardown() {
    logger.info("teardown");

    SecurityContextHolder.clearContext();
    session.invalidate();
}
 
開發者ID:oncecloud,項目名稱:devops-cstack,代碼行數:8,代碼來源:UserControllerTestIT.java

示例9: teardown

import org.springframework.security.core.context.SecurityContextHolder; //導入方法依賴的package包/類
@After
public void teardown() throws Exception {
    logger.info("teardown");

    ResultActions resultats =
            mockMvc.perform(delete("/application/" + applicationName).session(session).contentType(MediaType.APPLICATION_JSON));
    resultats.andExpect(status().isOk());

    SecurityContextHolder.clearContext();
    session.invalidate();
}
 
開發者ID:oncecloud,項目名稱:devops-cstack,代碼行數:12,代碼來源:Apache2ControllerTestIT.java

示例10: logoutSuccess

import org.springframework.security.core.context.SecurityContextHolder; //導入方法依賴的package包/類
@GetMapping(value = "/logoutSuccess")
public String logoutSuccess() {
    HttpSession session = SecurityUtil.getSession();
    if (session != null) {
        session.invalidate();
    }
    if (SecurityUtil.getCurrUserDetail() != null) {
        SecurityContextHolder.clearContext();
    }
    logger.debug("用戶成功退出登錄!");
    return "login";
}
 
開發者ID:DomKing,項目名稱:springbootWeb,代碼行數:13,代碼來源:SecurityController.java

示例11: unsuccessfulAuthentication

import org.springframework.security.core.context.SecurityContextHolder; //導入方法依賴的package包/類
@Override
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException failed) throws IOException, ServletException {
    SecurityContextHolder.clearContext();
    failureHandler.onAuthenticationFailure(request, response, failed);
}
 
開發者ID:Apereo-Learning-Analytics-Initiative,項目名稱:OpenLRW,代碼行數:7,代碼來源:JwtTokenAuthenticationProcessingFilter.java

示例12: saveOrUpdate

import org.springframework.security.core.context.SecurityContextHolder; //導入方法依賴的package包/類
@Test
public void saveOrUpdate() throws URISyntaxException {
	Response response = resource.saveOrUpdate("http://localhost:1/any");
	final String cookieValue = response.getCookies().get(RedirectResource.PREFERRED_COOKIE_HASH).getValue();
	Assert.assertTrue(cookieValue.startsWith(DEFAULT_USER + "|"));
	Assert.assertNull(response.getHeaderString("location"));

	response = resource.handleRedirect(null);
	Assert.assertEquals(cookieValue, response.getCookies().get(RedirectResource.PREFERRED_COOKIE_HASH).getValue());
	Assert.assertEquals("http://localhost:1/any", response.getHeaderString("location"));

	// Logout
	SecurityContextHolder.clearContext();
	response = resource.handleRedirect(cookieValue);
	Assert.assertNull(response.getCookies().get(RedirectResource.PREFERRED_COOKIE_HASH));
	Assert.assertEquals("http://localhost:1/any", response.getHeaderString("location"));

	// Change URL
	userSettingRepository.findByLoginAndName(DEFAULT_USER, RedirectResource.PREFERRED_URL)
			.setValue("http://localhost:2/any");
	em.flush();
	em.clear();

	response = resource.handleRedirect(cookieValue);
	Assert.assertNull(response.getCookies().get(RedirectResource.PREFERRED_COOKIE_HASH));
	Assert.assertEquals("http://localhost:2/any", response.getHeaderString("location"));

	// Change hash
	userSettingRepository.findByLoginAndName(DEFAULT_USER, RedirectResource.PREFERRED_HASH).setValue("new-hash");
	userSettingRepository.findByLoginAndName(DEFAULT_USER, RedirectResource.PREFERRED_URL)
			.setValue("http://localhost:2/any");
	em.flush();
	em.clear();
	response = resource.handleRedirect(cookieValue);
	Assert.assertNull(response.getCookies().get(RedirectResource.PREFERRED_COOKIE_HASH));
	Assert.assertEquals("http://localhost:8081/external", response.getHeaderString("location"));

	// Login
	initSpringSecurityContext(DEFAULT_USER);
	response = resource.handleRedirect(null);
	Assert.assertEquals(DEFAULT_USER + "|new-hash",
			response.getCookies().get(RedirectResource.PREFERRED_COOKIE_HASH).getValue());
	Assert.assertEquals("http://localhost:2/any", response.getHeaderString("location"));
}
 
開發者ID:ligoj,項目名稱:plugin-redirect,代碼行數:45,代碼來源:RedirectResourceTest.java

示例13: tearDown

import org.springframework.security.core.context.SecurityContextHolder; //導入方法依賴的package包/類
@After
public void tearDown() {
    SecurityContextHolder.clearContext();
}
 
開發者ID:syndesisio,項目名稱:syndesis,代碼行數:5,代碼來源:UserHandlerTest.java

示例14: teardown

import org.springframework.security.core.context.SecurityContextHolder; //導入方法依賴的package包/類
@After
public void teardown() throws Exception {
    logger.info("teardown");
    SecurityContextHolder.clearContext();
    session.invalidate();
}
 
開發者ID:oncecloud,項目名稱:devops-cstack,代碼行數:7,代碼來源:ModulesControllerTestIT.java

示例15: unsuccessfulAuthentication

import org.springframework.security.core.context.SecurityContextHolder; //導入方法依賴的package包/類
@Override
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
    AuthenticationException failed) throws IOException, ServletException {
  SecurityContextHolder.clearContext();
  failureHandler.onAuthenticationFailure(request, response, failed);
}
 
開發者ID:osswangxining,項目名稱:iotplatform,代碼行數:7,代碼來源:RefreshTokenProcessingFilter.java


注:本文中的org.springframework.security.core.context.SecurityContextHolder.clearContext方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。