Saravanan Sivaji
Saravanan Sivaji

Reputation: 45

How to handle "Could not get JDBC Connection exception" in Spring Security

I am implementing Spring Security Authentication and Authorization for the first time.

When Spring Security does authentication with JDBC-USER-SERVICE by executing the following query

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

I am getting the following exception

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)

Please find the part of security 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>

Upon authentication failure with username and password, I have designed to re-post the login page with error message like "invalid username or password".

But, How to redirect JDBC kind of underlying exception to the specific error page as mentioned in the 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>

I am using Spring 3.0.5 and Spring Security version 3.1.0, fyi.

Please advise. Thanks in Advance!!

Upvotes: 0

Views: 1394

Answers (1)

Bhaskara
Bhaskara

Reputation: 601

There are couple of ways doing this. The traditional way of doing it is in web.xml

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

Now define a Controller with @RequestMapping("/error") similar to this:

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

Have a view defined for internalServer.

The other way would be doing it in Spring security configurations. The authentication-failure-url attribute. This means that any exception during the login process will take you the URL defined in this attribute. Then define a view corresponding to the URL specified in the authentication-failure-url

You are on a little older version of Spring. From Spring v3.2 there is something called as @ControllerAdvice which simplifies the Exception handling in MVC.

Upvotes: 0

Related Questions