【发布时间】:2018-06-02 05:24:10
【问题描述】:
我在 AWS 上部署了一个 Java Web 应用程序,它使用基本的 Tomcat 身份验证 -
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
应用程序本身不执行任何身份验证。所有这些都由容器处理。我一直在尝试对 Google OAuth2 做同样的事情,其中应用程序不处理任何身份验证,但容器会处理,即使用 Google 或 GSuite 帐户登录。这可能吗?
OpenID Connect Authenticator for Tomcat 看起来很有希望,但是当我使用它登录时,它一直将我重定向到登录错误页面。
我尝试在我的 localhost 上实现此功能时遵循了这些步骤 -
- 我下载了他们的jar file并将其放在
/tomcat-8/lib/中。 - 我创建了一个客户端 ID,
Authorized JavaScript origins为https://localhost:8443,Authorized redirect URIs为https://localhost:8443/myapp/j_security_check。 - 我将
localhost设置为在port 8443上使用https,因为hostBaseURI需要它。 -
在我的应用程序的
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> 在
context.xml中,我将Valve属性设置为<Valve className="org.bsworks.catalina.authenticator.oidc.OpenIDConnectAuthenticator" discoveryDocumentURL="https://accounts.google.com/.well-known/openid-configuration" clientId="xxxx" clientSecret="xxxx"/>。- 将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