【问题标题】:Implementation of Google authentication (OAuth) in CAS在 CAS 中实现 Google 身份验证 (OAuth)
【发布时间】:2018-12-22 17:25:23
【问题描述】:

我正在尝试在 CAS 4.1.9 中实现 Google 身份验证 (OAuth)。

首先,我在 console.developers.google.com 上创建了一个新项目,在其中为我的应用程序生成了一个新的客户端 ID。

然后,我使用 Maven 覆盖安装了 CAS。在我的 pom.xml 中,我包含了 pac4j 依赖项,如下所示:

<dependency>
    <groupId>org.pac4j</groupId>
    <artifactId>pac4j-oauth</artifactId>
    <version>3.4.0</version>
    <scope>runtime</scope>
</dependency>

此外,这是我在 applicationContext.xml 文件中添加的内容:

<bean id="GoogleAuthenticationHandler"
   class="org.pac4j.oauth.client.Google2Client"
   p:key="<MYKEY>"
   p:secret="<MYSECRET>"
   p:scope="EMAIL_AND_PROFILE" />

<bean id="clients"
   class="org.pac4j.core.client.Clients">
   <property name="callbackUrl" value="https://myapplication.com/cas" />
   <property name="clients">
     <list>
       <ref bean="GoogleAuthenticationHandler" />
     </list>
   </property>
</bean>

最后我在 casLoginView.jsp 中添加了以下行

<a href="${Google2ClientUrl}">Authenticate with Google</a> <br />

maven 编译工作正常,war 文件成功部署在我的 tomcat 服务器上。

问题是我的 CAS 登录网页中指向 Google 的 href 不包含正确的 OAuth url,但我在 console.developers.google.com 下为我的客户端定义的重定向 url 相同。

作为第二次尝试,我尝试将 ${Google2ClientUrl} 替换为 OAuth 网址:

<a href="https://accounts.google.com/o/oauth2/auth?redirect_uri=https://myapplication.com/cas&response_type=code&client_i
d=<MY CLIENT ID>&scope=https://www.googleapis.com/auth/analytics.re
adonly+https://www.googleapis.com/auth/userinfo.email&approval_prompt=force&access_type=offline">Authenticate with Google</a> <br />

在这种情况下,Google 登录页面显示并接受用户身份验证,但似乎 CAS 无法识别 Google 成功的用户身份验证(即它不显示包含身份验证成功消息的正常页面)。

我错过了什么吗?

非常感谢您的帮助!

【问题讨论】:

  • 您需要包含您的代码。回调发生在这里:myapplication.com/cas
  • 我应该准确包含哪些代码?上面已经报告了我对 CAS 配置所做的唯一修改。我没有修改任何其他源/配置 CAS 文件。
  • 关于 StackOverflow 的第一条规则:包括重现问题所需的一切。我给了你一个提示,问题出在回调 URL 上。包括实现该 URL 的源代码。

标签: oauth oauth-2.0 cas google-oauth


【解决方案1】:

我设法通过安装更新​​版本的 CAS (5.3.6) 解决了这个问题。在这里,Google 身份验证器配置要容易得多。实际上只需要两个步骤:

1) 在 pom.xml 文件中添加 pac4j 依赖

     <dependency>
               <groupId>org.apereo.cas</groupId>
               <artifactId>cas-server-support-pac4j-webflow</artifactId>
               <version>${cas.version}</version>
     </dependency>

2)在cas.properties文件中添加谷歌验证器参数:

       cas.authn.pac4j.google.id=<MY_CLIENT_ID>
       cas.authn.pac4j.google.secret=<MY_SECRET>
       cas.authn.pac4j.google.clientName=<MY_CLIENT_NAME>
       cas.authn.pac4j.google.autoRedirect=false
       cas.authn.pac4j.google.principalAttributeId=
       cas.authn.pac4j.google.scope=EMAIL_AND_PROFILE

完成此操作后,指向 Google 身份验证的链接会自动显示在 CAS 登录页面中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多