Hi i am trying to implement spring security for user login. My spring-security.xml file is
<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.1.xsd">
<http use-expressions="true">
<intercept-url pattern="/home/**" access="isAuthenticated()" />
<intercept-url pattern="/login" access="permitAll" />
<intercept-url pattern="/" access="permitAll" />
<form-login login-page="/cart" authentication-failure-url="/#/login?error=1" always-use-default-target="true"/>
<logout />
</http>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userDetailsService"/>
</authentication-manager>
<beans:bean id="userDetailsService" class="com.dashboard.service.CustomUserDetailsService"/>
</beans:beans>
and here is my CustomUserDetailsService class
package com.dashboard.service;
import java.util.Collections;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import static java.util.Arrays.asList;
import com.dashboard.repositories.UserRepository;
@Service
public class CustomUserDetailsService implements UserDetailsService {
@Resource
UserService userService;
@Autowired
UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
System.out.println("here");
try{
SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority("ROLE_ADMIN");
com.dashboard.Model.User userObj = userService.getUser(email);
User user = new User(userObj.getUserName(), userObj.getPassword(), true, true, true, true, asList(simpleGrantedAuthority));
return null;
}catch(Exception e){
e.printStackTrace();
return null;
}
//return null;
}
}
and here is my UserService Class
package com.dashboard.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.dashboard.Model.User;
import com.dashboard.repositories.UserRepository;
@Service
public class UserService {
@Resource
UserRepository userRepository;
public User saveUser(User user){
return userRepository.save(user);
}
public User getUser(String email){
return userRepository.findByEmail(email);
}
}
here is my web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<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>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<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,
/WEB-INF/spring/spring-security.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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
My problem is when i submit the form and debug the application, when i reach to line
com.dashboard.Model.User userObj = userService.getUser(email);
my 'userService' object is null, can any one kindly tell my why the bean it is null. As far as my understanding i have declare a @service annotation in start of the UserService class, so shouldn't it create a new bean object?
@Autowired
annotated fields will only be processed if you declare a <context:component-scan>
or <context:annotation-config>
. You will need to add one of these.
Also note that annotating your class with @Service
and component-scan
ing it will create a bean for that class.
@Service
public class CustomUserDetailsService implements UserDetailsService {
Putting a bean definition for that class
<beans:bean id="userDetailsService" class="com.dashboard.service.CustomUserDetailsService"/>
will also generate a bean. In this case you will have 2 beans of type CustomUserDetailsService
in your context. You might not want this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.