Spring Security教程(7)---- 解决UsernameNotFoundException无法被捕获的问题
来源:互联网 发布:客户数据怎么划分 编辑:程序博客网 时间:2024/05/17 08:17
这个教程是我在往项目中一点一点添加 Spring Security的过程的一个笔记,也是我学习 Spring Security的一个过程。
在解决这个问题之前要先说一点authentication-provider默认加载的是DaoAuthenticationProvider类。
完成了上一章的内容后在测试的时候发现在UserDetailsService中抛出的UsernameNotFoundException无法被捕获。于是找到DaoAuthenticationProvider,源码看了好几遍没有看出端倪。然后直接查看最顶级的接口AuthenticationProvider。发现它只有一个方法如下
- Authentication authenticate(Authentication authentication) throws AuthenticationException;
然后找到DaoAuthenticationProvider的父类AbstractUserDetailsAuthenticationProvider的authenticate方法,发现了这段代码。
- try {
- user = retrieveUser(username, (UsernamePasswordAuthenticationToken) authentication);
- } catch (UsernameNotFoundException notFound) {
- logger.debug("User '" + username + "' not found");
- if (hideUserNotFoundExceptions) {
- throw new BadCredentialsException(messages.getMessage(
- "AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
- } else {
- throw notFound;
- }
- }
- <sec:authentication-manager>
- <sec:authentication-provider ref="authenticationProvider" />
- </sec:authentication-manager>
- <bean id="authenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
- <property name="hideUserNotFoundExceptions" value="false" />
- <property name="userDetailsService" ref="userDetailService" />
- <property name="userCache" ref="userCache" />
- <property name="messageSource" ref="messageSource" />
- <property name="passwordEncoder" ref="passwordEncode" />
- <property name="saltSource" ref="saltSource" />
- </bean>
- <!-- 配置密码加密类 -->
- <bean id="passwordEncode" class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" />
- <bean id="saltSource" class="org.springframework.security.authentication.dao.ReflectionSaltSource">
- <property name="userPropertyToUse" value="username"/>
- </bean>
注意:如果在authentication-provider配置中用ref指定AuthenticationProvider则authentication-provider的子元素将都不可以用。
即下面的这种配置是错误的
- <sec:authentication-manager>
- <sec:authentication-provider ref="authenticationProvider" >
- <sec:password-encoder ref="passwordEncode">
- <sec:salt-source user-property="username"/>
- </sec:password-encoder>
- </sec:authentication-provider>
- </sec:authentication-manager>
SaltSource是一个接口有两个实现类SystemWideSaltSource和ReflectionSaltSource。
SystemWideSaltSource :只能指定固定值
ReflectionSaltSource:可以指定UserDetails的属性,这里我们用的就是它
这样的话就可以保证在抛出UsernameNotFoundException时,前台能显示出来错误信息,如下所示。
在上一章中忘了介绍如何在前台显示登录是的异常信息,在这里补上。
UsernamePasswordAuthenticationFilter认证失败后,异常信息会写到Session中,key为SPRING_SECURITY_LAST_EXCEPTION
可以通过El表达式来获取到异常的信息。
- ${sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message}
- Spring Security教程(7)---- 解决UsernameNotFoundException无法被捕获的问题
- Spring Security教程(7)---- 解决UsernameNotFoundException无法被捕获的问题
- Spring Security教程(7)---- 解决UsernameNotFoundException无法被捕获的问题
- Spring Security教程(7)---- 解决UsernameNotFoundException无法被捕获的问题
- 今日发现的Datagrid控件[WEB]ItemCommand事件无法被捕获的问题。
- 异常捕获不抛出,Spring事务无法回滚 (默认spring只在发生未被捕获的runtimeexcetpion时才回滚。)
- 解决在项目里引入Spring Security后iframe或者frame所引用的页无法显示的问题
- 解决Spring Security 表单上传文件CSRF失效的问题
- 解决Spring Security导致的前后台传值乱码问题
- Springboot通过cors解决跨域问题(解决spring security oath2的/oauth/token跨域问题)
- 解决getCurrentSession()spring无法注入SessionFactory的问题
- 解决WebSocket无法依赖注入的问题(Spring)
- 利用spring-security解决CSRF问题
- Spring Security教程(4)---- 数据库表结构的创建
- Spring Security教程(13)---- 验证码功能的实现
- Spring Security教程(4)---- 数据库表结构的创建
- Spring Security教程(13)---- 验证码功能的实现
- Spring Security教程(4)---- 数据库表结构的创建
- KMP算法实现
- CGridView: Render customized/complex datacolumns cgridview datacolumn value调用 controller 和 model里的函数
- mysql 用户和权限分配
- 用ajax返回验证的时候总是弹出error原因
- Spring Security教程(6)---- 使用数据库管理用户及权限
- Spring Security教程(7)---- 解决UsernameNotFoundException无法被捕获的问题
- 第五章 采用SVM和神经网络的车牌识别(流程图及详细解释)
- mysql root没有权限的问题解决
- Spring Security教程外篇(1)---- AuthenticationException异常详解
- Redis Pub发布/sub订阅实现机制
- Spring Security教程(8)---- 自定义决策管理器及修改权限前缀
- Mincost
- Spring Security教程(9)---- 自定义AccessDeniedHandler
- 时间过的悄无声息,我却依然没发觉