Java spring身份验证管理器不绑定由@service注释的用户服务
我有这样的UserManager类-Java spring身份验证管理器不绑定由@service注释的用户服务,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,我有这样的UserManager类- @Service public class UserManager implements UserDetailsService { @Autowired UserRepositoryImpl userRepositoryImpl; @Override public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException { User use
@Service
public class UserManager implements UserDetailsService {
@Autowired
UserRepositoryImpl userRepositoryImpl;
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
User user = userRepositoryImpl.findUserByEmail(email);
List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>();
authList.add(new SimpleGrantedAuthority(user.getRole().getName()));
UserDetails userDetails = new org.springframework.security.core.userdetails.User(email,user.getPassword(),true,true,true,true,authList);
return userDetails;
}
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
/WEB-INF/application-security.xml
</param-value>
</context-param>
<context:annotation-config/>
<context:component-scan base-package="org.opentutor.controllers"/>
<context:component-scan base-package="org.opentutor.configs"/>
<context:component-scan base-package="org.opentutor.managers"/>
<context:component-scan base-package="org.opentutor.repoimpls"/>
<mvc:annotation-driven />
@服务
公共类UserManager实现UserDetailsService{
@自动连线
UserRepositoryImpl UserRepositoryImpl;
@凌驾
public UserDetails loadUserByUsername(字符串电子邮件)引发UsernameNotFoundException{
User User=userRepositoryImpl.findUserByEmail(电子邮件);
List authList=new ArrayList();
add(新的SimpleGrantedAuthority(user.getRole().getName());
UserDetails UserDetails=new org.springframework.security.core.UserDetails.User(电子邮件,User.getPassword(),true,true,true,authList);
返回用户详细信息;
}
现在应用程序安全是-
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="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.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<http use-expressions="true" auto-config="true">
<intercept-url pattern="/Home" access="permitAll" />
<intercept-url pattern="/resources/**" access="permitAll" />
<intercept-url pattern="/logout" access="permitAll" />
<intercept-url pattern="/Admin/**" access="hasRole('Admin')" />
<intercept-url pattern="/Teacher/**" access="hasRole('Teacher')" />
<intercept-url pattern="/Student/**" access="hasRole('Teacher')" />
<form-login login-page="/Login" authentication-failure-url="/Login?login_error=1" authentication-success-handler-ref="authenticationSuccessHandler" />
<logout logout-success-url="/Login" invalidate-session="true" delete-cookies="JSESSIONID" />
</http>
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userManager"/>
</authentication-manager>
</beans:beans>
在web.xml中,我添加了applicationSecurity.xml作为上下文参数,如下所示-
@Service
public class UserManager implements UserDetailsService {
@Autowired
UserRepositoryImpl userRepositoryImpl;
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
User user = userRepositoryImpl.findUserByEmail(email);
List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>();
authList.add(new SimpleGrantedAuthority(user.getRole().getName()));
UserDetails userDetails = new org.springframework.security.core.userdetails.User(email,user.getPassword(),true,true,true,true,authList);
return userDetails;
}
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
/WEB-INF/application-security.xml
</param-value>
</context-param>
<context:annotation-config/>
<context:component-scan base-package="org.opentutor.controllers"/>
<context:component-scan base-package="org.opentutor.configs"/>
<context:component-scan base-package="org.opentutor.managers"/>
<context:component-scan base-package="org.opentutor.repoimpls"/>
<mvc:annotation-driven />
上下文配置位置
/WEB-INF/applicationContext.xml
/WEB-INF/application-security.xml
问题是当我运行代码时,它会引发异常
原因:org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.security.authentication.dao.DaoAuthenticationProvider#0”的bean时出错:设置bean属性“userDetailsService”时无法解析对bean“userManager”的引用;嵌套异常为org.springframework.beans.factory.NoSuchBeanDefinitionE异常:未定义名为“userManager”的bean
原因:org.springframework.beans.factory.NoSuchBean定义异常:未定义名为“userManager”的bean
如果我将同一个userManager与controller自动关联,则该userManager可以正常工作。因为userManager类使用@Service annotation进行注释,甚至我在dispatcherServlet.xml中也启用了注释驱动,如下所示-
@Service
public class UserManager implements UserDetailsService {
@Autowired
UserRepositoryImpl userRepositoryImpl;
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
User user = userRepositoryImpl.findUserByEmail(email);
List<GrantedAuthority> authList = new ArrayList<GrantedAuthority>();
authList.add(new SimpleGrantedAuthority(user.getRole().getName()));
UserDetails userDetails = new org.springframework.security.core.userdetails.User(email,user.getPassword(),true,true,true,true,authList);
return userDetails;
}
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
/WEB-INF/application-security.xml
</param-value>
</context-param>
<context:annotation-config/>
<context:component-scan base-package="org.opentutor.controllers"/>
<context:component-scan base-package="org.opentutor.configs"/>
<context:component-scan base-package="org.opentutor.managers"/>
<context:component-scan base-package="org.opentutor.repoimpls"/>
<mvc:annotation-driven />
它应该找到userManager bean,但它没有。在扫描java配置之前是否有类似于加载身份验证管理器的情况。请帮助解决此错误。Spring web应用程序中上下文的生命周期如下所示:
ContextLoaderListener
从contextConfigLocation
加载和合并上下文(默认或指定)DispatcherServlet
使用ContextLoaderListener
加载的合并上下文作为父上下文加载servlet上下文DispatcherServlet
加载的上下文可以访问ContextLoaderListener
加载的上下文,但不能反过来访问。因此,此位
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userManager"/>
</authentication-manager>
在dispatcherServlet.xml
中
重新设计上下文。servlet上下文应仅包含与servlet上下文相关的bean定义。
UserManager
bean(及其相关的
)与应用程序相关,而不是与servlet相关,因此将其放在applicationContext.xml
中Spring web应用程序中上下文的生命周期如下所示:
ContextLoaderListener
从contextConfigLocation
加载和合并上下文(默认或指定)DispatcherServlet
使用ContextLoaderListener
加载的合并上下文作为父上下文加载servlet上下文DispatcherServlet
加载的上下文可以访问ContextLoaderListener
加载的上下文,但不能反过来访问。因此,此位
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userManager"/>
</authentication-manager>
在dispatcherServlet.xml
中
重新设计上下文。servlet上下文应仅包含与servlet上下文相关的bean定义。
UserManager
bean(及其相关的
)与应用程序相关,而不是与servlet相关,因此将其放在applicationContext.xml
中Spring web应用程序中上下文的生命周期如下所示:
ContextLoaderListener
从contextConfigLocation
加载和合并上下文(默认或指定)DispatcherServlet
使用ContextLoaderListener
加载的合并上下文作为父上下文加载servlet上下文DispatcherServlet
加载的上下文可以访问ContextLoaderListener
加载的上下文,但不能反过来访问。因此,此位
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userManager"/>
</authentication-manager>
在dispatcherServlet.xml
中
重新设计上下文。servlet上下文应仅包含与servlet上下文相关的bean定义。
UserManager
bean(及其相关的
)与应用程序相关,而不是与servlet相关,因此将其放在applicationContext.xml
中Spring web应用程序中上下文的生命周期如下所示:
ContextLoaderListener
从contextConfigLocation
加载和合并上下文(默认或指定)DispatcherServlet
使用ContextLoaderListener
加载的合并上下文作为父上下文加载servlet上下文DispatcherServlet
加载的上下文可以访问ContextLoaderListener
加载的上下文,但不能反过来访问。因此,此位
<authentication-manager alias="authenticationManager">
<authentication-provider user-service-ref="userManager"/>
</authentication-manager>
在dispatcherServlet.xml
中
重新设计上下文。servlet上下文应仅包含与servlet上下文相关的bean定义。UserManager
bean(及其相关的
)与应用程序相关,而不是servlet,因此请将其放在applicationContext.xml
仅供参考,
是多余的,如果您有
。仅供参考,
是多余的,如果您有
,仅供参考,
是多余的