2013-12-08 419 views
0

嗨,我想實現用戶登錄彈簧安全。我的彈簧security.xml文件是春季安全登錄CustomUserDetailsS​​ervice

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

,這裏是我的CustomUserDetailsS​​ervice類

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> 

我的問題EM是,當我提交表單和調試應用程序,當我到達排隊

com.dashboard.Model.User userObj = userService.getUser(email); 

我「userService」對象爲空,任何一個可以請你告訴我,爲什麼豆爲null。據我瞭解,我已經在UserService類的開頭聲明瞭一個@service註解,所以它不應該創建一個新的bean對象?

回答

0

@Autowired僅當您聲明<context:component-scan><context:annotation-config>時纔會處理帶註釋的字段。您需要添加其中一個。


也要注意,使用@Servicecomponent-scan荷蘭國際集團將創建該類的豆註釋類。

@Service 
public class CustomUserDetailsService implements UserDetailsService { 

把一個bean定義爲類

<beans:bean id="userDetailsService" class="com.dashboard.service.CustomUserDetailsService"/> 

也將產生一個bean。在這種情況下,您的上下文中將有2個類型爲CustomUserDetailsService的豆。你可能不想要這個。

+0

感謝您的回覆,現在我正在爲元素「context:component-scan」的前綴「context」未綁定。我在stackoverflow上搜索這樣的一些問題,但沒有爲我工作。 – Shahzeb

+0

@Shahzeb請注意,您的上下文文件中是否有命名空間聲明'xmlns:beans =「http://www.springframework.org/schema/beans」'?您需要爲'context'命名空間添加相應的一個。您還需要更新'schemaLocation'。 –

+0

我加了,現在我得到這個錯誤「匹配的通配符是嚴格的,但沒有聲明可以找到元素'上下文:組件掃描'。」 – Shahzeb