手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Spring Security 学习之OpenID认证

[日期:2014-02-28] 来源:Linux社区  作者:stevex [字体: ]

一、前言

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框架已经帮大家做好了这个事情,我们可以直接集成它。

Spring Security 学习之OpenID认证

集成步骤简单介绍:
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

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款