自定义AuthenticationProvider的简单例子

  • xml 配置:

 


       
   

 
 
 
      
                        
           

       

 

 

 

  • net.mantis.security.auth.MyAuthenticationProvider

 

public class MyAuthenticationProvider implements AuthenticationProvider {
 
    UserDetailsService userDetailsService;

    public Authentication authenticate(Authentication authentication)
            throws AuthenticationException {
         //username
        System.out.println("user name: "+authentication.getName());
        //password
        System.out.println("password: "+authentication.getCredentials());
        System.out.println("getPrincipal: "+authentication.getPrincipal());
        System.out.println("getAuthorities: "+authentication.getAuthorities());
        System.out.println("getDetails: "+authentication.getDetails());

        UserDetails userDetails = (UserDetails)this.userDetailsService.loadUserByUsername(authentication.getName());
      
        UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(
                userDetails, authentication.getCredentials(),userDetails.getAuthorities());
        return result;
    }


    public boolean supports(Class authentication) {
         return true;
    }

    public void setUserDetailsService(UserDetailsService userDetailsService){
        this.userDetailsService = userDetailsService;
    }
}

 

  • net.mantis.security.auth.NMUserDetailsService

public class NMUserDetailsService implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String userName)
            throws UsernameNotFoundException {
        ArrayList list = new ArrayList();
        list.add(new SimpleGrantedAuthority("ROLE_SUPERVISOR"));
        User details = new User("rod", "koala", list);
        return details;
    }

}

你可能感兴趣的:(Spring,security)