WebUtils.java 文件源码

java
阅读 35 收藏 0 点赞 0 评论 0

项目:atbash-octopus 作者:
/**
 * Redirects the to the request url from a previously
 * {@link #saveRequest(ServletRequest) saved} request, or if there is no saved request, redirects the
 * end user to the specified {@code fallbackUrl}.  If there is no saved request or fallback url, this method
 * throws an {@link IllegalStateException}.
 * <p/>
 * This method is primarily used to support a common login scenario - if an unauthenticated user accesses a
 * page that requires authentication, it is expected that request is
 * {@link #saveRequest(ServletRequest) saved} first and then redirected to the login page. Then,
 * after a successful login, this method can be called to redirect them back to their originally requested URL, a
 * nice usability feature.
 *
 * @param request     the incoming request
 * @param response    the outgoing response
 * @param fallbackUrl the fallback url to redirect to if there is no saved request available.
 * @throws IllegalStateException if there is no saved request and the {@code fallbackUrl} is {@code null}.
 * @throws IOException           if there is an error redirecting
 */
public static void redirectToSavedRequest(ServletRequest request, ServletResponse response, String fallbackUrl)
        throws IOException {
    String successUrl = null;
    boolean contextRelative = true;
    SavedRequest savedRequest = WebUtils.getAndClearSavedRequest(request);
    if (savedRequest != null && savedRequest.getMethod().equalsIgnoreCase(AccessControlFilter.GET_METHOD)) {
        successUrl = savedRequest.getRequestUrl();
        contextRelative = false;
    }

    if (successUrl == null) {
        successUrl = fallbackUrl;
    }

    if (successUrl == null) {
        throw new IllegalStateException("Success URL not available via saved request or via the " +
                "successUrlFallback method parameter. One of these must be non-null for " +
                "issueSuccessRedirect() to work.");
    }

    WebUtils.issueRedirect(request, response, successUrl, null, contextRelative);
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号