Spring-Security登录密码验证过程(UsernamePasswordAuthenticationFilter)

Spring-Security主要是一个由一堆Filter组成的过滤器链,每个Filter做自己的事情。今天我跟一下登录的密码认证过程,主要是UsernamePasswordAuthenticationFilter这个类

1.web.xml中配置security

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


 

2. spring-security.xml  这里定义了一个ValidateCodeAuthenticationFilter,这个filter继承自   UsernamePasswordAuthenticationFilter,在做认证的时候调用UsernamePasswordAuthenticationFilter中的attemptAuthentication方法

<beans:bean id="validateCodeAuthenticationFilter" class="com.*.interceptors.ValidateCodeAuthenticationFilter">  
   <beans:property name="authenticationSuccessHandler" ref="customAuthenticationSuccessHandler"></beans:property>
   <beans:property name="authenticationFailureHandler" ref="customAuthenticationFailureHandler"></beans:property>
   <beans:property name="authenticationManager" ref="authenticationManager"></beans:property>
   <beans:property name="filterProcessesUrl" value="/logincheck"></beans:property>
   <beans:property name="usernameParameter" value="username"></beans:property>
   <beans:property name="passwordParameter" value="password"></beans:property>
   <beans:property name="allowEmptyValidateCode" value="${webLogin.allowEmptyValidateCode}" />
</beans:bean>

UsernamePasswordAuthenticationFilter中的attemptAuthentication方法将信息赋值给UsernamePasswordAuthenticationToken(authRequest),然后调用getAuthenticationManager().authenticate(authRequest)对用户密码的正确性进行验证,认证失败就抛出异常,成功就返回Authentication对象。

 AuthenticationManager接口的实现类ProviderManager:

 AuthenticationProvider接口的实现类AbstractUserDetailsAuthenticationProvider

改抽象方法的实现在 DaoAuthenticationProvider类

此处调用的loadUserByUsername即到达自定义实现的UserDetailService的loadUserByUsername,实现根据登录名查询数据库,返回UserDetails;

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值