一、前言
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一样, 如:
1
2
3
4
|
<
http
>
<
intercept-url
pattern
=
"/**"
access
=
"ROLE_USER"
/>
<
openid-login
/>
</
http
>
|
在openid-login标签下支持attribute-exanchange标签来获取provider提供的用户信息属性,如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<
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方法获取这些属性值:
1
2
3
|
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修改自己的登录页面
本文转自sarchitect 51CTO博客,原文链接:http://blog.51cto.com/stevex/1358753,如需转载请自行联系原作者