简体   繁体   English

通过带有Spring Boot后端的Google Sign-In登录Android

[英]Android login via Google Sign-In with a Spring Boot backend

As the title says, I'm trying to use the Google Sign-In API with a Spring Boot backend server, as described here . 正如标题所说,我试图使用谷歌登录的API使用Spring的引导后端服务器,如所描述这里

Just to describe the context, the Spring backend is basically a resource+authentication server, that is currently providing Oauth2 authentication to a second spring boot application containing the frontend website, via Google SSO or simple form login (similar to what's described here ). 仅仅为了描述上下文,Spring后端基本上是一个资源+认证服务器,它目前通过Google SSO或简单的表单登录(类似于此处描述的内容)向包含前端网站的第二个Spring启动应用程序提供Oauth2身份验证。

My original idea was to mimic the @EnableOauth2Sso annotation by simply providing an access token to the android app and attach it to every request as "Bearer ". 我最初的想法是通过简单地为Android应用程序提供访问令牌来模仿@ EnableOauth2Sso注释,并将其作为“Bearer”附加到每个请求。 Using the user credentials for this was pretty straightforward: I simply make a request to the server at "/oauth/token", using those credentials inserted by the user as authentication and I correctly receive the access token. 使用用户凭据非常简单:我只是在“/ oauth / token”向服务器发出请求,使用用户插入的凭据作为身份验证,并且我正确地接收了访问令牌。

Now, I have absolutely no idea on how to build a similar procedure with the Google API in Android. 现在,我完全不知道如何在Android中使用Google API构建类似的程序。 The tutorial page I linked before describes how to get a token ID and how the server should validate it, but after that I don't know what to do. 我之前链接的教程页面描述了如何获取令牌ID以及服务器应如何验证它,但之后我不知道该怎么做。

So far I've managed to add a filter to the security chain that simply checks the token like this: 到目前为止,我已经设法在安全链中添加一个过滤器,只需像这样检查令牌:

    private Authentication attemptOpenIDAuthentication(@NonNull String tokenString){
        String clientId = authServices.getClientId();
        GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(transport, factory)
        .setAudience(Arrays.asList(clientId, androidClient))
        .build();

        try {
            GoogleIdToken token = verifier.verify(tokenString);
            if (token != null) {
                return authServices.loadAuthentication(token.getPayload());
            } else {
                throw new InvalidTokenException("ID token is null");
            }
        } catch (GeneralSecurityException | IOException e) {
            throw new BadCredentialsException("Could not validate ID token");
        }
    }

This manages indeed to create an Authentication object, but how can I generate an access token after the authentication filtering? 这确实管理了创建Authentication对象,但是如何在身份验证过滤后生成访问令牌?

To recap, so far I've got: 回顾一下,到目前为止我有:

  1. The Android app successfully retrieves the Google token ID and sends it to the server Android应用成功检索Google令牌ID并将其发送到服务器

  2. The server sucessfully intercepts the request and validates the token 服务器成功拦截请求并验证令牌

I'm basically missing the third point where I return a proper access token to the Android client. 我基本上错过了向Android客户端返回正确访问令牌的第三点。 Here you are a simple scheme to better understand the situation: 在这里,您是一个简单的方案,以更好地了解情况: 蹩脚的问题方案。 Web前端工作得很好,android缺少Google登录。

Is there any other way to validate the token and get an access token from the server, or should I completely change the authentication procedure on Android? 有没有其他方法可以验证令牌从服务器获取访问令牌,还是应该完全更改Android上的身份验证过程?

As far as I can tell: Yes, you need an access token from the server. 据我所知:是的,您需要来自服务器的访问令牌。 If I understand this correctly, a webapp is already authenticated via Oauth on your backend, so the procedure is similar here: Load the user with the google-ID and generate a token. 如果我理解正确,则已经通过后端的Oauth对webapp进行了身份验证,因此此过程类似于:使用google-ID加载用户并生成令牌。 In my application I used a JWT which is valid for 30 days. 在我的应用程序中,我使用了一个有效期为30天的JWT。 If the token expires, the Google authentication in the app is usually still valid, so the token can be renewed using the Google ID. 如果令牌过期,则应用中的Google身份验证通常仍然有效,因此可以使用Google ID续订令牌。 With Oauth you can also send a refresh-token directly. 使用Oauth,您还可以直接发送刷新令牌。 It is important that the app always checks the Google authentication first and only in a second step that of the backend. 重要的是,应用程序始终首先检查Google身份验证,并且仅在后端的第二步中检查。

For the Authentication process on the backend u may need to manually implement a dedicated securityConfiguration for this. 对于后端的身份验证过程,您可能需要为此手动实现专用的securityConfiguration。 Have a look at the jhipster project, they implemented a custom jwt-authentication which may give you an idea how it works. 看看jhipster项目,他们实现了一个自定义的jwt-authentication,可以让你知道它是如何工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM