SpringSecurity登陆错误处理

来源:互联网 发布:淘宝不能延长收货 编辑:程序博客网 时间:2024/06/02 04:39

1、在登录的时候出现错误时抛出的异常AuthenticationException异常详解

    UsernameNotFoundException 用户找不到

    BadCredentialsException 坏的凭据

    AccountStatusException用户状态异常它包含如下子类

    AccountExpiredException 账户过期

    LockedException账户锁定

    DisabledException账户不可用

    CredentialsExpiredException证书过期

 

2、UsernameNotFoundException异常信息捕获不到 

   通常情况下,抛UsernameNotFoundException异常信息是捕捉不了,跟踪源码后spring-security-core.jar发现

AbstractUserDetailsAuthenticationProvider类

 

            try {                user = retrieveUser(username, (UsernamePasswordAuthenti<wbr>cationToken) authentication);            } catch (UsernameNotFoundExceptio<wbr>n notFound) {                logger.debug("User '" + username + "' not found");                if (hideUserNotFoundExceptio<wbr>ns) {                    throw new BadCredentialsException(messages.getMessage(                            "AbstractUserDetailsAuthe<wbr>nticationProvider.badCredentials", "Bad credentials"));                } else {                    throw notFound;                }            }</wbr></wbr></wbr></wbr>

 

 

而默认情况下,hideUserNotFoundExceptions为true。所以就会导致明明抛UsernameNotFoundException,但前台还是只能捕获Badcredentials的问题。

 

解决办法我们可以直接覆盖org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider的类,然后修改hideUserNotFoundExceptions为false。

 

当然,这样的解决办法并不好。所以,我们还是走正规的途径,自定义org.springframework.security.authentication.dao.DaoAuthenticationProvider来替换默认的即可,即修改配置文件并定义provider,这就是IoC的伟大之处。

 

原来authentication-manager中简单的定义user-service-ref

 

 

                                                                                                

 

 

 

现在修改如下:

 

 

 

                                                                                                        

 

 

 

                                                                                                

  

 

这样修改后,在登录页面获取的异常已经是自己抛出去的UsernameNotFoundException了。

 

3、国际化异常信息

 
    
        
    
   

 
    
        
    

0 0
原创粉丝点击