我正在使用Apache Oltu,Java servlet和JSP创建一个Maven项目,该项目需要OAuth2身份验证才能访问API。到目前为止,我可以被重定向到网站并授权访问,但是,当网站重定向回我的回调网址时,我会收到一个弹出式提醒,其中包含我的回调网址,该网址附有授权代码参数,表示“页面加载”失败并显示错误:无法连接到服务器。'
这是因为我错误地映射了我的网址吗?还是我错过了别的什么?在授权代码交换访问令牌后,我尝试将用户重定向到apiConnector.jsp
。
我的servlet处理回调:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.oltu.oauth2.as.response.OAuthASResponse;
import org.apache.oltu.oauth2.client.OAuthClient;
import org.apache.oltu.oauth2.client.URLConnectionClient;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.apache.oltu.oauth2.client.response.GitHubTokenResponse;
import org.apache.oltu.oauth2.client.response.OAuthAuthzResponse;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.apache.oltu.oauth2.common.exception.OAuthSystemException;
import org.apache.oltu.oauth2.common.message.OAuthResponse;
import org.apache.oltu.oauth2.common.message.types.GrantType;
import OAuth2.main.OAuthUtils;
import OAuth2.model.OAuth2Details;
/**
* Servlet implementation class AuthCallbackController
*/
@WebServlet("/AuthCallbackController")
public class AuthCallbackController extends HttpServlet {
private OAuth2Details authDetails;
private static final long serialVersionUID = 1L;
private static final String LIST_DATA = "/apiConnector.jsp";
public static final String CODE_URL_PARAM_NAME = "code";
public static final String ERROR_URL_PARAM_NAME = "error";
public static final String URL_MAPPING = "/OAuth2Callback";
public static final String REDIRECT_URL = "/";
public AuthCallbackController() {
super();
authDetails = OAuthUtils.createOAuth2Details();
}
/**
* Handles the callback response from Higi Authorization Server
*/
protected void doGet (HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException {
String forward = LIST_DATA;
try {
OAuthAuthzResponse oar = OAuthAuthzResponse.oauthCodeAuthzResponse(servletRequest);
String code = oar.getCode();
OAuthClientRequest request = OAuthClientRequest
.tokenLocation(authDetails.getTokenEndpoint())
.setGrantType(GrantType.AUTHORIZATION_CODE)
.setClientId(authDetails.getClientId())
.setClientSecret(authDetails.getClientSecret())
.setRedirectURI(authDetails.getRedirectURI())
.setCode(code)
.buildQueryMessage();
OAuthClient oAuthClient = new OAuthClient (new URLConnectionClient());
GitHubTokenResponse oAuthResponse = oAuthClient.accessToken(request, GitHubTokenResponse.class);
String accessToken = oAuthResponse.getAccessToken();
String refreshToken = oAuthResponse.getRefreshToken();
Long expiresIn = oAuthResponse.getExpiresIn();
OAuthResponse r = OAuthASResponse
.tokenResponse(HttpServletResponse.SC_OK)
.setAccessToken(accessToken)
.setExpiresIn("3600")
.setRefreshToken(refreshToken)
.buildJSONMessage();
servletResponse.setStatus(r.getResponseStatus());
PrintWriter pw = servletResponse.getWriter();
pw.print(r.getBody());
pw.flush();
pw.close();
// Forward attributes to page
RequestDispatcher view = servletRequest.getRequestDispatcher(forward);
servletRequest.setAttribute("response",r.getBody());
view.forward(servletRequest, servletResponse);
} catch (OAuthProblemException | OAuthSystemException ex) {
ex.printStackTrace();
}
}
}
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>HigiSalesforceConnector</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>APIConnectorCallbackHandlerServlet</servlet-name>
<servlet-class>OAuth2.controller.AuthCallbackController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>APIConnectorCallbackHandlerServlet</servlet-name>
<url-pattern>/OAuth2Callback/*</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:0)
在做了更多的研究之后,我能够解决我的问题 - 最终必须在Eclipse上创建一个使用我更新的Tomcat配置进行SSL的新服务器。