0

I am implementing my own authenticator provider for Spring Security 3.1.4.

But when I running application on Tomcat I receive this log error from Tomcat.

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myAppAuthenticationProvider' is defined

In web.xml y have this than others things.

<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>

In myapp-servlet.xml y have this than others things.

 <!-- 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" />

In myapp-security.xml y have this than others things.

<security:authentication-manager alias="myAuthenticationManager">
    <security:authentication-provider ref="myAppAuthenticationProvider" />
</security:authentication-manager>

Then, I have my authenticator provider like this.

@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;
    }

}

Somebody can help me?, Thanks in advance.`enter code here

Dani
  • 4,001
  • 7
  • 36
  • 60

1 Answers1

3

You have two spring contexts (first one for application beans / DS / security and second one for Spring MVC). Please make sure that your MyAppAuthenticationProvider is picked up in the first one by corresponding component-scan element. I am pretty sure that it is picked up by the second context (Spring MVC) and referenced from the first one, where it does not exist.

Maksym Demidas
  • 7,707
  • 1
  • 29
  • 36
  • Yes, but then I have one problem, If I configure component-scan in security context, Every others beans declarations, included persistence beans layer, are in other context, then I have hibernate4 session transaction failure (UserService need sessionfactory). What can I do?. I need import to myAppAuthenticationProvider on security context one userService reference on other context... Sure? – Dani Jul 22 '13 at 11:26
  • Normlally all your application beans except controllers must be declared in first context. So just move them (I mean persistence beans layer) from MVC context to application context. In any way they will be available automatically in MVC context due to context nesting. This is a general advice, it will be better to see all your context files to better understand your situation. – Maksym Demidas Jul 22 '13 at 12:23
  • Thank you very mucho @Maksym Demidas, Now I understand correct xml config files behaviour and use import inside ApplicationContext.xml to load persistence and security layers. – Dani Jul 22 '13 at 14:42
  • You are wellcome! Think about accepting my answer if the problem was completely solved for you. – Maksym Demidas Jul 22 '13 at 14:51