作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在应用程序引擎上使用 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 容器无法找到名为 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
中定义,但是,该包被排除在组件扫描之外(请参阅排除过滤器),因此Spring不会注册相应的bean。
此外,您的身份验证管理器配置也存在问题。请按照下面的摘录进行操作。
<authentication-manager>
<authentication-provider user-service-ref='mutiboUserDetailsService'/>
</authentication-manager>
关于java - Spring Security 找不到自定义 UserDetailsService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26815299/
我是一名优秀的程序员,十分优秀!