当前位置: 首页>>代码示例>>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;未经允许,请勿转载。