No bean named 'myUserDetailsService' is defined



I'm trying connect my application with Spring Security and Hibernate. But context doesn't see beanmyUserDetailsService. I have tried several ways to resolve this problem with no result. I would like to use adnotation way but I tried XML way too. Below there are my configuration:


MyUserDetailsService.java



@Service("userDetailsService")
@Transactional
public class MyUserDetailsService implements UserDetailsService {

@Autowired
private UserRepository userRepository;

@Override
public UserDetails loadUserByUsername(final String userName)
throws UsernameNotFoundException {

com.engineering.pawel.pojo.User user = userRepository
.findByUserName(userName);
List<GrantedAuthority> authorities = buildUserAuthority(user
.getUserRole());

return buildUserForAuthentication(user, authorities);
}

private User buildUserForAuthentication(
com.engineering.pawel.pojo.User user,
List<GrantedAuthority> authorities) {
return new User(user.getNick(), user.getPassword(), true, true, true,
true, authorities);
}

private List<GrantedAuthority> buildUserAuthority(Set<UserRole> userRoles) {

Set<GrantedAuthority> setAuths = new HashSet<GrantedAuthority>();

for (UserRole userRole : userRoles) {
setAuths.add(new SimpleGrantedAuthority(userRole.getRole()));
}

List<GrantedAuthority> Result = new ArrayList<GrantedAuthority>(
setAuths);

return Result;
}
}


servlet-context.xml



<beans:beans xmlns="http://ift.tt/1bHqwjR"
xmlns:xsi="http://ift.tt/ra1lAU" xmlns:beans="http://ift.tt/GArMu6"
xmlns:tx="http://ift.tt/OGfeU2" xmlns:context="http://ift.tt/GArMu7"
xmlns:jee="http://ift.tt/OpNaZ5"
xsi:schemaLocation="http://ift.tt/1bHqwjR http://ift.tt/1kF4x7W
http://ift.tt/GArMu6 http://ift.tt/1cnl1uo
http://ift.tt/GArMu7 http://ift.tt/1ldEMZY
http://ift.tt/OGfeU2
http://ift.tt/KC395X
http://ift.tt/OpNaZ5
http://ift.tt/1g7TBr5">



<!-- Enables the Spring MVC @Controller programming model -->

<annotation-driven />
<context:annotation-config />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>

<!-- Configure to plugin JSON as request and response in method handler -->
<beans:bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter" />
</beans:list>
</beans:property>
</beans:bean>

<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</beans:bean>

<context:component-scan base-package="com.engineering.pawel" />


<!-- Database configuration -->
<tx:annotation-driven />

<jee:jndi-lookup id="entityManagerFactory" jndi-name="java:comp/env/persistence/emf"
expected-type="javax.persistence.EntityManagerFactory" />

<beans:bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<beans:property name="transactionManagerName" value="java:/TransactionManager" />
</beans:bean>

<beans:bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<beans:bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
<beans:property name="entityManagerFactory" ref="entityManagerFactory" />
</beans:bean>


</beans:beans>


spring-security.xml



<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://ift.tt/1c8inpe"
xmlns:beans="http://ift.tt/GArMu6"
xmlns:xsi="http://ift.tt/ra1lAU"
xmlns:jdbc="http://ift.tt/18IIlo0"
xmlns:context="http://ift.tt/GArMu7"
xsi:schemaLocation="http://ift.tt/GArMu6
http://ift.tt/1cnl1uo
http://ift.tt/18IIlo0
http://ift.tt/1elhrQW
http://ift.tt/1c8inpe
http://ift.tt/1epvZ6L
http://ift.tt/GArMu7
http://ift.tt/1bGeTcI">

<http auto-config="true">
<intercept-url pattern="/admin**" access="ROLE_USER" />
<form-login
login-page="/login"
default-target-url="/welcome"
authentication-failure-url="/login?error"
username-parameter="username"
password-parameter="password" />
<logout logout-success-url="/login?logout" />
<!-- enable csrf protection -->
<csrf/>
</http>

<authentication-manager>
<authentication-provider user-service-ref="myUserDetailsService" >
<password-encoder hash="md5" />
</authentication-provider>
</authentication-manager>

</beans:beans>


web.xml



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://ift.tt/nSRXKP" xmlns:xsi="http://ift.tt/ra1lAU"
xsi:schemaLocation="http://ift.tt/nSRXKP
http://ift.tt/1eWqHMP"
version="3.0">

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

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<!-- ........................................................................... -->
<!-- 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>

<persistence-unit-ref>
<persistence-unit-ref-name>persistence/emf</persistence-unit-ref-name>
<persistence-unit-name>engineerJPA</persistence-unit-name>
</persistence-unit-ref>

</web-app>


Exception log:



21:35:25,129 ERROR [org.springframework.web.context.ContextLoader] (MSC service thread 1-9) Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.filterChains': Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#0' while setting bean property 'sourceList' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#0': Cannot resolve reference to bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0' while setting constructor argument with key [4]; nested exception is 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 'myUserDetailsService' while setting bean property 'userDetailsService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myUserDetailsService' is defined

No comments:

Post a Comment