【问题标题】:How to implement Tomcat 8.0 container level authentication with Google OAuth2?如何使用 Google OAuth2 实现 Tomcat 8.0 容器级身份验证?
【发布时间】:2018-06-02 05:24:10
【问题描述】:

我在 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. 我将localhost 设置为在port 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 属性设置为&lt;Valve className="org.bsworks.catalina.authenticator.oidc.OpenIDConnectAuthenticator" discoveryDocumentURL="https://accounts.google.com/.well-known/openid-configuration" clientId="xxxx" clientSecret="xxxx"/&gt;

  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 容器级别的身份验证?

【问题讨论】:

    标签: java authentication tomcat8 openid-connect google-oauth


    【解决方案1】:

    事实证明,当 Google OAuth2 进行身份验证时,授权仍在由 Tomcat 处理,为此我必须创建一个角色 myrole 并向其中添加一个用户,其中用户名和密码是相同,即用户名和密码都是email@example.com -

    &lt;user username="email@example.com" password="email@example.com" roles="myrole"/&gt;

    摘自他们的文档 - “对于 OpenID Connect 身份验证器,需要以这样的方式配置领域,即密码始终与用户名相同(我们仍然需要密码,因为Tomcat API 中的限制)。”

    此外,我必须在&lt;auth-constraint&gt;&lt;security-role&gt; 中指定角色myrole -

    <security-constraint>
        ...
        <auth-constraint>
            <role-name>myrole</role-name>
        </auth-constraint>
    </security-constraint>
    <security-role>
        <role-name>myrole</role-name>
    </security-role>
    

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 2017-07-30
      • 1970-01-01
      • 2013-08-18
      • 1970-01-01
      • 2012-01-21
      • 2014-10-21
      • 1970-01-01
      • 2021-04-29
      相关资源
      最近更新 更多