整合shiro时,登录请求无法捕捉UnknownAccountException

来源:互联网 发布:linux hba卡 编辑:程序博客网 时间:2024/04/27 17:47

代码写累了,关于整合shiro,写个文章记录下一个简单但是困扰了我好久的问题:无论登陆时包什么异常,在controll中获取的都是异常的父类AuthenticationException。

框架使用ssm,在整合shiro时,自定义一个realm,命名为MyAuthorRealm,代码和网上大部分一样,主要是配置部分出了问题,老配置如下:

(贴了半天代码。。。。这个编辑器也是够了)

<!-- Shiro安全管理器 --><bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"><property name="realms"><list><ref bean="jdbcRealm"/><ref bean="myAuthorRealm"/></list></property><property name="cacheManager" ref="cacheManager"></property></bean>

这里配置了两个realm,按照顺序,shiro验证登录用户的时候会先走jdbcRealm,这时如果在MyAuthorRealm抛出throw new UnknownAccountException("用户不存在");,当捕获时却变成了它的父类AuthenticationException,在这里既然自定义了验证类MyAuthorRealm,就无需再用jdbcRealm了,去掉它,就可以正常捕获异常了:

try {subject.login(token);} catch (UnknownAccountException ex) {map.put("message","用户名没有找到");return new ModelAndView("/admin/login",map);} catch (IncorrectCredentialsException ex) {map.put("message","用户名密码不匹配");return new ModelAndView("/admin/login",map);}catch (AuthenticationException e) {map.put("message","其他的登录错误");return new ModelAndView("/admin/login",map);}


阅读全文
0 0