【问题标题】:Spring 3 - In CustomUserDetailsService class, i am getting resource nullSpring 3 - 在 CustomUserDetailsS​​ervice 类中,我得到资源 null
【发布时间】:2013-12-19 23:10:40
【问题描述】:

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

这是我的 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 为空。谁能帮我弄清楚为什么它是空的。

【问题讨论】:

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


    【解决方案1】:

    问题是混合了 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"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-10
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 2017-06-14
      • 1970-01-01
      • 2013-11-19
      • 2016-06-15
      相关资源
      最近更新 更多