Springecurity2.0.4+OpenID完整配置攻略(一)

2014-11-24 01:42:42 · 作者: · 浏览: 4

Spring Security2.04+OpenID配置攻略

目 录

1.QQ互联中的OpenID 3

2. 配置环境 3

3、 Spring Security的xml配置 3

4、 OpenID登录页 17

5、 申请OpenID 账号 17

6、 在本地登录OpenID 18

7、 其他注意事项 19


1.QQ互联中的OpenID

QQ 互联接口中,在QQ登陆成功后,会返回一个OpenID,但是此OpenID是一个大写字母和数字的组合。与一般的OpenID的格式不同,正常的OpenID应该是http开头的URL格式,例如http://zhangsan.openid.org.cn/ ,本文介绍的是国际通用的OpenID认证协议+Spring Security配置过程。

2.配置环境

本文的介绍的配置过程是在OpenJWeb2.63环境下配置的,OpenJWeb2.63集成了S2SH和Spring Security2.0.4,大家也可以自己搭建一个S2SH+Spring Security2.04的环境,在环境搭建好以后,我们需要从网上找到openid4java-0.9.8.jar,放到WEB-INF\lib目录下。有了这个驱动,我们就可以在spring security中进行openID的认证登陆。

另外,我们需要将spring-security-openid-2.0.4.jar放到lib目录中。

在web.xml文件中,我们需要增加一段配置以支持openID认证过滤器:

springSecurityFilterChain

/j_spring_openid_security_check

3、Spring Security的xml配置

   特别注意Spring Security2.0.4的头部配置,因为网上版本众多,有的是以 作为bean标签,有的是以 作为bean标签,如果不设置正确的xml头部,则xml中无法使用像

这种标签。可以参考下面的xml头:

xmlns:p="http://www.springframework.org/schema/p"

xmlns:security="http://www.springframework.org/schema/security"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd"

default-autowire="byType" default-lazy-init="true">

网上有很多pdf格式的spring security配置文档,里面介绍了

使用  就可以引入OpenID认证。

但是经过试验发现,使用  以后,通过spring_openid_security_check进行OpenID认证,则无法使用j_spring_security_check做最常用的本地数据库认证。显然我们希望是两者同时支持。所以我们就不能使用缺省的方式,因此上面的 那段配置可以在xml中删除。

参考现有的authenticationProcessingFilter的配置方式,我们需要增加以下配置来满足openID认证授权:

class="org.springframework.security.util.FilterChainProxy">

CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON

PATTERN_TYPE_APACHE_ANT

/**=httpSessionContextIntegrationFilter,logoutFilter,authenticationProcessingFilter,openIDAuthenticationFilter,securityContextHolderAwareRequestFilter,exceptionTranslationFilter,filterSecurityInterceptor

上面蓝字部分是增加了一个openIDAuthenticationFilter ,下面是增加的这个bean的配置:

class="org.springframework.security.ui.openid.OpenIDAuthenticationProcessingFilter"

p:authenticationManager-ref="authenticationManager"

p:authenticationFailureUrl="/secure/openid.jsp"

p:defaultTargetUrl="/apps/v22/ui/frame/main.jsp"

p:filterProcessesUrl="/j_spring_openid_security_check"

/>

p:authenticationFailureUrl指明认证失败跳转的页面。p:defaultTargetUrl指明认证成功跳转的页面。p:filterProcessesUrl指明openID的认证过滤器URL.

在authenticationManager Bean中,原来有 ,需要增加一个针对OpenID的Provider,见下文(蓝字是针对OpenID增加的Provider):

class="org.springframework.security.providers.ProviderManager"

p:sessionController-ref="concurrentSessionController">

下面我们要设置openidAuthenticationProvider Bean:

class="org.springframework.security.providers.openid.OpenIDAuthenticationProvider">

完整的配置(openjweb中此配置文件为D:\project\openjweb\src\java\applicationContext-security-new.xml):

xmlns:p="http://www.springframework.org/schema/p"

xmlns:security="http://www.springframework.org/schema/security"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xs