您的位置:首页 > 编程语言 > Java开发

spring security2认证详解说明

2012-04-27 11:50 363 查看
配置登录认证的filter

<!--
       和servlet
spec差不多,处理登陆请求 authenticationFailureUrl定义登陆失败时转向的页面
       defaultTargetUrl定义登陆成功时转向的页面 filterProcessesUrl定义登陆请求的页面
       rememberMeServices用于在验证成功后添加cookie信息
    -->
    <bean
id="authenticationProcessingFilter"
       class="com.platform.security.service.filter.UserAuthenticationProcessingFilter">
       <property
name="authenticationManager"
           ref="authenticationManager"
/>
       <property
name="authenticationFailureUrl">
           <value>/login.jsp?login_error=user_psw_error</value>
       </property>
       <property
name="filterProcessesUrl">
           <value>/j_spring_security_check</value>
       </property>
       <property
name="defaultTargetUrl">
           <value>/security/index.do</value>
       </property>
       <property
name="alwaysUseDefaultTargetUrl">
           <value>true</value>
       </property>
       <property
name="userManager"ref="userManager"
/>
       <property
name="loginDao"ref="loginLogDao"></property>
       <property
name="rememberMeServices"ref="rememberMeServices"
/>
       <property
name="exceptionMappings">
           <value>
              org.springframework.security.AuthenticationException=/login.jsp?login_error=user_psw_error
              org.springframework.security.concurrent.ConcurrentLoginException=/login.jsp?login_error=too_many_user_error
           </value>
       </property>
    </bean>

 

在这个bean中定义了authenticationManager,具体的是否能够登录的判断就是由authenticationManager来进行的:

authenticationManager的定义如下:

<bean
id="authenticationManager"
       class="org.springframework.security.providers.ProviderManager">
       <property
name="providers">
           <list>
              <ref
local="daoAuthenticationProvider"/>
              <ref
local="anonymousAuthenticationProvider"/>
              <ref
local="rememberMeAuthenticationProvider"/>
           </list>
       </property>
       <property
name="sessionController">
           <ref
bean="concurrentSessionController"/>
       </property>
    </bean>

在这个中定义了具体的认证管理器实现,而其中的

<ref
local="daoAuthenticationProvider"/>
              <ref
local="anonymousAuthenticationProvider"/>
              <ref
local="rememberMeAuthenticationProvider"/>
是用来进行从数据库里面读取用户等信息进行判断的:

<bean
id="daoAuthenticationProvider"
       class="org.springframework.security.providers.dao.DaoAuthenticationProvider">
       <property
name="userDetailsService"ref="jdbcDaoImpl"
/>
       <property
name="userCache"ref="userCache"
/>
       <property
name="passwordEncoder"ref="passwordEncoder"
/>
    </bean>

 

DaoAuthenticationProvider类继承了AbstractUserDetailsAuthenticationProvider类,而进行具体判断的时候,是调用AbstractUserDetailsAuthenticationProvider类中的public
Authenticationauthenticate(Authentication authentication) throws AuthenticationException方法,在该方法中首先会从缓存中去获取用户信息:

UserDetails user = this.userCache.getUserFromCache(username);

如果没有获取到,那么就在调用方法:

user = retrieveUser(username,(UsernamePasswordAuthenticationToken) authentication);去获取用户,在这个方法中会调用在xml文件中配置的userDetailsService来实现获取用户,所以这个userDetailsService也是可以进行自定义的,默认是使用jdbcDaoImpl。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息