Spring Security无法找到自定义UserDetailsS​​ervice

时间:2014-11-08 08:37:43

标签: java spring spring-security

我试图在app引擎上使用spring security。我实现了自己的userdetails服务。

package com.example.mymodule.app2;

import com.example.mymodule.repoobject.MutiboUser;
import com.example.mymodule.repository.MutiboUserRepo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
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.Component;

import java.util.Collection;
import java.util.List;


@Component
public class MutiboUserDetailsService implements UserDetailsService {

    private final MutiboUserRepo mutiboUserRepo;

    @Autowired
    public MutiboUserDetailsService(MutiboUserRepo mutiboUserRepo) {

        if (mutiboUserRepo == null) {
            throw new IllegalArgumentException("mutiboUserRepo cannot be null");
        }
        this.mutiboUserRepo = mutiboUserRepo;
    }

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

        MutiboUser mutiboUser = mutiboUserRepo.findByName(username);

        if (mutiboUser == null) {
            throw new UsernameNotFoundException("Invalid username/password.");
        }

        List<GrantedAuthority> authorityList = AuthorityUtils
                .createAuthorityList("ROLE_USER");

        return new User(mutiboUser.getEmail(), mutiboUser.getPassword(), authorityList);
    }
}

这是我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/services.xml
            /WEB-INF/spring/security.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <!-- intercept all requests -->
    <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>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>


    <!-- Spring MVC DispatcherServelt -->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>
                org.springframework.web.context.support.AnnotationConfigWebApplicationContext
            </param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                com.example.mymodule.app2.Application
            </param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <!-- to mora it preko https (app engine)-->
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>question</web-resource-name>
            <url-pattern>/question/*</url-pattern>
        </web-resource-collection>
        <user-data-constraint>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint>
    </security-constraint>


</web-app>

的services.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

    <context:component-scan base-package="com.example.mymodule">
        <context:exclude-filter type="regex" expression="com.example.mymodule.*"/>
    </context:component-scan>


</beans>

和security.xml

<?xml version="1.0" encoding="UTF-8"?>
<b:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:b="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.xsd">

    <http auto-config="true">
        <intercept-url access="ROLE_USER" pattern="/*" />
    </http>
    <authentication-manager>
        <authentication-provider
            ref="mutiboUserDetailsService"/>
    </authentication-manager>
</b:beans>

但每次我厌倦测试我的应用程序时都会收到此错误。

'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0': Cannot 
resolve reference to bean 'mutiboUserDetailsService' while setting bean property 
'userDetailsService'; nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 
'mutiboUserDetailsService' is defined. 

有人能帮助我吗?感谢。

1 个答案:

答案 0 :(得分:2)

您的问题与App Engine无关,但Spring容器无法找到名为mutiboUserDetailsService的bean。

我想说根本原因是你的组件扫描配置。

<context:component-scan base-package="com.example.mymodule">
    <context:exclude-filter type="regex" expression="com.example.mymodule.*"/>
</context:component-scan>

MutiboUserDetailsService是在包com.example.mymodule.app2中定义的,但是,此包被排除(参见exclude-filter),因此Spring不会注册相应的bean。

此外,您的身份验证管理器配置也存在问题。请看下面的摘录。

<authentication-manager>
    <authentication-provider user-service-ref='mutiboUserDetailsService'/>
</authentication-manager>