您好我正在尝试为用户登录实现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对象吗?
答案 0 :(得分:0)
@Autowired
或<context:component-scan>
时才会处理 <context:annotation-config>
带注释的字段。您需要添加其中一个。
另请注意,使用@Service
和component-scan
注释您的课程将为该课程创建一个bean。
@Service
public class CustomUserDetailsService implements UserDetailsService {
为该类设置bean定义
<beans:bean id="userDetailsService" class="com.dashboard.service.CustomUserDetailsService"/>
也会生成一个bean。在这种情况下,您的上下文中将有2个类型为CustomUserDetailsService
的bean。你可能不想要这个。