Results 1 to 6 of 6

Thread: Custom Authentication Provider - No AuthenticationProvider found

  1. #1
    Join Date
    Oct 2010
    Posts
    3

    Default Custom Authentication Provider - No AuthenticationProvider found

    I am trying to use Spring Security with a custom Authentication Provider. I need to use a custom Authentication Provide becuase username/password validation needs to be done through the TopSecret API which exists on the mainframe which is exposed to a webservice.

    When I try and login I keep getting a "No AuthenticationProvider found for org.springframework.security.authentication.Userna mePasswordAuthenticationToken.". There must be some disconnect that I am missing.

    Code is as follows

    Code:
    <?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.0.xsd
               http://www.springframework.org/schema/security
               http://www.springframework.org/schema/security/spring-security-3.1.xsd">
    
              
        <http pattern="/css/**" security="none"/>
        <http pattern="/images/**" security="none"/>
        <http pattern="/js/**" security="none"/>
        <http pattern="/pages/Login.jsp*" security="none"/>
      
        <http auto-config='true'>
            <intercept-url pattern="/**" access="ROLE_USER" />
    	<form-login login-page="/pages/Login.jsp" authentication-failure-url="/pages/Login.jsp?login_error=1"
                default-target-url='/mainSearch.do'
                        always-use-default-target='true'/>
            <session-management>
                <concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
            </session-management>
        </http>   
    	
        <authentication-manager alias="authenticationManager">
            <authentication-provider ref="topSecretAuthenticationProvider" />
        </authentication-manager>
        
        <beans:bean id="topSecretAuthenticationProvider" class="com.ams.cms.security.TopSecretAuthenticationProvider" />
    	
    	<!-- Automatically receives AuthenticationEvent messages -->
        <beans:bean id="loggerListener" class="org.springframework.security.authentication.event.LoggerListener"/>
    	             
    </beans:beans>
    This class isn't getting called. It is just implementing basic authentication for testing.

    Code:
    public class TopSecretAuthenticationProvider implements AuthenticationProvider {
    
        @SuppressWarnings("serial")
        private static Map<String, String> SIMPLE_USERS = new HashMap<String, String>(2) {{
    	    put("joe", "joe");
    	    put("bob", "bob");
        }};
        
        @SuppressWarnings("serial" )
    	private static List<GrantedAuthority> AUTHORITIES = new ArrayList<GrantedAuthority>(1) {{
        	add(new GrantedAuthorityImpl("ROLE_USER"));
        }};
    	    
    	@Override
    	public Authentication authenticate(Authentication auth) throws AuthenticationException
    	{
    		// All your user authentication needs
    		System.out.println("==Authenticate Me==");
            if (SIMPLE_USERS.containsKey(auth.getPrincipal()) 
            	&& SIMPLE_USERS.get(auth.getPrincipal()).equals(auth.getCredentials()))
            {
                return new UsernamePasswordAuthenticationToken(auth.getName(), auth.getCredentials(), AUTHORITIES);
            }
            throw new BadCredentialsException("Username/Password does not match for " + auth.getPrincipal());
    	}
    }
    Any Ideas??

  2. #2
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default

    You must have implemented the supports method (which isn't included in your code listing) incorrectly...
    Peter Mularien | Blog
    Author, Spring Security 3 (Book) - Packt Publishing, Available in print and eBook form
    SCJP 5, Oracle DBA
    Any postings are my own opinion, and should not be attributed to my employer or clients.


  3. #3
    Join Date
    Oct 2010
    Posts
    3

    Default

    That is what I am probably missing since I have not implemented that. Could you please expand on this?

  4. #4
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default

    I'm confused how your code would compile, then, because supports is part of the AuthenticationProvider interface:

    http://static.springsource.org/sprin...nProvider.html

    Just look at one of the existing implementations, for example AbstractUserDetailsAuthenticationProvider, and either extend or copy/paste as you feel appropriate.
    Peter Mularien | Blog
    Author, Spring Security 3 (Book) - Packt Publishing, Available in print and eBook form
    SCJP 5, Oracle DBA
    Any postings are my own opinion, and should not be attributed to my employer or clients.


  5. #5
    Join Date
    Oct 2010
    Posts
    3

    Default

    Yes thank you! You were spot on. I added this code and it worked perfect.

    Code:
    @Override
    public boolean supports(Class<?> authentication) {
        return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
    }
    Thanks again

  6. #6
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default

    Great! Glad to hear it.
    Peter Mularien | Blog
    Author, Spring Security 3 (Book) - Packt Publishing, Available in print and eBook form
    SCJP 5, Oracle DBA
    Any postings are my own opinion, and should not be attributed to my employer or clients.


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •