Spring 3 - 在CustomUserDetailsS​​ervice类中,我获取资源null

时间:2013-12-03 18:28:22

标签: spring angularjs spring-mvc spring-security spring-data

您好我正在为我的应用程序添加弹簧安全性(带有角度的弹簧)。但我得到的服务资源为null。以下是我的代码。

这是我的security.xml内容

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                http://www.springframework.org/schema/security
                http://www.springframework.org/schema/security/spring-security-3.1.xsd
                http://www.springframework.org/schema/context 
                http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <http auto-config="true" use-expressions="true" entry-point-ref="loginUrlAuthenticationEntryPoint">
        <intercept-url pattern="/" access="permitAll" />
        <intercept-url pattern="/home" access="isAuthenticated()" />
        <form-login />
    </http>

    <beans:bean id="loginUrlAuthenticationEntryPoint"
        class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
        <beans:property name="loginFormUrl" value="/" />
    </beans:bean>

    <beans:bean id="securityFilter"
        class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
        <beans:property name="authenticationManager" ref="authenticationManager" />
        <beans:property name="authenticationSuccessHandler"
            ref="authenticationSuccessHandler" />
    </beans:bean>
    <beans:bean id="customUserDetailsService" class="com.dashboard.service.CustomUserDetailsService"/>

         <authentication-manager alias="authenticationManager">
            <authentication-provider user-service-ref="customUserDetailsService"/>
         </authentication-manager>

    <beans:bean id="authenticationSuccessHandler"
        class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
        <beans:property name="defaultTargetUrl" value="/home" />
    </beans:bean>
</beans:beans>

我的自定义用户详细信息服务类是

@Service
public class CustomUserDetailsService implements UserDetailsService {

    @Resource
    UserRepository userRepository;

    @Resource
    UserService userService;

    @Override
    public UserDetails loadUserByUsername(String email)
            throws UsernameNotFoundException {

        try{
            User user = userService.getUser(email);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

我的角度控制器是

DashBoard.controller('LoginController', function($scope,$http,$location) {


    $scope.login = function(){
        var data = "j_username="+$scope.username+"&j_password="+$scope.password+"&_spring_security_remember_me=true";

        $http.post('j_spring_security_check', data, {
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            }
        }).success(function(data,status) {
                $location.path('/home');
            }).
            error(function(data,status) {
                console.debug("failed :"+status+" Data : "+data);
                $location.path('/login');
            });
    };
});

并且登录表单是

<form class="form-horizontal" name="signUpForm" ng-controller="LoginController" ng-submit="login()">
    <input class="span12 tenF" ng-model="username" type="email" id="j_username" name="j_username" placeholder="E-mail address" required>
    <input class="span12 tenF" type="password" ng-model="password" id="j_password" name="j_password" placeholder="Password" required>
    <input class="span3 btn btn-large btn-block btn-success" type="submit" value="Login" style="float:right;font-size:20px;margin-top:10px;">
 </ng-form>

调试时我到达

User user = userService.getUser(email);//.findByEmail(email);

但userService为null。任何人都可以帮我弄清楚为什么它是空的。

1 个答案:

答案 0 :(得分:0)

问题在于混合xml配置和基于注释的自动装配。

在这种情况下,我通常添加一个构造函数,它接受所有必需的参数并添加到xml autowire="constructor"

public class CustomUserDetailsService implements UserDetailsService {
     private UserRepository userRepository;
     private UserService userService;

     @Autowire
     public CustomUserDetailsService(UserRepository userRepository,
                                     UserService userService){
        this.userRepository=userRepository;
        this.userService=userService;
     }
     ....
}

<beans:bean id="customUserDetailsService"
            class="com.dashboard.service.CustomUserDetailsService"
            autowire="constructor"/>