歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring Security 學習之OpenID認證

Spring Security 學習之OpenID認證

日期:2017/3/1 9:47:52   编辑:Linux編程

一、前言

OpenID是一個以用戶為中心的數字身份識別框架,它具有開放、分散、自由等特性.

登錄一個支持 OpenID 的網站非常簡單(即便你是第一次訪問這個網站也是一樣)。 只需要輸入你注冊好的 OpenID 用戶名,然後你登錄的網站會跳轉到你的 OpenID 服務網站, 在你的 OpenID 服務網站輸入密碼(或者其它需要填寫的信息)驗證通過後, 你會回到登錄的網站並且已經成功登錄。 OpenID 系統可以應用於所有需要身份驗證的地方, 既可以應用於單點登錄系統,也可以用於共享敏感數據時的身份認證。

除了一處注冊,到處通行以外,OpenID 給所有支持 OpenID 的網站帶來了價值--共享用戶資源。 用戶可以清楚的控制哪些信息可以被共享,例如姓名、地址、電話號碼等。

更多請參考http://openid.net.cn/

二、Spring Security對OpenID支持和配置
1. Spring Security專門有一個jar支持OpenID: spring-security-openid-3.2.0.RELEASE.jar

2. 配置
配置超級簡單,只需要openid-login標簽即可,與表單驗證的form-login一樣, 如:
<http>
<intercept-url pattern="/**" access="ROLE_USER" />
<openid-login />
</http>

在openid-login標簽下支持attribute-exanchange標簽來獲取provider提供的用戶信息屬性,如:
<http>
<intercept-url pattern="/**" access="ROLE_USER"/>
<logout/>
<openid-login login-page="/openidlogin.jsp" user-service-ref="registeringUserService"
authentication-failure-url="/openidlogin.jsp?login_error=true">
<attribute-exchange identifier-match="https://www.google.com/.*">
<openid-attribute name="email" type="http://axschema.org/contact/email" required="true" count="1"/>
<openid-attribute name="firstname" type="http://axschema.org/namePerson/first" required="true" />
<openid-attribute name="lastname" type="http://axschema.org/namePerson/last" required="true" />
</attribute-exchange>
<attribute-exchange identifier-match=".*yahoo.com.*">
<openid-attribute name="email" type="http://axschema.org/contact/email" required="true"/>
<openid-attribute name="fullname" type="http://axschema.org/namePerson" required="true" />
</attribute-exchange>
</openid-login>
<remember-me token-repository-ref="tokenRepo"/>
</http>

identifier-match : 用於過濾不同的ID,可以使用正則表達式。
remember-me : 登錄一次後自動在浏覽器增加cookie記住登錄信息,避免下次重新登錄。

可以通過OpenIDAuthenticationToken類的getAttributes方法獲取這些屬性值:
OpenIDAuthenticationToken token =
(OpenIDAuthenticationToken)SecurityContextHolder.getContext().getAuthentication();
List<OpenIDAttribute> attributes = token.getAttributes();

3. 登錄處理
<openid-login/> 登錄處理的默認URL是“/j_spring_openid_security_check”,我們再登錄頁面需要把表單信息提交給這個URL處理;當然我們可以通過login-processing-url這個屬性來更改登錄處理URL。

三、openid-selector

現在有很多網站是OpenID服務提供者,如google、yahoo!等,所以應用可以直接把這些常用的網站都放在登錄頁面上以方便用戶使用,openid-selector框架已經幫大家做好了這個事情,我們可以直接集成它。

集成步驟簡單介紹:
1. 下載openid-selector源碼

https://code.google.com/p/openid-selector/downloads/list

2. 將css、images、js三個目錄拷貝到自己的web目錄下

3. 參考demo.html修改自己的登錄頁面

Spring Security 學習之OpenID認證相關文件下載

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2014年資料/2月/28日/Spring Security 學習之OpenID認證

下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm

Copyright © Linux教程網 All Rights Reserved