问题描述
我有一个应用程序A(客户端),该应用程序对应用程序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
://sometestsite.com/pagLogin。
并尝试使用HttpURLConnection
上的setRequestProperty("cookieName","value")
将cookie发送到服务器appB。
2)由于在没有cookie的情况下appB使用sendRedirect,因此如何(最好的做法)是将登录凭据和get请求一起从appA发送到appB,以便appB在进行sendRedirect
调用时可以转发这些详细信息。
1楼
该设置似乎已实现了OAuth2.0授权代码授予类型。 在OAuth2.0术语中,承载登录页面的服务器称为“授权服务器”,承载API或任何需要身份验证的网站的服务器称为“资源服务器”,而尝试使用api的应用程序称为“客户端”。
现在,如果“客户端”代表用户执行操作(考虑到最终用户要登录Web应用程序),那么您描述的设置就是正确的设置。 可以使用“授权码”授予类型,“隐式”授予类型和“资源所有者密码凭证”授予类型中的任何一种,并且它们中的每一个都会将用户重定向到如上所述的登录页面。
但是,当“客户”不像您的情况那样代表任何单个用户(例如,批处理作业)时,要使用的授权类型为“客户凭证”授权类型。 在这里,不会重定向到登录页面。 相反,“客户端”将使用客户端ID和客户端机密直接与“授权服务器”通信,而“授权服务器”将返回访问代码。 客户端可以使用访问代码与“资源服务器”中的api通信(可以通过cookie)。
有关完整的详细信息,请参阅RFC 6749 OAuth2.0规范中的“客户端凭据”授予类型描述。