更改客户端目标httpservlet

时间:2016-09-19 16:02:30

标签: java servlets

我目前正在使用HttpServletResponse和HttpServletRequest设置服务器,我想知道我是否可以根据请求将响应发送到其他IP地址或相同的IP地址但是不同的端口。我的想法是用Java创建一个认证模块,用户使用Java应用程序登录,应用程序将触发浏览器,以便用户登录浏览器。登录成功后,浏览器将重定向回验证模块,而是将响应发送回浏览器,我将其发送到不同端口的Java应用程序(例如192.168.1.13:8993)。

我的代码基本上是这样的

@Override
    public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject)
            throws AuthException {
        final HttpServletRequest request = (HttpServletRequest) messageInfo.getRequestMessage();
        log.warning("validate request");
        final HttpServletResponse response = (HttpServletResponse) messageInfo.getResponseMessage();
        String ipAddress = request.getHeader("X-FORWARDED-FOR");
        final HttpSession session = request.getSession(true);
        final String requestUrl = getRequestUrl(request);
        final String returnToUrl = getReturnToURL(request);
        log.info(String.format("Incoming authentication request on \"%s\", expecting return to \"%s\".", requestUrl,
                returnToUrl));
        UserPrincipal principal;
        try {
            if (requestUrl.startsWith(returnToUrl)) {
                    log.info("Verifying authentication.");
                    principal = verifyReturningUser(request);
                    if (principal != null) {
                        storePrincipal(session, principal, clientSubject);
                        session.setAttribute(UserPrincipal.SESSION_ATTRIBUTE_KEY, principal);
                        String redirectUrl = (String) session.getAttribute(ATTR_KEY_USER_REDIRECT_URL);
                        session.removeAttribute(ATTR_KEY_USER_REDIRECT_URL);
                        response.addHeader(HTTP_HEADER_AUTH_KEY, HTTP_HEADER_AUTH_VALUE_SUCCESS);
                        if (redirectUrl != null) {
//Send the response back of redirect to /rest into destination with other IP address or the same address but different port.
                            response.sendRedirect(redirectUrl);
                            log.info(String.format("Initial authentication successful. Redirecting back to: %s",
                                    redirectUrl));
                        } else {
                            log.info("No redirect URL found.");
                        }

                        return AuthStatus.SEND_SUCCESS;
                    } 

有可能吗?

0 个答案:

没有答案