0

i'm using acegi authentication for login . when i try to compare the values from databade i'm using the following class

 @Transactional(readOnly = true)
    @Service
    public class CustomUserDetailsService implements UserDetailsService {
        @Autowired
        private UserDAO userdao;
    public UserDetails loadUserByUsername(String username)
                throws UsernameNotFoundException, DataAccessException {
com.quest.model.dto.User dbUser = userdao.searchDatabase(username);
    }
    }

but the autowired field is not initializing. But it initialized in the controller class. This class is mapped through spring-security XML

<

    ?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:security="http://www.springframework.org/schema/security"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/security 
                http://www.springframework.org/schema/security/spring-security.xsd
                http://www.springframework.org/schema/mvc 
                http://www.springframework.org/schema/mvc/spring-mvc.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

        <!-- This is where we configure Spring-Security  -->
        <!-- <context:annotation-config />
        <context:component-scan base-package="com.quest" /> 
        <mvc:annotation-driven /> --> 
        <security:http auto-config="true" use-expressions="true" access-denied-page="/auth/denied" >

            <security:intercept-url pattern="/auth/home" access="permitAll"/>
            <security:intercept-url pattern="/admin/*" access="hasRole('ROLE_ADMIN')"/>
            <security:intercept-url pattern="/student/*" access="hasRole('ROLE_STUDENT')"/>
            <security:intercept-url pattern="/control/*" access="hasRole('ROLE_TEACHER')"/>

            <!-- <security:form-login
                    login-page="/auth/home" 
                    authentication-failure-url="/auth/home?error=true" 
                    default-target-url="/control/dummyLogin"/> -->
            <security:form-login
                    login-page="/auth/home" 
                    authentication-failure-url="/auth/home?error=true" 
                    default-target-url="/student/student"/>

            <security:logout 
                    invalidate-session="true" 
                    logout-success-url="/auth/home" 
                    logout-url="/student/logout"/>

        </security:http>

        <!-- Declare an authentication-manager to use a custom userDetailsService -->
        <security:authentication-manager>
                <security:authentication-provider user-service-ref="customUserDetailsService">
                        <security:password-encoder ref="passwordEncoder"/>
                </security:authentication-provider>
        </security:authentication-manager>

        <!-- Use a Md5 encoder since the user's passwords are stored as Md5 in the database -->
        <bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" id="passwordEncoder"/>

        <!-- A custom service where Spring will retrieve users and their corresponding access levels  -->
        <bean id="customUserDetailsService" class="com.quest.service.CustomUserDetailsService"/>


    </beans>

this is spring-servlrt.xml

<?xml  version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:ws="http://jax-ws.dev.java.net/spring/core"   
    xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"   
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://jax-ws.dev.java.net/spring/core http://jax-ws.java.net/spring/core.xsd
        http://jax-ws.dev.java.net/spring/servlet http://jax-ws.java.net/spring/servlet.xsd">


    <context:annotation-config />
    <context:component-scan base-package="com.quest.*" />   
    <mvc:annotation-driven /> 
    <bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
      <property name="baseAddress" value="http://localhost:9999/"/>
    </bean>

    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" />

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan">
            <list>
                <value>com.quest.model.dto</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.configurationClass">org.hibernate.cfg.AnnotationConfiguration</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven />

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager" depends-on="sessionFactory">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

<!--    <bean id="jacksonMessageConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean> !-->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jacksonMessageConverter" />
            </list>
        </property>
    </bean>


     <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="com.quest.data.converter.HibernateAwareObjectMapper" />
                </property>
            </bean>

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="200000" />
    </bean>

    <bean id="seedDataInstaller" 
        class="com.quest.model.SeedData"  init-method="populateSeed">
    </bean>

</beans>

and the userDAO looks like

package com.quest.model.dao; @Repository("userdao") public class UserDAO { ...}
Akhil
  • 83
  • 4
  • 14

3 Answers3

0

No bean named 'userdao' is defined

you have to define either UserDao in xml configuration as a bean or annotate UserDao class with @component

if you are following the annotation based config then please uncomment the following portion in your config xml

<!-- 
<context:annotation-config />
<context:component-scan base-package="com.quest.*>" /> 
<mvc:annotation-driven /> 
--> 
RPaul
  • 946
  • 10
  • 11
0

Your web.xml is creating two Spring application contexts. Let's call them Security (after spring-security.xml) and Servlet (after spring-servlet.xml). Security is created by the ContextLoaderListener listener, and Servlet is created by the DispatcherServlet servlet. Security is the parent of Servlet. That means that the beans in Security are only visible to other beans in Security, and the beans in Servlet can see both the beans in Security and in Servlet.

You're defining the CustomUserDetailsService (CUDS) bean in Security, and the UserAccountDao and UserDao beans in Servlet, so the CUDS bean can't see them.

  • That's good reasoning for the background of the problem. Could you please extend your answer to include concrete steps on how to actually solve the problem? – avalancha Apr 07 '16 at 11:17
0

I got this solved by adding the UserDAO in spring config file

<bean id="userDAOImpl" class="com.qvantel.dao.UserDAOImpl"/>

check in db-config.xml

My config files

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

  <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml, /WEB-INF/spring/appServlet/db-config.xml, /WEB-INF/spring/appServlet/spring-security.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>QRTServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/appServlet/webconfig-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>QRTServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <session-config>
        <session-timeout>60</session-timeout>
    </session-config>

</web-app>

webconfig-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee https://www.springframework.org/schema/jee/spring-jee-4.0.xsd">

    <mvc:annotation-driven conversion-service="conversionService" />
    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean" >
        <property name="converters">
            <set>
                <bean class="com.qvantel.conveter.JobConverter"/>
                <!-- 
                add all the converters here
                <bean class="com......Converter"/> -->
            </set>
        </property>
    </bean>
    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <context:component-scan base-package="com.qvantel.web" />
    <!-- <context:component-scan base-package="com.qvantel.web" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan> -->
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- max upload size in bytes -->
        <property name="maxUploadSize" value="20971520" /> <!-- 20MB -->

        <!-- max size of file in memory (in bytes) -->
        <property name="maxInMemorySize" value="1048576" /> <!-- 1MB -->
    </bean>

     <!-- Tiles configuration -->

    <bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles/tiles-definitions.xml</value>
            </list>
        </property>
    </bean>
</beans>

db-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee https://www.springframework.org/schema/jee/spring-jee-4.0.xsd">

    <context:component-scan base-package="com.qvantel.dao, com.qvantel.service" />
    <!-- <context:component-scan base-package="com.qvantel">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="regex" expression="com.qvantel.*"/>
    </context:component-scan> -->
     <jee:jndi-lookup id="qvantelDataSource" expected-type="javax.sql.DataSource" jndi-name="jdbc/Qvantel_DB" /> 

     <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="qvantelDataSource" />
        </property>
        <property name="annotatedClasses">
            <list>
                <value>com.qvantel.model.User</value>
                <value>com.qvantel.model.Candidate</value>
                <value>com.qvantel.model.Job</value>
                <value>com.qvantel.model.ProfilesScreened</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">false</prop>
            </props>
        </property>
    </bean>
    <tx:annotation-driven />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <bean id="userDAOImpl" class="com.qvantel.dao.UserDAOImpl"/>
</beans>

spring-security.xml

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

    <!-- <context:component-scan base-package="com.qvantel" />   -->
    <security:http auto-config="true" use-expressions="true" >
        <security:intercept-url pattern="/" access="permitAll" />
        <security:intercept-url pattern="/viewJobs" access="hasRole('ADMIN')" />
        <security:intercept-url pattern="/viewCandidates" access="hasRole('ADMIN')" />
        <!-- <intercept-url pattern="/home" access="permitAll" />
        <intercept-url pattern="/admin**" access="hasRole('ADMIN')" />
        <intercept-url pattern="/dba**" access="hasRole('ADMIN') and hasRole('DBA')" /> -->
        <security:form-login  login-page="/login"
                     login-processing-url="/login"
                     default-target-url="/home"
                     authentication-failure-url="/login?error"
                     username-parameter="userName"
                     password-parameter="password" />
        <security:logout logout-success-url="/login?logout" logout-url="/j_spring_security_logout" />
        <security:csrf disabled="true"/>
    </security:http>

    <security:authentication-manager >
        <security:authentication-provider user-service-ref="customUserDetailsService">
           <!-- <security:password-encoder ref="bcryptEncoder"/> -->
           <security:password-encoder hash="bcrypt" />
        </security:authentication-provider>   
    </security:authentication-manager>

    <beans:bean id="bcryptEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />  
    <beans:bean id="customUserDetailsService" class="com.qvantel.service.CustomUserDetailsService" />

</beans:beans>

Used @Repositoryon DAOImpl classes, @Service on service classes and @Controller on controller classes

Used @Transactional on all DAOImpl methods where ever required