我正在为Spring Security 3.1.4实现我自己的身份验证器提供程序。
但是当我在Tomcat上运行应用程序时,我从Tomcat收到此日志错误。
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myAppAuthenticationProvider' is defined
在web.xml中,你有这个比其他东西。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml,
/WEB-INF/myapp-datasource.xml,
/WEB-INF/myapp-security.xml
</param-value>
</context-param>
<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>
</filter-mapping>
在 myapp-servlet.xml 中,y比其他东西更多。
<!-- Activa la configuracion de beans mediante anotaciones -->
<mvc:annotation-driven />
<!-- Activa la configuracion de los controladores mediante anotaciones -->
<context:component-scan base-package="com.myapp.**" />
<!-- Activa la configuracion de seguridad mediante anotaciones -->
<security:global-method-security secured-annotations="enabled" pre-post-annotations="enabled" />
在 myapp-security.xml 中,y比其他东西要好。
<security:authentication-manager alias="myAuthenticationManager">
<security:authentication-provider ref="myAppAuthenticationProvider" />
</security:authentication-manager>
然后,我有这样的身份验证提供程序。
@Component("myAppAuthenticationProvider")
public class MyAppAuthenticationProvider implements AuthenticationProvider {
private static final Logger Log = LoggerFactory.getLogger(AuthenticationProvider.class);
@Autowired
private UserService userService;
@Override
public final Authentication authenticate(Authentication authentication) {
final UsernamePasswordWithTypeAuthenticationToken authenticationToken =
(UsernamePasswordWithTypeAuthenticationToken) authentication;
String name = authenticationToken.getName();
String password = authenticationToken.getCredentials().toString();
String type = authenticationToken.getType();
if(isUserValid(authentication)){
List<GrantedAuthority> grantedAuth = new ArrayList<GrantedAuthority>();
grantedAuth.add(new SimpleGrantedAuthority("ROLE_USER"));
Authentication auth = new UsernamePasswordAuthenticationToken(name, password, grantedAuth);
return auth;
}else {
throw new BadCredentialsException("Bad authentication.");
}
}
@Override
public final boolean supports(Class<?> authentication) {
return UsernamePasswordWithTypeAuthenticationToken.class.isAssignableFrom(authentication);
}
private boolean isUserValid(Authentication authentication) {
User user = this.userService.getUserByEmailAndPassword(
authentication.getName(), authentication.getCredentials().toString());
if (user != null) {
return true;
}
return false;
}
}
有人可以帮助我吗?在此先感谢.enter code
答案 0 :(得分:3)
您有两个spring上下文(第一个用于应用程序bean / DS /安全性,第二个用于Spring MVC)。请确保通过相应的MyAppAuthenticationProvider
元素在第一个component-scan
中获取{{1}}。我很确定它是由第二个上下文(Spring MVC)拾取并从第一个上下文中引用的,它不存在。