Integrating Spring Security with SiteMinder

前端 未结 1 2019
刺人心
刺人心 2020-12-29 16:19

How do you integrate Spring Security with SiteMinder to receive a User and Role?

I have a project setup with Spring Security \'in-memory\' and I want to use convert

1条回答
  •  隐瞒了意图╮
    2020-12-29 17:03

    There is Spring Security for SiteMinder that exists to receive a User only. However, to receive a Role you'll need to create an extended authentication process. This will authenticate a user using a role.

    Within the root-security.xml

    
    
    
        
            
                
            
        
    
    
    
        
        
        
        
    
    
    
        
    
    

    SiteMinderUserDetailsService

    public class SiteMinderUserDetailsService extends PreAuthenticatedGrantedAuthoritiesUserDetailsService implements
            UserDetailsService {
    
        @Override
        public UserDetails loadUserByUsername(String arg0) throws UsernameNotFoundException {
            SiteMinderUserDetails userDetails = new SiteMinderUserDetails();
            userDetails.setUsername(arg0);      
            return userDetails;
        }
    
        @Override
        protected UserDetails createuserDetails(Authentication token, Collection authorities) {
            return super.createuserDetails(token, authorities);
        }
    }
    

    SiteMinderUserDetails

    public class SiteMinderUserDetails implements UserDetails {
        // implement all methods
    }
    

    SiteMinderFilter

    public class SiteMinderFilter extends RequestHeaderAuthenticationFilter {
    
        private String rolesRequestHeader;
        private String rolesDelimiter;
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException, NullPointerException {
    
            String roles = (String)  ((HttpServletRequest)request).getHeader(getRolesRequestHeader());
            String[] rolesArray = roles.split(rolesDelimiter);
    
            Collection auth = new ArrayList();
            for (String s : rolesArray) {               
                auth.add(new SimpleGrantedAuthority(s));
            }
    
            SiteMinderUserDetails userDetails = new SiteMinderUserDetails();
            userDetails.setUsername((String) super.getPreAuthenticatedPrincipal(((HttpServletRequest)request)));
            userDetails.setAuthorities(auth);
    
            AuthenticationImpl authentication = new AuthenticationImpl();
            authentication.setAuthenticated(true);
            authentication.setAuthorities(auth);
            authentication.setPrincipal(userDetails);
            authentication.setCredentials(super.getPreAuthenticatedCredentials(((HttpServletRequest)request)));
            SecurityContextHolder.getContext().setAuthentication(authentication);
    
            super.doFilter(request, response, chain);
        }
    
        public SiteMinderFilter() {
            super();        
        }
    
        @Override
        public void setPrincipalRequestHeader(String principalRequestHeader) {
            super.setPrincipalRequestHeader(principalRequestHeader);
        }
    
        public void setRolesRequestHeader(String rolesRequestHeader) {
            this.rolesRequestHeader = rolesRequestHeader;
        }
    
        public String getRolesRequestHeader() {
            return rolesRequestHeader;
        }
    
    
        public void setRolesDelimiter(String rolesDelimiter) {
            this.rolesDelimiter = rolesDelimiter;
        }
    
        public String getRolesDelimiter() {
            return rolesDelimiter;
        }
    }
    

    AuthenticationImpl

    public class AuthenticationImpl implements Authentication {
        // implement all methods
    }
    

    0 讨论(0)
提交回复
热议问题