如何使用 Google OAuth2 实现 Tomcat 8.0 容器级身份验证?

How to implement Tomcat 8.0 container level authentication with Google OAuth2?

我在 AWS 上部署了一个 Java Web 应用程序,它使用基本的 Tomcat 身份验证 -

<login-config>
    <auth-method>BASIC</auth-method>
</login-config>

应用程序本身不执行任何身份验证。所有这些都由容器处理。我一直在尝试对 Google OAuth2 做同样的事情,其中​​应用程序 处理任何身份验证,但容器会处理,即使用 Google 或 GS​​uite 帐户。这可能吗?

OpenID Connect Authenticator for Tomcat 看起来很有希望,但是当我用它登录时,它一直将我重定向到登录错误页面。

我在尝试在我的 localhost -

上实施时遵循了这些步骤
  1. 我下载了他们的 jar file 并放在 /tomcat-8/lib/ 中。
  2. 我创建了一个客户端 ID,其中 Authorized JavaScript originshttps://localhost:8443Authorized redirect URIshttps://localhost:8443/myapp/j_security_check
  3. 我设置 localhostport 8443 上使用 https,因为 hostBaseURI 需要它。
  4. 在我的应用程序 web.xml 中,我设置了 -

    <security-constraint> <web-resource-collection> <web-resource-name>All</web-resource-name> <url-pattern>*.html</url-pattern> <url-pattern>/myapp/*</url-pattern> </web-resource-collection> <auth-constraint> <role-name>*</role-name> </auth-constraint> </security-constraint>

    <login-config> <auth-method>FORM</auth-method> <realm-name>authTestWeb</realm-name> <form-login-config> <form-login-page>/WEB-INF/public/login.html</form-login-page> <form-error-page>/WEB-INF/public/login-error.html</form-error-page> </form-login-config> </login-config>

  5. context.xml中,我将Valve属性设置为<Valve className="org.bsworks.catalina.authenticator.oidc.OpenIDConnectAuthenticator" discoveryDocumentURL="https://accounts.google.com/.well-known/openid-configuration" clientId="xxxx" clientSecret="xxxx"/>

  6. Google Sign-In script 放入 login.html

当我访问 https://localhost:8443/myapp 时,它会提示我输入 Google 用户名和密码。但是,当我登录时,它会短暂地向我显示 Check Cookie 消息并重定向到 login-error.html.

我做错了什么?

我也考虑过使用 Philip Green II's module for Google OAuth 2,但看起来这仅适用于 Tomcat 8.5 及更高版本。不幸的是,AWS Elastic Beanstalk 只有 Tomcat 8.0 作为最新版本。

那么,如何使用 Google OAuth2 实现 Tomcat 8.0 容器级别的身份验证?

事实证明,虽然 Google OAuth2 正在执行身份验证,但授权仍在由 Tomcat 处理,为此我必须创建一个角色 myrole 并将用户添加到即,用户名和密码相同,即用户名和密码均为 email@example.com -

<user username="email@example.com" password="email@example.com" roles="myrole"/>

摘自他们的文档 - "For the purpose of the OpenID Connect authenticator, the realm needs to be configured in such a way, that the password is always the same as the username (we still need the password due to limitations in the Tomcat API)."

此外,我必须在 <auth-constraint><security-role> -

中指定角色 myrole
<security-constraint>
    ...
    <auth-constraint>
        <role-name>myrole</role-name>
    </auth-constraint>
</security-constraint>
<security-role>
    <role-name>myrole</role-name>
</security-role>