2016-09-02 16 views
0

Apache Oltu, Java 서블릿 및 API에 액세스하기 위해 OAuth2 인증이 필요한 JSP를 사용하여 Maven 프로젝트를 만들고 있습니다. 지금까지 웹 사이트로 리디렉션되고 액세스 권한을 부여 할 수 있었지만 웹 사이트가 콜백 URL로 리디렉션되면 콜백 URL에 '페이지로드에 오류가 발생하지 않았습니다.'라는 인증 코드 매개 변수가 추가 된 경고 팝업이 표시됩니다. : 서버에 연결할 수 없습니다. 'Apache Oltu OAuth2.0 콜백 처리

내 URL을 잘못 매핑했기 때문입니까? 아니면 다른 것을 놓치고 있습니까? 액세스 코드에 대한 인증 코드가 교환 된 후 사용자를 apiConnector.jsp으로 리디렉션하려고합니다.

콜백 처리하기 위해 내 서블릿

: 내 problem--를 해결할 수 있었다 더 많은 연구를하고 후

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

이를 만들 필요 결국 이클립스의 새로운 서버는 SSL을 위해 업데이트 된 Tomcat 설정을 사용했다.