1

I am implementing spring security in an existing spring mvc project. I had used xml to configure the spring security. I have used this tutorial for implementing spring security http://www.mkyong.com/spring-security/spring-security-form-login-using-database/

In my project I have a db-source file(MySQL_Datasource.xml) in resources folder just under main (outside of webapp). And the way spring security is implemented in tutorial, the datasource needs to be under webapp folder. I am facing this problem of integration.

Below is the snap of my project structure and on the right side config. code of web.xml, I have commented on the line in image where i have to define my dataSource location.

web.xml

This is code of spring security where dataSource will be used

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    <!-- enable use-expressions -->
    <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />

        <!-- access denied page -->
        <access-denied-handler error-page="/403" />
        <form-login 
            login-page="/login" 
            default-target-url="/welcome" 
            authentication-failure-url="/login?error" 
            username-parameter="usr"
            password-parameter="pwd" />
        <logout logout-success-url="/login?logout"  />
        <!-- enable csrf protection -->
        <csrf/>
    </http>

    <!-- Select users and user_roles from database -->
    <authentication-manager>
        <authentication-provider>
            <jdbc-user-service data-source-ref="dataSource"
                users-by-username-query=
                    "select username,password, enabled from users where username=?"
                authorities-by-username-query=
                    "select username, role from user_roles where username =?  " />
        </authentication-provider>
    </authentication-manager>

</beans:beans>

I am doing this first time. I need help so that I can get this done.

UPDATE:

MYSQL_DataSource.xml code:

<bean id="dataSource" class= "org.springframework.jdbc.datasource.DriverManagerDataSource">      
      <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
      <property name="url" value="${jdbc.url}"></property>
      <property name="username" value="${jdbc.username}"></property>
      <property name="password" value="${jdbc.password}"></property>
   </bean>

   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
     <property name="location">
        <value>db.properties</value>
     </property>
  </bean>

and below is the db.properties values:

jdbc.url        =   jdbc:mysql://localhost/bhaiyag_prod_grocery
jdbc.username   =   newuser
jdbc.password   =   kmsg
RishiPandey
  • 212
  • 1
  • 5
  • 22
  • What's your errors – bmarkham Dec 28 '16 at 07:23
  • Thanks for your time. You can see in web.xml code at line No. 23 in the image. I have to give the path of my dataSource under so that dataSource can be used in my spring-security.xml. I am facing problem of giving path. so either i am not giving path properly or i have gone wrong way of implementation – RishiPandey Dec 28 '16 at 07:28
  • Right, but what problems have you ran into using your method – bmarkham Dec 28 '16 at 07:30
  • my dataSource can not be found (because i have not given correct path). when i move my dataSource under webapp Spring security works but i can't do this because all my beans are defined in dataSource and i have to use them in application context – RishiPandey Dec 28 '16 at 07:33
  • Can you post your stacktrace – bmarkham Dec 28 '16 at 07:35
  • Please show the complete erro stacktrace. – KayV Dec 28 '16 at 08:18

2 Answers2

2

If your project is correctly configured, src/main/resources folder will be packaged during project build under WEB-INF/classes.

So, if maven configuration or deployment-assembly section in project/properties is Ok, the path that you should use in your web.xml is like this:

<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>
   /WEB-INF/groceryapp-servlet.xml
   /WEB-INF/spring-security.xml
   /WEB-INF/classes/MySQL_DataSource.xml
 </param-value>    
</context-param>

It should work this way.

Once it works, have a look at this question and answers spring-scheduler-is-executing-twice and this one too web-application-context-root-application-context-and-transaction-manager-setup. In many of the Mkyong's tutorials the application context is loading twice, and I'm pretty sure it would happen the same with your project once it starts working.

As your groceryapp-servlet.xml is already loaded by Spring MVC's dispatcher servlet, you could try just removing it from contextConfigLocation setting, just this way:

<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>
   /WEB-INF/spring-security.xml
   /WEB-INF/classes/MySQL_DataSource.xml
 </param-value>    
</context-param>

Properties loading problem:

To load correctly the db.properties, try this config in DB config xml:

 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
     <property name="location">
        <value>classpath:/db.properties</value>
     </property>
  </bean>
Community
  • 1
  • 1
jlumietu
  • 6,234
  • 3
  • 22
  • 31
  • Thanks for your Answer. I had update my code and now my dataSouce is giving fileNotFoundException for db.properties file. when i hard coded the db entries and removed the db.properties entry from dataSource there is no error shown by compiler. But my ajax services are giving 405: post method not supported – RishiPandey Dec 28 '16 at 09:13
  • Ok i solved this post method not supported problem. But i do need help that why my MySQL_DataSource.xml not accessing db.properties – RishiPandey Dec 28 '16 at 09:27
  • You should post your MySQL_DataSource.xml at least to see what is happening – jlumietu Dec 28 '16 at 09:43
  • See the updates section in question. I have added the related code of dataSource and db.properties – RishiPandey Dec 29 '16 at 08:27
  • @RishiPandey, have a look at my answer in "Properties loading problem" – jlumietu Dec 29 '16 at 09:57
  • Thanks this works too. I need one more help. In the authentication manager section why can't i change the query of selecting rows – RishiPandey Dec 29 '16 at 11:23
  • when i try to add few more fields in query like name and contactNo, it fails. i need this data to add in session attribute – RishiPandey Dec 29 '16 at 11:25
  • You won't be able to do it like this. The `jdbc-user-service` queries only can take an output like you have in your question. If you need more data to be retrieved from database, there are a few alternatives. Let me think for a moment which is the easiest one... – jlumietu Dec 29 '16 at 11:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131788/discussion-between-rishipandey-and-jlumietu). – RishiPandey Dec 29 '16 at 11:30
0

You can also specify context location relatively to current classpath. Make sure the resources folder is on your classpath and if it is. Then you can load the configuration file in your resources folder like,

<context-param>
    <param-value>classpath:MySQL_DataSource.xml</param-value>
</context-param>
Lucky
  • 16,787
  • 19
  • 117
  • 151