package com.xxx;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.joda.time.DateTime;
import org.opensaml.Configuration;
import org.opensaml.saml2.core.*;
import org.opensaml.saml2.core.impl.AssertionBuilder;
import org.opensaml.saml2.core.impl.ResponseBuilder;
import org.opensaml.saml2.metadata.AssertionConsumerService;
import org.opensaml.saml2.metadata.Endpoint;
import org.opensaml.xml.XMLObjectBuilderFactory;
import org.opensaml.xml.security.CriteriaSet;
import org.opensaml.xml.security.credential.Credential;
import org.opensaml.xml.security.credential.CredentialResolver;
import org.opensaml.xml.security.credential.UsageType;
import org.opensaml.xml.security.criteria.EntityIDCriteria;
import org.opensaml.xml.security.criteria.UsageCriteria;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import com.xxx.saml.idp.BindingAdapter;
import com.xxx.saml.idp.IdpConfiguration;
import com.xxx.saml.idp.UIASAttributeStatementGenerator;
import com.xxx.saml.util.IDService;
import com.xxx.saml.util.TimeService;
import com.xxx.saml.xml.AttributeStatementGenerator;
import com.xxx.saml.xml.AuthnStatementGenerator;
import com.xxx.saml.xml.EndpointGenerator;
import com.xxx.saml.xml.IssuerGenerator;
import com.xxx.saml.xml.StatusGenerator;
import com.xxx.saml.xml.SubjectGenerator;
import com.xxx.uias.userInfo.pojo.Privilege;
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 2572744487603163969L;
private final XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();
private TimeService timeService = new TimeService();
private IDService idService = new IDService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
request.setCharacterEncoding("utf-8"); //设置编码
response.setContentType("text/html; charset=utf-8");
String appCode = "demo333";
String issuingEntityName = "UIAS";
//http://172.16.15.109:5050/UIASClientDemo/samlsp/samlAuth.do?appCode=demo333
String acURL = "http://172.16.15.109:5050/UIASClientDemo/samlsp/AssertionConsumerService";
int validForInSeconds = 300;
String username = "testccc";
String ip = "172.16.15.109";
String inResponseTo = "";
DateTime authnInstant = new DateTime(System.currentTimeMillis());
AuthnStatementGenerator authnStatementGenerator = new AuthnStatementGenerator();
AttributeStatementGenerator attributeStatementGenerator = new AttributeStatementGenerator();
IssuerGenerator issuerGenerator = new IssuerGenerator(issuingEntityName);
SubjectGenerator subjectGenerator = new SubjectGenerator(timeService);
StatusGenerator statusGenerator = new StatusGenerator();
//1.生成authResponse
EndpointGenerator endpointGenerator = new EndpointGenerator();
Endpoint endpoint = endpointGenerator.generateEndpoint(AssertionConsumerService.DEFAULT_ELEMENT_NAME, acURL, null);
//2.
ResponseBuilder responseBuilder = (ResponseBuilder) builderFactory.getBuilder(Response.DEFAULT_ELEMENT_NAME);
Response authResponse = responseBuilder.buildObject();
Issuer responseIssuer = issuerGenerator.generateIssuer();
AssertionBuilder assertionBuilder = (AssertionBuilder)builderFactory.getBuilder(Assertion.DEFAULT_ELEMENT_NAME);
Assertion assertion = assertionBuilder.buildObject();
Subject subject = subjectGenerator.generateSubject(acURL, validForInSeconds, username, inResponseTo, ip);
Issuer issuer = issuerGenerator.generateIssuer();
AuthnStatement authnStatement = authnStatementGenerator.generateAuthnStatement(authnInstant);
assertion.setIssuer(issuer);
assertion.setSubject(subject);
assertion.setID(idService.generateID());
assertion.setIssueInstant(timeService.getCurrentDateTime());
assertion.getAuthnStatements().add(authnStatement);
//权限
Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();;
for(Privilege p: Privilege.values()){
authorities.add(new SimpleGrantedAuthority("ROLE_" + p.name()));
}
assertion.getAttributeStatements().add(attributeStatementGenerator.generateAttributeStatement(authorities));
//其他属性
UIASAttributeStatementGenerator attrState = new UIASAttributeStatementGenerator();
Map<String, String> attrMap = new HashMap<String, String>();
attrMap.put("appCode", appCode);
attrMap.put("name", "张三");
assertion.getAttributeStatements().add(attrState.generateAttributeStatement(attrMap));
authResponse.getAssertions().add(assertion);
authResponse.setIssuer(responseIssuer);
authResponse.setID(idService.generateID());
authResponse.setIssueInstant(timeService.getCurrentDateTime());
authResponse.setInResponseTo(inResponseTo);
authResponse.setDestination(acURL);
authResponse.setStatus(statusGenerator.generateStatus(StatusCode.SUCCESS_URI));
//JKS
CriteriaSet criteriaSet = new CriteriaSet();
criteriaSet.add(new EntityIDCriteria(issuingEntityName));
criteriaSet.add(new UsageCriteria(UsageType.SIGNING));
CredentialResolver credentialResolver = IdpConfiguration.buildJKSCredentialResolver();
Credential signingCredential = credentialResolver.resolveSingle(criteriaSet);
BindingAdapter adapter = IdpConfiguration.buildBindingAdapter();
adapter.sendSAMLMessage(authResponse, endpoint, signingCredential, response);
} catch (Exception e) {
e.printStackTrace();
}
}
}