[英]spring security login with CustomUserDetailsService
嗨,我正在尝试为用户登录实施Spring Security。 我的spring-security.xml文件是
<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>
这是我的CustomUserDetailsService类
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;
}
}
这是我的UserService类别
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);
}
}
这是我的web.xml文件
<?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>
我的问题是当我提交表格并调试应用程序时
com.dashboard.Model.User userObj = userService.getUser(email);
我的'userService'对象为null,任何人都可以告诉我为什么bean为null。 据我了解,我已经在UserService类的开头声明了一个@service注释,那么它不应该创建一个新的bean对象吗?
只有声明了<context:component-scan>
或<context:annotation-config>
时,才会处理@Autowired
注释的字段。 您将需要添加其中之一。
还要注意,用@Service
注释类并对其进行component-scan
将为该类创建一个bean。
@Service
public class CustomUserDetailsService implements UserDetailsService {
为该类放置一个bean定义
<beans:bean id="userDetailsService" class="com.dashboard.service.CustomUserDetailsService"/>
还会生成一个bean。 在这种情况下,您的上下文中将有2个类型为CustomUserDetailsService
bean。 您可能不想要这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.