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

标签 java authentication tomcat8 openid-connect google-oauth

我在 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. 我使用 Authorized JavaScript origins 创建了一个客户端 ID如https://localhost:8443Authorized redirect URIshttps://localhost:8443/myapp/j_security_check .
  3. 我设置localhost使用httpsport 8443 ,自 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 scriptlogin.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"/>

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

此外,我必须指定角色 myrole<auth-constraint><security-role> -

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

关于java - 如何使用Google OAuth2实现Tomcat 8.0容器级身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47892675/

相关文章:

java - 反转所有单词,设置 "."并对接下来的句子执行相同的操作

java - 如何让嵌入式Jpanel走向WEST

.net - .Net 2.0 Web 服务中的 SSPI 连接

wcf - 如何使用 Delphi XE 发送 WCF 的 ClientCredentials

java - spring-boot web 应用程序中未考虑 web.xml session 超时?

java - 查看更改时出现错误 JSON 输入意外结束

java - 钻石广场执行不当

iphone - 什么 ios 方法引用 "login through UIWebview and redirects me to another view controller"?

linux - 使用 service tomcat8 start 服务 tomcat8 启动失败

java - 我可以用 Tomcat 8 注册多个 HttpSessionListener 覆盖吗?