【问题标题】:How to handle "Could not get JDBC Connection exception" in Spring Security如何在 Spring Security 中处理“无法获取 JDBC 连接异常”
【发布时间】:2015-02-06 08:38:23
【问题描述】:

我是第一次实现 Spring Security 认证和授权。

当 Spring Security 通过执行以下查询使用 JDBC-USER-SERVICE 进行身份验证时

select username, password, status as enabled from bbp_user where username=?

我收到以下异常

2014-12-08 21:23:23,852 [http-bio-8090-exec-4] DEBUG org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter  - 
Authentication request failed: 
org.springframework.security.authentication.AuthenticationServiceException: **Could not get JDBC 
Connection**; nested exception is org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create 
PoolableConnectionFactory (Io exception: **The Network Adapter could not establish the connection)

请查找安全 XML 部分

<form-login 
        login-page="/inventory/auth/login" 
        default-target-url="/inventory/landing/loadDashBoardPage"
        authentication-failure-url="/inventory/auth/login?error"
        username-parameter="username" 
        password-parameter="password" />

<authentication-manager>
    <authentication-provider>

        <jdbc-user-service data-source-ref="dataSource"
            users-by-username-query="select username, password, status as enabled from bbp_user where username=?"
            />
    </authentication-provider>
</authentication-manager>

在使用用户名和密码进行身份验证失败时,我已设计重新发布登录页面,并显示“用户名或密码无效”等错误消息。

但是,如何将 JDBC 类型的底层异常重定向到 web.xml 中提到的特定错误页面

<!-- Application Exceptions -->
<error-page>
    <!-- Route all exceptions to error page -->
    <exception-type>java.lang.Throwable</exception-type>
    <location>/Dashboard/jsp/error.jsp</location>
</error-page>

我正在使用 Spring 3.0.5 和 Spring Security 3.1.0 版,仅供参考。

请指教。提前致谢!!

【问题讨论】:

  • 你使用的是哪个版本的 Spring?
  • Spring 3.0.5 和 Spring Security 3.1.0

标签: spring security jdbc


【解决方案1】:

有几种方法可以做到这一点。传统的做法是在 web.xml 中

<error-page>
    <error-code>500</error-code> <!-- All Internal Server Errors -->
    <location>/error.htm</location>
</error-page>

现在用@RequestMapping("/error") 定义一个控制器,类似于:

 @RequestMapping("/error")
    public String handle500()
    {
      return "internalServer";
    }

internalServer定义一个视图。

另一种方法是在 Spring 安全配置中进行。 authentication-failure-url 属性。这意味着登录过程中的任何异常都会将您带到此属性中定义的 URL。然后定义一个视图对应authentication-failure-url中指定的URL

您使用的是旧版本的 Spring。从 Spring v3.2 开始,有一个叫做 @ControllerAdvice 的东西,它简化了 MVC 中的异常处理。

【讨论】:

  • 目前,在我的安全 xml 文件中,我包含了 authentication-failure-url。这是工作。 “未找到用户名”、“密码错误”等身份验证失败应转到 authentication-failure-url 属性中提到的 URL。但是对于身份验证过程中的“无法建立JDBC连接”等意外的底层异常,我想从web.xml重定向到我在中提到的页面。但它现在没有发生。目前,对于身份验证期间的任何异常,spring security 会将其重定向到我在 authentication-failure-url 中定义的页面。
  • 我明白按照你的建议,我们可以写一个Controller来处理。 Spring Security 如何区分异常并相应地将其重定向到页面?请多多指教。
  • Spring 安全不这样做。当出现异常时,在您的 web.xml 中,按照定义,您将被重定向到 /error.htm。此请求将由您定义的带有@RequestMapping("/error") 注释的控制器之一处理。从此控制器,您可以将请求重定向到错误页面。查看答案的第一部分
  • 在上面的链接中,他们专门处理了 UsernameNotFoundException、DisabledException。同样,我们不能再有一个 if 条件来匹配 org.springframework.security.authentication.AuthenticationServiceException 我在日志中得到的并将其重定向到 web.xml 中定义的错误页面吗??
猜你喜欢
  • 2022-12-11
  • 2016-01-07
  • 1970-01-01
  • 1970-01-01
  • 2016-01-20
  • 1970-01-01
  • 1970-01-01
  • 2012-10-25
  • 1970-01-01
相关资源
最近更新 更多