简体   繁体   English

如何使用Web登录页面凭据对REST Web服务进行身份验证

[英]how to authenticate REST webservice get call using web login page credentials

I have an app A(client), which makes a web-service GET call to App B(server). 我有一个应用程序A(客户端),该应用程序对应用程序B(服务器)进行了Web服务GET调用。 App B is using web page authentication redirect for all these incoming web service get request calls. 应用程序B使用网页身份验证重定向来处理所有这些传入的Web服务获取请求调用。 AppB is processing GET request some thing like: 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);
}

}

I am working on appA(client) side, to make requests to appB(server), appA is java, REST, spring boot based micro-service. 我在appA(客户端)上工作,向appB(服务器)发出请求,appA是基于Java,REST,基于spring boot的微服务的。

Question: How can I successfully get through this authentication?

1) In appA I have tried using ApacheHttpClient, and URLConnection to establish a connection to url: https://sometestsite.com/pagLogin . 1)在appA中,我尝试使用ApacheHttpClient和URLConnection建立与url的连接: https://sometestsite.com/pagLogin ://sometestsite.com/pagLogin。 and tried to send cookies to server appB using setRequestProperty("cookieName","value") on HttpURLConnection . 并尝试使用HttpURLConnection上的setRequestProperty("cookieName","value")将cookie发送到服务器appB。

2) as appB uses sendRedirect in case no cookie exist, How to (is it a best practice to) send login crendentials along with get request from appA to appB, so that appB can forward those details when it makes sendRedirect call. 2)由于在没有cookie的情况下appB使用sendRedirect,因此如何(最好的做法)是将登录凭据和get请求一起从appA发送到appB,以便appB在进行sendRedirect调用时可以转发这些详细信息。

The setup seems to have implemented OAuth2.0 Authorization Code grant type. 该设置似乎已实现了OAuth2.0授权代码授予类型。 In OAuth2.0 terminology, the server hosting the login page is called "authorization server", the server hosting the API or any website requiring authentication is called "resource server" and the application trying to consume the api is called "client". 在OAuth2.0术语中,承载登录页面的服务器称为“授权服务器”,承载API或任何需要身份验证的网站的服务器称为“资源服务器”,而尝试使用api的应用程序称为“客户端”。

Now, if the "Client" acts on behalf of a user (consider an end user wants to log into a web application), the setup you described is the right setup. 现在,如果“客户端”代表用户执行操作(考虑到最终用户要登录Web应用程序),那么您描述的设置就是正确的设置。 Any one of Authorization Code grant type, Implicit grant type and Resource Owner Password Credential grant type can be used and each of them will redirect the user to a login page as you mentioned above. 可以使用“授权码”授予类型,“隐式”授予类型和“资源所有者密码凭证”授予类型中的任何一种,并且它们中的每一个都会将用户重定向到如上所述的登录页面。

But when the "Client" is not acting on behalf of any individual user (eg a batch job) as in your case, the grant type to be used is Client Credential grant type. 但是,当“客户”不像您的情况那样代表任何单个用户(例如,批处理作业)时,要使用的授权类型为“客户凭证”授权类型。 Here no redirection to login page will happen. 在这里,不会重定向到登录页面。 Instead the "client" will directly communicate with the "authorization server" with a client id and client secret and the "authorization server" will return an access code. 相反,“客户端”将使用客户端ID和客户端机密直接与“授权服务器”通信,而“授权服务器”将返回访问代码。 The client can the communicate with the api in "resource server" with the access code (may be through cookie). 客户端可以使用访问代码与“资源服务器”中的api通信(可以通过cookie)。

Refer to Client Credential grant type description in RFC 6749 OAuth2.0 specification for complete details. 有关完整的详细信息,请参阅RFC 6749 OAuth2.0规范中的“客户端凭据”授予类型描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM