I have did the same in below way,
I have implemented my custom user details object and populated values from SAML Response and mapped each user fields into user object attributes and set the same in Principal of SpringContext.
- create your own custom user details object using the class "mrpSAMLUserDetails" .
E.g.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import org.apache.log4j.Logger;
import org.opensaml.saml2.core.Attribute;
import org.opensaml.xml.XMLObject;
import org.opensaml.xml.schema.XSString;
import org.opensaml.xml.schema.impl.XSAnyImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.hierarchicalroles.UserDetailsServiceWrapper;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.saml.SAMLCredential;
import org.springframework.security.saml.userdetails.SAMLUserDetailsService;
public class mrpSAMLUserDetails implements SAMLUserDetailsService {
private static Logger logger = Logger.getLogger(mrpSAMLUserDetails.class);
@Override
public Object loadUserBySAML(SAMLCredential mrpSAMLCredential)
throws UsernameNotFoundException {
logger.info("mrp custom loaduserBySAML Entered");
boolean enabled=true,accountNONExpired=true,credentialNONExpired=true,accountNONLocked=true;
String username = "",password="",role="",fetchRole="Role",sourceOfLogin="";
//String[] roleArray = null;
List<String> samlRoles = new ArrayList<String>();
GrantedAuthority authority = new SimpleGrantedAuthority("mrpnoneROLE");
List<GrantedAuthority> authoritiesList = new ArrayList<GrantedAuthority>();
username = mrpSAMLCredential.getNameID().getValue();
username = username.toLowerCase();
role= mrpSAMLCredential.getAttributeAsString("Role").toString();
//String[] roleArray = mrpSAMLCredential.getAttributeAsStringArray(samlRoleName);
authority = new SimpleGrantedAuthority(tempRole);
authoritiesList.add(authority);
UserDetails mrpuser=new User(username,password,enabled,accountNONExpired,credentialNONExpired,accountNONLocked,authoritiesList);
logger.info("mrp custom loaduserBySAML DONE!!!");
return mrpuser;
}
}
configure the "mrpSAMLUserDetails" in your spring-security.xml file like..
<property name="userDetails" ref="mrpcustomuser" />
<property name="forcePrincipalAsString" value="false"/>
</bean>
<bean id="mrpcustomuser" class="com.mrp.sso.security.mrpSAMLUserDetails" />
After this change you can access email,username, role etc from SpringSecurity Principal Object.