Custom authentication in Spring

前端 未结 3 642
被撕碎了的回忆
被撕碎了的回忆 2021-02-06 16:16

I have a question. In Struts, I have an Action that deals with user authentication, i.e., I took the user\'s credentials and used a DAO to validate user credentials. I want to m

3条回答
  •  天命终不由人
    2021-02-06 16:53

    You can create a custom authentication provider that implements org.springframework.security.authentication.AuthenticationProvider like this

    package com.bzone.example;
    
    import org.springframework.security.authentication.AuthenticationProvider;
    import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
    import org.springframework.security.core.Authentication;
    import org.springframework.security.core.AuthenticationException;
    
    
    public class CustomAuthenticationProvider implements AuthenticationProvider{
    
        @Override
        public Authentication authenticate(Authentication authentication)
                throws AuthenticationException {
            // TODO call custom service or do whatever you want 
            return null;
        }
    
        @Override
        public boolean supports(Class authentication) {
            // copied it from AbstractUserDetailsAuthenticationProvider
            return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
        }
    
    }
    

    one more step is to configure spring security to use this custom authentication provider

    
    
    
        
        
            
            
    
            
            
            
            
            
        
    
        
        
            
        
    
    
    

提交回复
热议问题