org.springframework.security.web.savedrequest.RequestCache.getRequest()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(12.0k)|赞(0)|评价(0)|浏览(121)

本文整理了Java中org.springframework.security.web.savedrequest.RequestCache.getRequest方法的一些代码示例,展示了RequestCache.getRequest的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。RequestCache.getRequest方法的具体详情如下:
包路径:org.springframework.security.web.savedrequest.RequestCache
类名称:RequestCache
方法名:getRequest

RequestCache.getRequest介绍

[英]Returns the saved request, leaving it cached.
[中]返回保存的请求,将其保留为缓存。

代码示例

代码示例来源:origin: wuyouzhuguli/SpringAll

@GetMapping("/authentication/require")
  @ResponseStatus(HttpStatus.UNAUTHORIZED)
  public String requireAuthentication(HttpServletRequest request, HttpServletResponse response) throws IOException {
    SavedRequest savedRequest = requestCache.getRequest(request, response);
    if (savedRequest != null) {
      String targetUrl = savedRequest.getRedirectUrl();
      if (StringUtils.endsWithIgnoreCase(targetUrl, ".html"))
        redirectStrategy.sendRedirect(request, response, "/login.html");
    }
    return "访问的资源需要身份认证!";
  }
}

代码示例来源:origin: BroadleafCommerce/BroadleafCommerce

SavedRequest savedRequest = requestCache.getRequest(request, response);
if (savedRequest == null) {
  super.onAuthenticationSuccess(request, response, authentication);

代码示例来源:origin: cloudfoundry/uaa

case MFA_COMPLETED:
  logger.debug("MFA has been completed for " + getAuthenticationLogInfo());
  SavedRequest savedRequest = cache.getRequest(request, response);
  if (savedRequest != null) {
    logger.debug("Redirecting request to " + savedRequest.getRedirectUrl());

代码示例来源:origin: cloudfoundry/uaa

@Test
public void follow_completed_redirect_with_saved_request() throws Exception {
  String location = "/oauth/authorize";
  SavedRequest savedRequest = getSavedRequest(location);
  when(cache.getRequest(any(), any())).thenReturn(savedRequest);
  request.setPathInfo("/force_password_change_completed");
  request.setMethod(HttpMethod.POST.name());
  SecurityContextHolder.getContext().setAuthentication(authentication);
  when(authentication.isAuthenticated()).thenReturn(true);
  when(authentication.isRequiresPasswordChange()).thenReturn(false);
  filter.doFilterInternal(request, response, chain);
  verify(chain, never()).doFilter(any(), any());
  verify(response, times(1)).sendRedirect(location);
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void do_filter_mfa_completed_with_saved_request() throws Exception {
  SavedRequest savedRequest = mock(SavedRequest.class);
  String redirect = "http://localhost:8080/uaa/oauth/authorize";
  when(savedRequest.getRedirectUrl()).thenReturn(redirect);
  when(requestCache.getRequest(same(request), same(response))).thenReturn(savedRequest);
  request.setContextPath("/uaa");
  when(spyFilter.getNextStep(any(HttpServletRequest.class))).thenReturn(MFA_COMPLETED);
  spyFilter.doFilter(request, response, chain);
  verify(requestCache, times(1)).getRequest(same(request), same(response));
  verify(spyFilter, times(1)).sendRedirect(eq(redirect), same(request), same(response));
}

代码示例来源:origin: cloudfoundry/uaa

@Test
public void do_filter_mfa_completed_no_saved_request() throws Exception {
  request.setContextPath("/uaa");
  when(spyFilter.getNextStep(any(HttpServletRequest.class))).thenReturn(MFA_COMPLETED);
  spyFilter.doFilter(request, response, chain);
  verify(requestCache, times(1)).getRequest(same(request), same(response));
  verify(spyFilter, times(1)).sendRedirect(eq("/"), same(request), same(response));
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void requestCacheAsBean() throws Exception {
  this.spring.register(RequestCacheBeanConfig.class,
      AuthenticationTestConfiguration.class).autowire();
  RequestCache requestCache = this.spring.getContext().getBean(RequestCache.class);
  this.mockMvc.perform(formLogin())
      .andExpect(authenticated());
  verify(requestCache).getRequest(any(), any());
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void requestCache() throws Exception {
  this.spring.register(RequestCacheConfig.class,
      AuthenticationTestConfiguration.class).autowire();
  RequestCacheConfig config = this.spring.getContext().getBean(RequestCacheConfig.class);
  this.mockMvc.perform(formLogin())
      .andExpect(authenticated());
  verify(config.requestCache).getRequest(any(), any());
}

代码示例来源:origin: cloudfoundry/uaa

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
  if (isIgnored(request, response)) {
    //pass through even though 'change' is required request
    filterChain.doFilter(request, response);
  } else if (isCompleted(request)) {
    logger.debug("Forced password change has been completed.");
    SavedRequest savedRequest = cache.getRequest(request, response);
    if (savedRequest != null) {
      sendRedirect(savedRequest.getRedirectUrl(), request, response);
    } else {
      sendRedirect("/", request, response);
    }
  } else if (needsPasswordReset() && !matcher.matches(request)) {
    logger.debug("Password change is required for user.");
    cache.saveRequest(request, response);
    sendRedirect(redirectUri, request, response);
  } else if (matcher.matches(request) && isAuthenticated() && !needsPasswordReset()) {
    sendRedirect("/", request, response);
  } else {
    //pass through
    filterChain.doFilter(request, response);
  }
}

代码示例来源:origin: spring-projects/spring-security

SavedRequest savedRequest = this.requestCache.getRequest(request, response);
if (savedRequest != null) {
  redirectUrl = savedRequest.getRedirectUrl();

代码示例来源:origin: stackoverflow.com

RequestCache requestCache = new HttpSessionRequestCache();
SavedRequest savedRequest = requestCache.getRequest(request, response);
String targetUrl = savedRequest.getRedirectUrl();

代码示例来源:origin: stackoverflow.com

HttpServletRequest req = (HttpServletRequest)request.getNativeRequest();
HttpServletResponse resp = (HttpServletResponse)request.getNativeResponse();

RequestCache rc = new HttpSessionRequestCache();
SavedRequest savedRequest = rc.getRequest(req, resp);
String targetUrl = savedRequest.getRedirectUrl();
if(targetUrl != null){
   log.info("Redirecting to DefaultSavedRequest Url: " + targetUrl);
   new DefaultRedirectStrategy().sendRedirect(req, resp, targetUrl);
   hasSentRedirect = true;
}

代码示例来源:origin: whyalwaysmea/Spring-Security

/**
 * 当需要身份认证的时候,跳转过来
 * @param request
 * @param response
 * @return
 */
@RequestMapping(SecurityConstants.DEFAULT_UNAUTHENTICATION_URL)
@ResponseStatus(code = HttpStatus.UNAUTHORIZED)
public BaseResponse requireAuthenication(HttpServletRequest request, HttpServletResponse response) throws IOException {
  SavedRequest savedRequest = requestCache.getRequest(request, response);
  if (savedRequest != null) {
    String targetUrl = savedRequest.getRedirectUrl();
    logger.info("引发跳转的请求是:" + targetUrl);
    if (StringUtils.endsWithIgnoreCase(targetUrl, ".html")) {
      redirectStrategy.sendRedirect(request, response, securityProperties.getBrowser().getLoginPage());
    }
  }
  return new BaseResponse("访问的服务需要身份认证,请引导用户到登录页");
}

代码示例来源:origin: chocotan/lolibox

private String extractOriginalUrl(NativeWebRequest request) {
  HttpServletRequest nativeReq = request.getNativeRequest(HttpServletRequest.class);
  HttpServletResponse nativeRes = request.getNativeResponse(HttpServletResponse.class);
  SavedRequest saved = requestCache.getRequest(nativeReq, nativeRes);
  if (saved == null) {
    return null;
  }
  requestCache.removeRequest(nativeReq, nativeRes);
  removeAutheticationAttributes(nativeReq.getSession(false));
  return saved.getRedirectUrl();
}

代码示例来源:origin: tihomcode/TiHom-Security

/**
 * 当需要身份认证时,跳转到这里
 * @param request
 * @param response
 * @return
 */
@RequestMapping(SecurityConstants.DEFAULT_UNAUTHENTICATION_URL)
@ResponseStatus(code = HttpStatus.UNAUTHORIZED) //401
public SimpleResponse requireAuthentication(HttpServletRequest request, HttpServletResponse response) throws IOException {
  SavedRequest savedRequest = requestCache.getRequest(request, response);
  if(savedRequest!=null){
    String target = savedRequest.getRedirectUrl();
    logger.info("引发跳转的请求是:"+target);
    if(StringUtils.endsWithIgnoreCase(target,".html")){
      redirectStrategy.sendRedirect(request,response,securityProperties.getBrowser().getLoginPage());
    }
  }
  return new SimpleResponse("访问的服务需要身份认证,请引导用户到登录页");
}

代码示例来源:origin: org.craftercms/crafter-security-provider

protected void redirectToSavedRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
  SavedRequest savedRequest = requestCache.getRequest(request, response);
  if (!isAlwaysUseDefaultTargetUrl() && savedRequest != null) {
    RedirectUtils.redirect(request, response, savedRequest.getRedirectUrl());
  } else {
    RedirectUtils.redirect(request, response, getDefaultTargetUrl());
  }
}

代码示例来源:origin: luotuo/springboot-security-wechat

public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
  SavedRequest savedRequest = this.requestCache.getRequest(request, response);
  if(savedRequest == null) {
    //super.onAuthenticationSuccess(request, response, authentication);
    handle(request, response, authentication);
    super.clearAuthenticationAttributes(request);
  } else {
    String targetUrlParameter = this.getTargetUrlParameter();
    if(!this.isAlwaysUseDefaultTargetUrl() && (targetUrlParameter == null || !StringUtils.hasText(request.getParameter(targetUrlParameter)))) {
      this.clearAuthenticationAttributes(request);
      String targetUrl = savedRequest.getRedirectUrl();
      this.logger.debug("Redirecting to DefaultSavedRequest Url: " + targetUrl);
      //this.getRedirectStrategy().sendRedirect(request, response, targetUrl);
    } else {
      this.requestCache.removeRequest(request, response);
      //super.onAuthenticationSuccess(request, response, authentication);
      handle(request, response, authentication);
      super.clearAuthenticationAttributes(request);
    }
  }
}

代码示例来源:origin: apache/servicemix-bundles

@Override
public void onAuthenticationSuccess(HttpServletRequest request,
    HttpServletResponse response, Authentication authentication)
    throws ServletException, IOException {
  SavedRequest savedRequest = requestCache.getRequest(request, response);
  if (savedRequest == null) {
    super.onAuthenticationSuccess(request, response, authentication);
    return;
  }
  String targetUrlParameter = getTargetUrlParameter();
  if (isAlwaysUseDefaultTargetUrl()
      || (targetUrlParameter != null && StringUtils.hasText(request
          .getParameter(targetUrlParameter)))) {
    requestCache.removeRequest(request, response);
    super.onAuthenticationSuccess(request, response, authentication);
    return;
  }
  clearAuthenticationAttributes(request);
  // Use the DefaultSavedRequest URL
  String targetUrl = savedRequest.getRedirectUrl();
  logger.debug("Redirecting to DefaultSavedRequest Url: " + targetUrl);
  getRedirectStrategy().sendRedirect(request, response, targetUrl);
}

代码示例来源:origin: peholmst/vaadin4spring

@Override
public void onAuthenticationSuccess(Authentication authentication) throws Exception {
  HttpServletRequest request = http.getCurrentRequest();
  HttpServletResponse response = http.getCurrentResponse();
  SavedRequest savedRequest = requestCache.getRequest(request, response);
  if (savedRequest == null) {
    super.onAuthenticationSuccess(authentication);
    return;
  }
  String targetUrlParameter = getTargetUrlParameter();
  if (isAlwaysUseDefaultTargetUrl() || (targetUrlParameter != null && StringUtils.hasText(request.getParameter(targetUrlParameter)))) {
    requestCache.removeRequest(request, response);
    super.onAuthenticationSuccess(authentication);
    return;
  }
  clearAuthenticationAttributes();
  // Use the DefaultSavedRequest URL
  String targetUrl = savedRequest.getRedirectUrl();
  logger.debug("Redirecting to saved request redirect url: " + targetUrl);
  redirectStrategy.sendRedirect(targetUrl);
}

代码示例来源:origin: org.vaadin.spring.extensions/vaadin-spring-ext-security

@Override
public void onAuthenticationSuccess(Authentication authentication) throws Exception {
  HttpServletRequest request = http.getCurrentRequest();
  HttpServletResponse response = http.getCurrentResponse();
  SavedRequest savedRequest = requestCache.getRequest(request, response);
  if (savedRequest == null) {
    super.onAuthenticationSuccess(authentication);
    return;
  }
  String targetUrlParameter = getTargetUrlParameter();
  if (isAlwaysUseDefaultTargetUrl() || (targetUrlParameter != null && StringUtils.hasText(request.getParameter(targetUrlParameter)))) {
    requestCache.removeRequest(request, response);
    super.onAuthenticationSuccess(authentication);
    return;
  }
  clearAuthenticationAttributes();
  // Use the DefaultSavedRequest URL
  String targetUrl = savedRequest.getRedirectUrl();
  logger.debug("Redirecting to saved request redirect url: " + targetUrl);
  redirectStrategy.sendRedirect(targetUrl);
}

相关文章

微信公众号

最新文章

更多