java - Spring Security 找不到自定义 UserDetailsS​​ervice

标签 java spring spring-security

我正在尝试在应用程序引擎上使用 Spring Security。我实现了自己的 userdetailsservice。

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>

服务.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. 

有人可以帮助我吗?谢谢。

最佳答案

您的问题与 App Engine 无关,而是与 Spring 容器无法找到名为 mutiboUserDetailsS​​ervice 的 bean 相关。

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

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

MutiboUserDetailsS​​ervice在包com.example.mymodule.app2中定义,但是,该包被排除在组件扫描之外(请参阅排除过滤器),因此Spring不会注册相应的bean。

此外,您的身份验证管理器配置也存在问题。请按照下面的摘录进行操作。

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

关于java - Spring Security 找不到自定义 UserDetailsS​​ervice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26815299/

相关文章:

java - 如何在 successhandler 中设置的登录处理程序中检索 session 值

java - 将包命名为 "foo.bar.import"是个好主意吗?

java - 如何修复 java.lang.UnsupportedClassVersionError : Unsupported major. 次要版本

java - java中将字符串转换为变量

java - Spring - 它是什么以及我为什么要使用它?

spring-boot - Springboot安全+jwt 'springSecurityFilterChain'错误

java - 无法启动 Cassandra : +HeapDumpOnOutOfMemoryError

java - 如何使用 java 读取从存储过程返回的类型数组?

java - 用户 'root' @'localhost' Spring Boot MySql 访问被拒绝

mongodb - 将 Spring 安全 ACL 与 spring-data-mongodb 结合使用