Spring security ApacheOltuSpring安全OAuth2和Google集成
参考资料仅取自以下网站:-Spring security ApacheOltuSpring安全OAuth2和Google集成,spring-security,oauth-2.0,google-oauth,oltu,Spring Security,Oauth 2.0,Google Oauth,Oltu,参考资料仅取自以下网站:- 我已经开发了字符串安全OAuth2 Facebook集成示例,现在我期待着开发安全OAuth2 Google(以及后来的Github)集成示例,其中将提供AppID和Secret以获取“访问令牌”和“刷新令牌”等,用于访问受保护的资源,如UserDetails等 所以,第一步将是注册应用程序。所以它给了我“客户端ID”和“客户端机密”,我还配置了重定向URI,完成了 现在我已经开始编写实际的ApacheAuth客户端,但我不确定需要提供哪些参数(类似地,我为F
OAuthClientRequest request = OAuthClientRequest
.authorizationLocation("")
.setClientId("3kT21Hlkzzt5eV1")
.setRedirectURI("http://localhost:8080/apache-oltu/google/redirect")
.setResponseType("")
.setScope("")
.buildQueryMessage();
下面的代码是为回调开发的
private void getAccessToken(String authorizationCode) throws OAuthSystemException, OAuthProblemException {
OAuthClientRequest request = OAuthClientRequest
.tokenLocation("")
.setGrantType()
.setClientId("3kT21H5EO3zzt5eV1")
.setClientSecret("1kT21Hdlkzzt5eV1")
.setRedirectURI("http://localhost:8080/apache-oltu/google/redirect")
.setCode()
.buildBodyMessage();
添加了以下代码以获取受保护的资源,如用户配置文件:
request= new OAuthBearerClientRequest("https://www.googleapis.com/auth/userinfo.profile").
setAccessToken(oAuthResponse.getAccessToken()).
buildQueryMessage();
有关完整示例,请参见此处:
我已经开发了ApacheOLTU和Spring集成示例,它在我这边运行良好 您需要按照@prtk_shah的建议启用Google+API。谢谢 您需要转到并单击您的项目,在我的例子中是“ApacheOLTU”,在您的开放项目中查找选项“APIsandAuth”-->APIs。搜索Google+API并启用它 在这里你应该可以看到这个屏幕。 因此,我将在下面修改您的代码,应该是这样的: (IMP)-您的客户端ID应该是这样的,例如:(755670439314-jcumfghnkmcm72hf40beikvoatknstml.apps.googleusercontent.com),请确保它是正确的。仅供参考-按google开发者控制台提供的方式使用
OAuthClientRequest request = OAuthClientRequest
.authorizationLocation("https://accounts.google.com/o/oauth2/auth")
.setClientId("3kT21Hlkzzt5eV1.apps.googleusercontent.com")
.setRedirectURI("Give your projects redirect URI")
.setResponseType("responsecode")
.setScope("openId profile email")
.buildQueryMessage();
回调代码应为:
private void getAccessToken(String authorizationCode) throws OAuthSystemException, OAuthProblemException {
OAuthClientRequest request = OAuthClientRequest
.tokenLocation("https://accounts.google.com/o/oauth2/token")
.setGrantType(GrantType.AUTHORIZATION_CODE)
.setClientId("give your complete client id")
.setClientSecret("give your secret")
.setRedirectURI("This will be your callback or Redirect URL (Give it correctly)")
.setCode(authorizationCode)
.buildBodyMessage();
这是我在我的例子中得到的,只是想向你们展示
希望这会有所帮助。我已经修改了代码并使用了scope(),但我发现它没有给我配置文件数据。您能提供帮助/指导吗?添加作用域“openId配置文件电子邮件”,并在您配置的客户端应用程序中启用此作用域。如果在谷歌控制台中您没有启用谷歌+相关的socpes,那么谷歌将不会向您提供用户信息。@prtk_-shah:我使用.setScope(“openId配置文件电子邮件”),仍然无法获取配置文件的详细信息。你能尽快帮我吗?萨琪布:谢谢。你能提供更详细的细节吗。它不理解您使用了哪个URL。您必须在Google开发者控制台中启用“Google+API”。转到并单击您的项目,在项目中打开“API和auth”>API。搜索Google+API并启用它。