- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我开发了一个 Java 网络应用程序,我想实现 SAML。这些是我认为实现 SAML 的正确步骤。
我有一个示例代码,我可以创建 SAML 请求以及类似的请求
<samlp:AuthnRequest xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
ID="_c7b796f4-bc16-4fcc-8c1d-36befffc39c2" Version="2.0"
IssueInstant="2014-10-30T11:21:08Z" ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
AssertionConsumerServiceURL="http://localhost:8080/mywebapp/consume.jsp">
<saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">http://localhost:8080/mywebapp
</saml:Issuer>
<samlp:NameIDPolicy
Format="urn:oasis:names:tc:SAML:2.0:nameid-format:unspecified"
AllowCreate="true"></samlp:NameIDPolicy>
<samlp:RequestedAuthnContext Comparison="exact">
<saml:AuthnContextClassRef xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport
</saml:AuthnContextClassRef>
</samlp:RequestedAuthnContext>
</samlp:AuthnRequest>
我可以对其进行编码并发送给 IdP。
我想创建示例 Java 代码来获取此 SAML 请求,然后创建 SAML 响应。如何解码请求并验证它并创建响应?我需要用证书签署 saml 响应吗?然后发回SP?
谢谢。
最佳答案
虽然这是一篇旧帖子,但我添加了我认为有用的示例代码和引用资料。
SAMLResponse = hreq.getParameter("SAMLResponse");
InputSource inputSource = new InputSource(new StringReader(SAMLResponse));
SAMLReader samlReader = new SAMLReader();
response2 = org.opensaml.saml2.core.Response)samlReader.readFromFile(inputSource);
现在验证数字签名:
org.opensaml.saml2.core.Response response2 = (org.opensaml.saml2.core.Response)samlReader.readFromFile(inputSource);
//To fetch the digital signature from the response.
Signature signature = response2.getSignature();
X509Certificate certificate = (X509Certificate) keyStore.getCertificate(domainName);
//pull out the public key part of the certificate into a KeySpec
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(certificate.getPublicKey().getEncoded());
//get KeyFactory object that creates key objects, specifying RSA - java.security.KeyFactory
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//generate public key to validate signatures
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
//we have the public key
BasicX509Credential publicCredential = new BasicX509Credential();
//add public key value
publicCredential.setPublicKey(publicKey);
//create SignatureValidator
SignatureValidator signatureValidator = new SignatureValidator(publicCredential);
//try to validate
try{
signatureValidator.validate(signature);
catch(Exception e){
//
}
现在获取断言图:
samlDetailsMap = setSAMLDetails(response2);
在上面的逻辑中,使用下面的私有(private)方法来拉取所有断言属性。最后,您将获得发送给您的所有字段的 map 。
private Map<String, String> setSAMLDetails(org.opensaml.saml2.core.Response response2){
Map<String, String> samlDetailsMap = new HashMap<String, String>();
try {
List<Assertion> assertions = response2.getAssertions();
LOGGER.error("No of assertions : "+assertions.size());
for(Assertion assertion:assertions){
List<AttributeStatement> attributeStatements = assertion.getAttributeStatements();
for(AttributeStatement attributeStatement: attributeStatements){
List<Attribute> attributes = attributeStatement.getAttributes();
for(Attribute attribute: attributes){
String name = attribute.getName();
List<XMLObject> attributes1 = attribute.getAttributeValues();
for(XMLObject xmlObject : attributes1){
if(xmlObject instanceof XSString){
samlDetailsMap.put(name, ((XSString) xmlObject).getValue());
LOGGER.error("Name is : "+name+" value is : "+((XSString) xmlObject).getValue());
}else if(xmlObject instanceof XSAnyImpl){
String value = ((XSAnyImpl) xmlObject).getTextContent();
samlDetailsMap.put(name, value);
}
}
}
}
}
} catch (Exception e) {
LOGGER.error("Exception occurred while setting the saml details");
}
LOGGER.error("Exiting from setSAMLDetails method");
return samlDetailsMap;
}
添加新类 SAMLReader 如下:
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.opensaml.DefaultBootstrap;
import org.opensaml.xml.Configuration;
import org.opensaml.xml.XMLObject;
import org.opensaml.xml.io.UnmarshallingException;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class SAMLReader {
private static DocumentBuilder builder;
static{
try{
DefaultBootstrap.bootstrap ();
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance ();
factory.setNamespaceAware (true);
builder = factory.newDocumentBuilder ();
}catch (Exception ex){
ex.printStackTrace ();
}
}
/**
*
* @param filename
* @return
* @throws IOException
* @throws UnmarshallingException
* @throws SAXException
*/
public XMLObject readFromFile (String filename)
throws IOException, UnmarshallingException, SAXException{
return fromElement (builder.parse (filename).getDocumentElement ());
}
/**
*
* @param is
* @return
* @throws IOException
* @throws UnmarshallingException
* @throws SAXException
*/
public XMLObject readFromFile (InputStream is)
throws IOException, UnmarshallingException, SAXException{
return fromElement (builder.parse (is).getDocumentElement ());
}
/**
*
* @param is
* @return
* @throws IOException
* @throws UnmarshallingException
* @throws SAXException
*/
public XMLObject readFromFile (InputSource is)
throws IOException, UnmarshallingException, SAXException{
return fromElement (builder.parse (is).getDocumentElement ());
}
/**
*
* @param element
* @return
* @throws IOException
* @throws UnmarshallingException
* @throws SAXException
*/
public static XMLObject fromElement (Element element)
throws IOException, UnmarshallingException, SAXException{
return Configuration.getUnmarshallerFactory ()
.getUnmarshaller (element).unmarshall (element);
}
关于java - 根据 SAML 请求创建 SAML 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26647664/
阅读自 http://en.wikipedia.org/wiki/SAML_2.0 ,我试图了解保证在流程的第 5 步中发送的 SAMLResponse 的真实性的机制。 根据我的理解,SAML 的设
是否可以在 SAML 身份验证请求中发送属性? https://sp.example.com/SAML2 最佳答案 从技术上讲,是的,这是可能的,因为 AuthnRequest 可
在阅读了一些文章和引用资料后,我发现它们实际上说明了什么是 SAML,它包含哪些组件,它是如何工作的。一些不错的链接如下: Good documentation about Shibboleth an
我看过一堆关于如何通过重定向在身份提供商 (IdP)、服务提供商 (SP) 和浏览器之间传递的流程图。然而,现在对我来说似乎没有必要,所以我知道我错过了一些东西。 有人可以向我提供一个使用案例,其中与
我需要在 SAML 请求中添加一个请求参数(例如 locale=en),以便让登录页面显示正确的语言。我怎么做? 我试图将属性添加到作为参数发送到开始方法 (SamlEntryPoint) 的 Htt
我在将我的应用程序与 SAML 集成时遇到问题。 以下是我的错误: org.springframework.security.saml.SAMLProcessingFilter.attemptAuth
我是 SAML 的新手,对预期的签名和信任过程感到困惑。 我正在编写一个 SP 并从 IDP 接收到一个签名的 samlp:Response,其中包括 KeyInfo:
ADFS 具有看似错误的意外行为。 我有使用 SHA1 哈希算法进行数字签名的 SP。 在此 SP 的 ADFS 上,我在高级选项卡上设置为使用 SHA256。 对我来说,这些不相关是正常的,每一方选
我正在使用 SAML 登录我的应用程序,我想在登录响应断言的属性中包含用户组。我想知道登录请求是否应指定该属性是必需的,或者这是一般需要在 IDP 上完成的配置,还是在专门为我的服务提供商的 IDP
我的任务是设计一个非常简单的 SSO(单点登录)流程。我的雇主已指定它应该在 SAML 中实现。我想在确认 SAML 规范的同时创建尽可能简单的消息。 如果你们中的一些人能查看我的请求和响应消息,并告
我们正在使用ruby-saml将我们的应用程序建立为服务提供商,同时使用 Google 作为身份提供商,尽管我不认为这个问题特定于 Ruby 或该项目。 我见过this answer from the
我们正在使用ruby-saml将我们的应用程序建立为服务提供商,同时使用 Google 作为身份提供商,尽管我不认为这个问题特定于 Ruby 或该项目。 我见过this answer from the
有人建议 SAML token 应该有效多长时间(在 SOA 基础架构中)?我想到了几个 (6-12) 小时。 非常感谢马库斯 最佳答案 让您的 token 拥有如此长的生命周期通常不是一个好主意,因
我对 SAML 证书的这个概念很陌生。 我目前正在为网站配置 SSO,需要知道如何生成 SAML 证书?我为本网站使用的设置不是通过 Azure,而是直接来自供应商网站,他们正在请求我的 SAML 证
我是 SAML 的新手。您能否用简单的英语解释一下什么是 SAML 配置文件和绑定(bind),并提供几个示例。 最佳答案 nrathus 在他的评论中指出,维基百科的 entry on SAML是一
我在 Keycloak Server 4.2.1 中使用 saml 协议(protocol)注销时遇到问题。在 Keycloak 旁边有一个 Wildfly 9 服务器。在 Wildfly 服务器中部
有人能解释一下 和有什么区别吗?收件人和 观众 在 SAML 2.0 中? 我在这里从 OneLogin 中找到了非常模糊的解释: https://support.onelogin.com/hc/en
我知道在 SAML 协议(protocol)中,IDP 和 SP 他们拥有自己的 key 对,并且不会将他们的私钥暴露给对方。 我假设下面的领域 key 是 IDP key 对,这是有道理的,因为私钥
场景: 浏览器(用户)向服务提供商 (SP) 请求资源。 SP 重定向(通过 SAML 请求)到身份提供商 (IdP)。 由于是首次登录,用户会向 (IdP) 提供他/她的有效凭据。 然后,IdP 将
JWT(Json Web Token)和 SAML 的主要区别是什么?请向我推荐任何带有 Spring 安全性的示例。提前致谢。 最佳答案 两个SAML和 JWT是不依赖于任何编程语言的安全 toke
我是一名优秀的程序员,十分优秀!