Spring Security教程(7)---- 解决UsernameNotFoundException无法被捕获的问题

来源:互联网 发布:客户数据怎么划分 编辑:程序博客网 时间:2024/05/17 08:17

这个教程是我在往项目中一点一点添加 Spring Security的过程的一个笔记,也是我学习 Spring Security的一个过程。

在解决这个问题之前要先说一点authentication-provider默认加载的是DaoAuthenticationProvider类。

完成了上一章的内容后在测试的时候发现在UserDetailsService中抛出的UsernameNotFoundException无法被捕获。于是找到DaoAuthenticationProvider,源码看了好几遍没有看出端倪。然后直接查看最顶级的接口AuthenticationProvider。发现它只有一个方法如下

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. Authentication authenticate(Authentication authentication) throws AuthenticationException;  
抛出AuthenticationException异常,而UsernameNotFoundException是AuthenticationException的子类,那问题应该就出在authenticate这个方法上了。

然后找到DaoAuthenticationProvider的父类AbstractUserDetailsAuthenticationProvider的authenticate方法,发现了这段代码。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. try {  
  2.     user = retrieveUser(username, (UsernamePasswordAuthenticationToken) authentication);  
  3. catch (UsernameNotFoundException notFound) {  
  4.     logger.debug("User '" + username + "' not found");  
  5.   
  6.     if (hideUserNotFoundExceptions) {  
  7.         throw new BadCredentialsException(messages.getMessage(  
  8.                 "AbstractUserDetailsAuthenticationProvider.badCredentials""Bad credentials"));  
  9.     } else {  
  10.         throw notFound;  
  11.     }  
  12. }  
它这里有个hideUserNotFoundExceptions属性,默认是true。这样的话即便我们抛出了UsernameNotFoundException它也会转为BadCredentialsException,所以我们需要将hideUserNotFoundExceptions属性的值设为false,而在上一章中的那种配置方法是没有办法为其属性赋值的所以我们要手动注入.authentication-provider,所以配置就变成了下面的内容

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <sec:authentication-manager>  
  2.     <sec:authentication-provider ref="authenticationProvider" />  
  3. </sec:authentication-manager>  
  4.   
  5. <bean id="authenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">  
  6.     <property name="hideUserNotFoundExceptions" value="false" />  
  7.     <property name="userDetailsService" ref="userDetailService" />  
  8.     <property name="userCache" ref="userCache" />  
  9.     <property name="messageSource" ref="messageSource" />  
  10.     <property name="passwordEncoder" ref="passwordEncode" />  
  11.     <property name="saltSource" ref="saltSource" />  
  12. </bean>  
  13.   
  14.   
  15. <!-- 配置密码加密类 -->  
  16. <bean id="passwordEncode" class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" />  
  17. <bean id="saltSource" class="org.springframework.security.authentication.dao.ReflectionSaltSource">  
  18.     <property name="userPropertyToUse" value="username"/>  
  19. </bean>  

注意:如果在authentication-provider配置中用ref指定AuthenticationProvider则authentication-provider的子元素将都不可以用。

即下面的这种配置是错误的

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <sec:authentication-manager>  
  2.     <sec:authentication-provider ref="authenticationProvider" >  
  3.         <sec:password-encoder ref="passwordEncode">  
  4.             <sec:salt-source user-property="username"/>  
  5.         </sec:password-encoder>  
  6.     </sec:authentication-provider>  
  7. </sec:authentication-manager>  
所以我们的盐值加密就需要注入到AuthenticationProvider中了。

SaltSource是一个接口有两个实现类SystemWideSaltSource和ReflectionSaltSource。

SystemWideSaltSource :只能指定固定值

ReflectionSaltSource:可以指定UserDetails的属性,这里我们用的就是它

这样的话就可以保证在抛出UsernameNotFoundException时,前台能显示出来错误信息,如下所示。


在上一章中忘了介绍如何在前台显示登录是的异常信息,在这里补上。

UsernamePasswordAuthenticationFilter认证失败后,异常信息会写到Session中,key为SPRING_SECURITY_LAST_EXCEPTION

可以通过El表达式来获取到异常的信息。

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ${sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message}  

0 0
原创粉丝点击