我们使用 WebLogic提供的 API进行用户身份验证,这样才能完成一个让 WebLogic Server认为是合法的登录。这个weblogic.servlet.security.ServletAuthentication提供的方法具体实现在不同的SP版本中总有变化,比如WLS8.1 SP3和8.1 SP4,因此需要留意这一点。本文方案的实现,以WELS8.1SP4为准。
public static int login(java.lang.String username,
java.lang.String password,
javax.servlet.http.HttpServletRequest request)
throws javax.security.auth.login.LoginException
Returns an int value for AUTHENTICATED or FAILED_AUTHENTICATION after using the username and password to authenticate the user and setting that user information into the session. This method is similar to "weak", except that the LoginException is propogated to caller.
Parameters:
username - String
password - String
request - HttpServletRequest
Returns:
int authentication value
Throws:
javax.security.auth.login.LoginException -
IDP根据用户信息生成相应的SAML Assertion Token
这里的代码来自于opensaml提供的POSTProfileTest.java,用于生成 SAMLResponse并进行签名
SAMLResponse r = SAMLPOSTProfile.prepare(
"www.opensaml.org",
"www.opensaml.org",
Collections.singleton("http://www.opensaml.org"),
"foo",
"foo",
null,
"127.0.0.1",
"foo",
new Date(),
Collections.singleton(
new SAMLAuthorityBinding(SAMLBinding.SAML_SOAP_HTTPS,
"http://www.opensaml.org",
new QName(XML.SAMLP_NS,"AttributeQuery")
)
)
);
assertNotNull("No SAMLResponse was generated.",r);
Iterator i = r.getAssertions();
((SAMLAssertion)i.next()).sign(
XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1,
ks.getKey(alias,password),
Arrays.asList(ks.getCertificateChain(alias))
);
r.sign(
XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA1,
ks.getKey(alias,password),
Arrays.asList(ks.getCertificateChain(alias))
);
Identity Assertion Provider校验IDP生成的Token
具体Identity Assertion Provider的实现超出了本文讨论的篇幅。这里只描述其需要实现的主要逻辑,同样来自于POSTProfileTest.java
assertTrue("SAMLResponse is not signed.",r.isSigned());
System.err.println("================ Generated Response ===============");
r.toStream(System.err);
System.err.println();
r.verify(ks.getCertificate(alias));
SAMLResponse r2 = SAMLPOSTProfile.accept(r.toBase64(), "www.opensaml.org", 60, true);
assertTrue("SAMLResponse is not signed.",r2.isSigned());
SAMLPOSTProfile.getSSOAssertion(r2,Collections.singleton("http://www.opensaml.org")).verify(ks.getCertificate(alias));
r2.verify(ks.getCertificate(alias));
System.err.println("================ Verified Response ===============");
r2.toStream(System.err);
System.err.println();
只要将上面的逻辑放入在Identity Assertion Provider中就可以了。