SpringSecurity - Custom automatic authentication

后端 未结 2 1259
不思量自难忘°
不思量自难忘° 2020-12-13 01:19

This is my scenario:

  • a web-app perform a sort-of SSO for many applications
  • logged-in user than click on a link and the app makes a post with user info
2条回答
  •  攒了一身酷
    2020-12-13 01:59

    Yes, Pre-Authentication Scenarios are exactly what you are looking for.

    It seems that those object are expected to be used when the principal is already in session, put by some previous authentication machanism (is it right?).

    Not really, you can use Pre-Authentication to create PreAuthenticatedAuthenticationToken from request, as you want. Just do few things I described in another question.

    First extend AbstractPreAuthenticatedProcessingFilter to obtain username and roles from request:

    public class MyPreAuthenticatedProcessingFilter
        extends AbstractPreAuthenticatedProcessingFilter {
    
      public MyPreAuthenticatedProcessingFilter(
          AuthenticationManager authenticationManager) {
        setAuthenticationDetailsSource(new MyAuthenticationDetailsSource());
      }
    
      @Override
      protected Object getPreAuthenticatedPrincipal(HttpServletRequest request) {
        return "Anonymous";
      }
    
      @Override
      protected Object getPreAuthenticatedCredentials(HttpServletRequest request) {
        return "N/A";
      }
    
      public static class MyAuthenticationDetailsSource implements 
          AuthenticationDetailsSource {
        // roles probably should be encrypted somehow
        static final String ROLES_PARAMETER = "pre_auth_roles";
    
        @Override
        public MySessionUserDetails buildDetails(HttpServletRequest req) {
          // create container for pre-auth data
          return new MySessionUserDetails(req.getParameter(ROLES_PARAMETER));
        }
      }
    }
    

    MySessionUserDetails class will split spring with roles to List of SimpleGrantedAuthority or any other GrantedAuthority implementation. Also, List is recommended and superior to GrantedAuthority[].

    Second, implement AuthenticationUserDetailsService:

    public class MyPreAuthenticatedUserDetailsService implements 
        AuthenticationUserDetailsService {
    
      @Override
      public UserDetails loadUserDetails(PreAuthenticatedAuthenticationToken token)
          throws UsernameNotFoundException {
        MySessionUserDetails sessionUserDetails =
            (MySessionUserDetails) token.getDetails();
        List authorities = sessionUserDetails.getAuthorities();
        return new User(token.getName(), "N/A", true, true, true, true, authorities);
      }
    }
    

    Then in your XML connect blocks together:

    
      
      
    
    
    
      
    
    
    
      
        
      
    
    
    
      
    
    

    And voila! You should have authenticated User principal to use in your application.

    Code I written here requires Spring Security 3.1 which I strongly recommend if you're about to using it (it does requrire Spring 3.0.7+). Also, Spring Security reference manual is your friend!

提交回复
热议问题