簡體   English   中英

LinkedIn登錄與Spring MVC Web應用程序集成

[英]LinkedIn login integration with Spring MVC web Application

我是Spring的新手,我想登錄Spring MVC應用程序。 使用LinkedIn時,我使用SCRIBE進行登錄,但是當我運行該應用程序時,它將引發異常。

我在MVC中的Linkedin控制器代碼如下:

  @RequestMapping(value="/loginlinkedin", method=RequestMethod.GET)
    public String callback(WebRequest request){

        OAuthService service=linkedInServiceProvider.getService();
        System.out.println("above request token"); 
        //request token
        Token requestToken=service.getRequestToken(); 

        System.out.println(requestToken);
        System.out.println("below request token");

        //getting authorized url
        String oauth_verif= service.getAuthorizationUrl(requestToken);
        System.out.println(oauth_verif);

        //verifing the requested token
        Verifier verifier=new Verifier(in.nextLine());
        System.out.println("below verifier"+verifier);

        //get AccessToken
        Token accessToken=service.getAccessToken(requestToken, verifier);
        System.out.println("access tolen"+accessToken);


        OAuthRequest oauthRequest=new OAuthRequest(Verb.GET, "http://api.linkedin.com/v1/people/~:(id,first-name,last-name,industry,headline)");
        System.out.println("outhrequest"+oauthRequest);
        service.signRequest(accessToken, oauthRequest);
        Response outhResponse=oauthRequest.send();
        System.out.println(outhResponse.getBody());
        //ModelAndView mav=new ModelAndView("redirect:loginPage");
        return "result";
    }

和用於鏈接登錄的bean:

<bean id="linkedInServiceProvider" class="com.spring.handler.OAuthServiceProvider">
    <constructor-arg name="oauthServiceConfig" ref="linkedInServiceConfig" />
</bean

和OAuthServiceConfig類用於配置服務:

  package com.spring.handler;
import org.scribe.builder.api.Api;
public class OAuthServiceConfig {
private String apiKey;
private String apiSecret;
private String callback;
private Class apiClass;

public OAuthServiceConfig(String apiKey,String apiSecret,String callback,Class apiClass){
    super();
    this.apiKey=apiKey;
    this.apiSecret=apiSecret;
    this.callback=callback;
    this.apiClass=apiClass;
    System.out.println(apiKey+apiSecret+callback+apiClass);
}

public String getApiKey() {
    return apiKey;
}

public void setApiKey(String apiKey) {
    this.apiKey = apiKey;
}

public String getApiSecret() {
    return apiSecret;
}

public void setApiSecret(String apiSecret) {
    this.apiSecret = apiSecret;
}

public String getCallback() {
    return callback;
}

public void setCallback(String callback) {
    this.callback = callback;
}

public Class getApiClass() {
    return apiClass;
}

public void setApiClass(Class apiClass) {
    this.apiClass = apiClass;
}



}

OAuthServiceProvider類,用於使用linkedInServiceProvider.getService()方法向OAuthService提供服務:

   package com.spring.handler;

import org.scribe.builder.ServiceBuilder;
import org.scribe.oauth.OAuthService;

public class OAuthServiceProvider {
    OAuthServiceConfig oauthServiceConfig;

    public OAuthServiceProvider() {
    }

    public OAuthServiceProvider(OAuthServiceConfig oauthServiceConfig) {
        this.oauthServiceConfig = oauthServiceConfig;
    }

    public OAuthService getService() {
        System.out.println(oauthServiceConfig);
        return new ServiceBuilder().provider(oauthServiceConfig.getApiClass())
                .apiKey(oauthServiceConfig.getApiKey())
                .apiSecret(oauthServiceConfig.getApiSecret())
                .callback(oauthServiceConfig.getCallback()).build();
    }
}

但是,當我登錄到應用程序時,拋出的異常是:

May 28, 2015 5:42:17 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [HelloWebHandler] in context with path [/SpringHandler] threw exception [Request processing failed; nested exception is org.scribe.exceptions.OAuthException: Response body is incorrect. Can't extract token and secret from this: 'oauth_problem=permission_unknown'] with root cause
org.scribe.exceptions.OAuthException: Response body is incorrect. Can't extract token and secret from this: 'oauth_problem=permission_unknown'
    at org.scribe.extractors.TokenExtractorImpl.extract(TokenExtractorImpl.java:41)
    at org.scribe.extractors.TokenExtractorImpl.extract(TokenExtractorImpl.java:27)
    at org.scribe.oauth.OAuth10aServiceImpl.getAccessToken(OAuth10aServiceImpl.java:104)
    at org.scribe.oauth.OAuth10aServiceImpl.getAccessToken(OAuth10aServiceImpl.java:85)
    at org.scribe.oauth.OAuth10aServiceImpl.getAccessToken(OAuth10aServiceImpl.java:90)
    at com.spring.handler.UserController.callback(UserController.java:109)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

Permission_unknown :用戶尚未決定是否允許此使用者訪問受保護的資源。 通常在用戶完成授權過程之前,消費者請求訪問令牌時發生。

要跟蹤並解決您的問題:

  • 執行ServiceBuilder 上的 debug()方法時打開划線調試模式
  • 仔細閱讀 scribe-java 入門指南
  • 查看您的代碼是否存在不一致之處:
    //verifing the requested token
    Verifier verifier=new Verifier(in.nextLine());
    System.out.println("below verifier"+verifier);

什么是in 您必須驗證請求令牌,就像您從scribe示例中復制了該行一樣,無論如何都很難相信您的代碼可以正常工作,因為未聲明in

  • 重建控制器 :首先實現啟動身份驗證方法 ,獲取或存儲請求令牌屬性,並在需要時重定向到授權url(linkedin url)( service.getAuthorizationUrl() ); 實現您的回調方法 (在用戶批准對個人資料的訪問后,他將被重定向到您在OAuthServiceConfig類中定義的回調端點),並獲取oauth_verifier和請求令牌以驗證並獲取accessToken,對其進行存儲,對其進行簽名請求...等等...等等

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM