如何使用Web登录页面凭据验证REST Web服务获取调用

问题描述 投票:1回答:1

我有一个应用程序A(客户端),它对App B(服务器)进行Web服务GET调用。应用B正在使用网页身份验证重定向来进行所有这些传入的Web服务获取请求调用。 AppB正在处理GET请求,例如:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
// code lines
//....
..
String login_URL = "https://sometestsite.com/pagLogin";
StringBuffer baseURL = request.getRequestURL();
String query = request.getQueryString();
String encReturnURL = URLEncoder.encode(baseURL.toString() + "?" + query, "UTF-8");
String final_URL = login_URL + encReturnURL ;
Cookie[] cookies = request.getCookies();
    if ((cookies == null) || (cookies.length == 0))
    {
        response.sendRedirect(noCookieURL);
                return;
    }
String cookieValue= null;

for (int i = 0; i < cookies.length; i++)
        {
            Cookie thisCookie = cookies[i];
            String cookieName = thisCookie.getName();

            if (cookieName == null)
            {               
                //logger.info("cookieName is null");
            }
            //logger.info("cookieName is " + cookieName);

            if (cookieName.equals("myCookie"))
            {           
                cookieValue = thisCookie.getValue();
                break;
            }
        }

String ESEncypt = esGatekeeper.esGatekeeper(cookieValue,"password");
if(ESEncrypt satisfies some condition){
    // construct output message and response
    String output = "{Some JSON message}";
    response.setContentType("application/json");
    response.getWriter().append(output);
}

}

我正在appA(客户端)方面,向appB(服务器)发出请求,appA是java,REST,基于spring boot的微服务。

Question: How can I successfully get through this authentication?

1)在appA中,我尝试使用ApacheHttpClient和URLConnection建立与url:https://sometestsite.com/pagLogin的连接。并尝试使用setRequestProperty("cookieName","value")上的HttpURLConnection将cookie发送到服务器appB。

2)因为appB使用sendRedirect以防万一没有cookie,如何(最佳做法)发送登录凭证以及从appA到appB的get请求,以便appB可以在调用sendRedirect时转发这些细节。

java rest web-services authentication login
1个回答
0
投票

该设置似乎已实现OAuth2.0授权代码授权类型。在OAuth2.0术语中,托管登录页面的服务器称为“授权服务器”,托管API的服务器或任何需要身份验证的网站称为“资源服务器”,尝试使用api的应用程序称为“客户端”。

现在,如果“客户端”代表用户(考虑最终用户想要登录Web应用程序),则您所描述的设置是正确的设置。可以使用授权代码授权类型,隐式授权类型和资源所有者密码凭据授予类型中的任何一种,并且它们中的每一个都将用户重定向到如上所述的登录页面。

但是,如果“客户端”没有像您的情况那样代表任何单个用户(例如批处理作业),则要使用的授权类型是“客户端凭据”授予类型。这里不会重定向到登录页面。相反,“客户端”将直接与具有客户端ID和客户端秘密的“授权服务器”通信,并且“授权服务器”将返回访问代码。客户端可以使用访问代码与“资源服务器”中的api进行通信(可以通过cookie)。

有关完整的详细信息,请参阅RFC 6749 OAuth2.0规范中的客户端凭据授予类型说明。

© www.soinside.com 2019 - 2024. All rights reserved.