Spring Security a odczyt userow z bazy

0

mam problem z odczytem i zalogowaniem sie jako user, ktorego username i password sa w bazie. Problem dzieje sie juz na poczatku. Korzystam z wzorca Spring MVC i polaczenia z baza za pomoca Hiberneta. Moze zapodam jak wygladaja moje pliki:

spring-security.xml

 
<http auto-config="true" use-expressions="true">
		<intercept-url pattern="/admins**" access="hasRole('ROLE_ADMIN')" />
		<form-login 
		    login-page="/logins" 
		    default-target-url="/admins" 
			authentication-failure-url="/logins?error" 
			username-parameter="username"
			password-parameter="password" />
		<logout logout-success-url="/logins?logout" />
		<!-- enable csrf protection -->
		<csrf/>
	</http>
 
	<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>

dupadupa-servlet.xml

 
<bean id="dataSource"
        class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"
        p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" 
        p:username="${jdbc.username}"
        p:password="${jdbc.password}"></bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

i teraz po odpaleniu Tomcata krzyczy bledem:

 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0': Cannot resolve reference to bean 'org.springframework.security.authentication.ProviderManager#0' while setting bean property 'authenticationManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.ProviderManager#0': Cannot resolve reference to bean 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authenticationManager': Cannot resolve reference to bean 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0' while setting constructor argument with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0': Cannot resolve reference to bean 'org.springframework.security.provisioning.JdbcUserDetailsManager#0' while setting bean property 'userDetailsService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.provisioning.JdbcUserDetailsManager#0': Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined

pomozecie koledzy? cos zle pewnie ustawilem z spring-security.xml , mysle i mysle i nie wiem co tu zle...

0

A czy Pan czytał cokolwiek o konfiguracji springa? Moim zdaniem wystarczy przenieść beana datasource do głównego konktektsu aplikacji.

0

ale on jest w glownym kontekscie aplikacji, w pliku nazwa_aplikacji-servlet.xml tak jak jest to standardowo

0

Moim zdaniem bujasz, i dataSource jest w widoczny tylko w dispatcherze, pokaż web.xml

0
 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

<servlet>
    <servlet-name>student-recruitment-app</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>student-recruitment-app</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
 
        <!-- Loads Spring Security config file -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/spring-security.xml
		</param-value>
	</context-param>
 
	<!-- Spring Security -->
	<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>
	
</web-app>
0

Skąd Ty wziąłeś tą konfigurację? Odwołujesz się w spring-security.xml do beana dataSource, który jest w dupadupa-servlet (DISPATCHER) !!! Czyli wyszło na moje, bujasz kolego.

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/root-context.xml (stwórz ten plik i dodaj w nim datasource)
            /WEB-INF/spring-security.xml
            /WEB-INF/dispatcher-servlet.xml(Twój dupadupa-servlet)
        </param-value>
    </context-param>

Na przyszłość http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-servlet

1 użytkowników online, w tym zalogowanych: 0, gości: 1