spring security2认证详解说明

来源:互联网 发布:python应聘有什么要求 编辑:程序博客网 时间:2024/06/06 05:37


配置登录认证的filter

<!--

       servletspec差不多,处理登陆请求 authenticationFailureUrl定义登陆失败时转向的页面

       defaultTargetUrl定义登陆成功时转向的页面 filterProcessesUrl定义登陆请求的页面

       rememberMeServices用于在验证成功后添加cookie信息

    -->

    <beanid="authenticationProcessingFilter"

       class="com.platform.security.service.filter.UserAuthenticationProcessingFilter">

       <propertyname="authenticationManager"

           ref="authenticationManager"/>

       <propertyname="authenticationFailureUrl">

           <value>/login.jsp?login_error=user_psw_error</value>

       </property>

       <propertyname="filterProcessesUrl">

           <value>/j_spring_security_check</value>

       </property>

       <propertyname="defaultTargetUrl">

           <value>/security/index.do</value>

       </property>

       <propertyname="alwaysUseDefaultTargetUrl">

           <value>true</value>

       </property>

       <propertyname="userManager"ref="userManager"/>

       <propertyname="loginDao"ref="loginLogDao"></property>

       <propertyname="rememberMeServices"ref="rememberMeServices"/>

       <propertyname="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的定义如下:

<beanid="authenticationManager"

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

       <propertyname="providers">

           <list>

              <reflocal="daoAuthenticationProvider"/>

              <reflocal="anonymousAuthenticationProvider"/>

              <reflocal="rememberMeAuthenticationProvider"/>

           </list>

       </property>

       <propertyname="sessionController">

           <refbean="concurrentSessionController"/>

       </property>

    </bean>

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

<reflocal="daoAuthenticationProvider"/>

              <reflocal="anonymousAuthenticationProvider"/>

              <reflocal="rememberMeAuthenticationProvider"/>

是用来进行从数据库里面读取用户等信息进行判断的:

<beanid="daoAuthenticationProvider"

       class="org.springframework.security.providers.dao.DaoAuthenticationProvider">

       <propertyname="userDetailsService"ref="jdbcDaoImpl"/>

       <propertyname="userCache"ref="userCache"/>

       <propertyname="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