java saml_Java使用OpenSAML创建一个SAML Assertion(二)
参数资料: OpenSAML官方
需要Jar包:xalan.jar (2.7.1), xercesImpl.jar, xml-apis.jar,opensaml-1.1.jar, xmlsec-20050514.jar,log4j-1.2.5.jar, commons-logging-1.03.jar, commons-codec-1.3.jar
代码案例:import org.opensaml.DefaultBootstrap;
import org.opensaml.Configuration;
import org.opensaml.saml1.core.Assertion;
import org.opensaml.saml1.core.Attribute;
import org.opensaml.saml1.core.AttributeValue;
import org.opensaml.saml1.core.NameIdentifier;
import org.opensaml.saml1.core.Subject;
import org.opensaml.saml1.core.SubjectConfirmation;
import org.opensaml.saml1.core.SubjectStatement;
import org.opensaml.saml1.core.AuthenticationStatement;
import org.opensaml.saml1.core.AttributeStatement;
import org.opensaml.saml1.core.ConfirmationMethod;
import org.opensaml.saml1.core.Conditions;
import org.opensaml.saml1.core.DoNotCacheCondition;
import org.opensaml.saml1.core.impl.AssertionBuilder;
import org.opensaml.saml1.core.impl.AssertionImpl;
import org.opensaml.saml1.core.impl.AssertionMarshaller;
import org.opensaml.saml1.core.impl.SubjectImpl;
import org.opensaml.common.SAMLVersion;
import org.opensaml.common.SAMLObjectBuilder;
import org.opensaml.common.xml.SAMLConstants;
import org.opensaml.xml.XMLObjectBuilder;
import org.opensaml.xml.XMLObjectBuilderFactory;
import org.opensaml.xml.util.XMLHelper;
import org.opensaml.xml.util.XMLHelper;
import org.opensaml.xml.schema.XSString;
import org.w3c.dom.Element;
import org.joda.time.DateTime;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
public class AMUserAssertion {
private static String strIssuer = "Example:FrontEnd";
private static String strNameID = "testUserID";
private static String strNameQualifier = "Example:FrontEnd";
private static String strNamespace = "urn:bea:security:saml:groups";
private static String strAttrName = "Groups";
private static String strAuthMethod = "SunAccessManager";
public static void main(String args[]) {
try {
// OpenSAML 2.3
DefaultBootstrap.bootstrap();
XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();
// Create the NameIdentifier
SAMLObjectBuilder nameIdBuilder = (SAMLObjectBuilder) builderFactory.getBuilder(NameIdentifier.DEFAULT_ELEMENT_NAME);
NameIdentifier nameId = nameIdBuilder.buildObject();
nameId.setNameIdentifier(strNameID);
nameId.setNameQualifier(strNameQualifier);
nameId.setFormat(NameIdentifier.UNSPECIFIED);
// Create the SubjectConfirmation
SAMLObjectBuilder confirmationMethodBuilder = (SAMLObjectBuilder) builderFactory.getBuilder(ConfirmationMethod.DEFAULT_ELEMENT_NAME);
ConfirmationMethod confirmationMethod = confirmationMethodBuilder.buildObject();
confirmationMethod.setConfirmationMethod("urn:oasis:names:tc:SAML:1.0:cm:sender-vouches");
SAMLObjectBuilder subjectConfirmationBuilder = (SAMLObjectBuilder) builderFactory.getBuilder(SubjectConfirmation.DEFAULT_ELEMENT_NAME);
SubjectConfirmation subjectConfirmation = subjectConfirmationBuilder.buildObject();
subjectConfirmation.getConfirmationMethods().add(confirmationMethod);
// Create the Subject
SAMLObjectBuilder subjectBuilder = (SAMLObjectBuilder) builderFactory.getBuilder(Subject.DEFAULT_ELEMENT_NAME);
Subject subject = subjectBuilder.buildObject();
subject.setNameIdentifier(nameId);
subject.setSubjectConfirmation(subjectConfirmation);
// Create Authentication Statement
SAMLObjectBuilder authStatementBuilder = (SAMLObjectBuilder) builderFactory.getBuilder(AuthenticationStatement.DEFAULT_ELEMENT_NAME);
AuthenticationStatement authnStatement = authStatementBuilder.buildObject();
authnStatement.setSubject(subject);
authnStatement.setAuthenticationMethod(strAuthMethod);
authnStatement.setAuthenticationInstant(new DateTime());
// Create the attribute statement
SAMLObjectBuilder attrBuilder = (SAMLObjectBuilder) builderFactory.getBuilder(Attribute.DEFAULT_ELEMENT_NAME);
Attribute attrGroups = attrBuilder.buildObject();
attrGroups.setAttributeName("Groups");
XMLObjectBuilder stringBuilder = builderFactory.getBuilder(XSString.TYPE_NAME);
XSString attrNewValue = (XSString) stringBuilder.buildObject(AttributeValue.DEFAULT_ELEMENT_NAME, XSString.TYPE_NAME);
attrNewValue.setValue("AssetManager");
attrGroups.getAttributeValues().add(attrNewValue);
SAMLObjectBuilder attrStatementBuilder = (SAMLObjectBuilder) builderFactory.getBuilder(AttributeStatement.DEFAULT_ELEMENT_NAME);
AttributeStatement attrStatement = attrStatementBuilder.buildObject();
attrStatement.getAttributes().add(attrGroups);
// attrStatement.setSubject(subject);
// Create the do-not-cache condition
SAMLObjectBuilder doNotCacheConditionBuilder = (SAMLObjectBuilder) builderFactory.getBuilder(DoNotCacheCondition.DEFAULT_ELEMENT_NAME);
DoNotCacheCondition condition = doNotCacheConditionBuilder.buildObject();
SAMLObjectBuilder conditionsBuilder = (SAMLObjectBuilder) builderFactory.getBuilder(Conditions.DEFAULT_ELEMENT_NAME);
Conditions conditions = conditionsBuilder.buildObject();
conditions.getConditions().add(condition);
// Create the assertion
SAMLObjectBuilder assertionBuilder = (SAMLObjectBuilder) builderFactory.getBuilder(Assertion.DEFAULT_ELEMENT_NAME);
Assertion assertion = assertionBuilder.buildObject();
assertion.setIssuer(strIssuer);
assertion.setIssueInstant(new DateTime());
assertion.setVersion(SAMLVersion.VERSION_10);
assertion.getAuthenticationStatements().add(authnStatement);
assertion.getAttributeStatements().add(attrStatement);
assertion.setConditions(conditions);
// Print the assertion to standard output
AssertionMarshaller marshaller = new AssertionMarshaller();
Element element = marshaller.marshall(assertion);
System.out.println("AMUserAssertion (SAML 1):\\n");
System.out.println(XMLHelper.prettyPrintXML(element));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
输出内容:<?xml version="1.0" encoding="UTF-8"?>
testUserIDlt;/saml1:NameIdentifier>
urn:oasis:names:tc:SAML:1.0:cm:sender-vouches
AssetManager
java saml_Java使用OpenSAML创建一个SAML Assertion(二)相关推荐
- java.io,PrintWriter可以用来创建一个文件并向本文文件写入数据
java.io,PrintWriter可以用来创建一个文件并向本文文件写入数据 PrintWriter(String filename); PrintWriter(File file);<!-- ...
- java数组及Arrays创建一个int 类型数组 数组元素由键盘录入,每次打印插入排序的结果(数组扩容,数组排序,键盘录入)
@author silence丶你的名字 java数组及Arrays创建一个int 类型数组 数组元素由键盘录入,每次打印插入排序的结果 初始数组元素1 for死循环 获取用户录入的数据,如果为952 ...
- 【小题目】写JAVA程序时可以创建一个名为123.java的源文件吗
在写JAVA程序时创建名为123.java的源文件,前提是该源文件中没有public修饰的类,因为一旦使用public修饰后类名必须与文件名相同,而类名为标识符,不能以数字开头.因此,在没有用publ ...
- java存款输入密码类,创建一个银行账户类,包括账号、用户姓名、密码和余额属性,实现开户、查询余额、取款、存款功能。...
用类与对象创建一个银行账户类.银行账户类包括账号.用户姓名.密码和余额属性,银行账户类的功能包括: 开户 查询余额 取款 存款 功能实现思路: 开户方法 Account(),创建账户的账号.密码.姓名 ...
- Java使用IntelliJ IDEA创建一个基于Swing的GUI图形化程序,打包发布为jar
1.创建GUI窗体 首先,直接新建一个默认的空的Java模块即可,随便取个名字 之后再src目录下右键,新建,创建一个Swing的GUI窗体,随便取个名字 给主窗体改个名字 到java代码中生成一个窗 ...
- Java练习题 类 先创建一个Point类,然后定义Trianglele类。在Trianglele类中定义三个Point的实体来表示一个三角形的三个点,然后定义两个方法求三角形的周长、面积。
先创建一个Point类,然后定义Trianglele类. 在Trianglele类中定义三个Point的实体来表示一个三角形的三个点,再定义构造方法对这三个点进行初始化,然后定义两个方法求三角形的周长 ...
- Java中,File创建一个文件
import java.io.File; import java.io.IOException; /** * * 如何用File创建一个文件 */ public class test1 {public ...
- Java图形window界面创建一个文本框和一个按钮awt容器
package com.java;import java.awt.*;public class HelloWorld {public static void main(String[] args) { ...
- java hdfs 新建目录_如何用java在hdfs中创建一个新目录?
public static void main(String[] args) throws IOException, URISyntaxException { ????????????配置config ...
最新文章
- FTP的主动模式和被动模式,你应该用那种?
- javaWeb服务详解【客户端调用】(含源代码,测试通过,注释) ——applicationContext.xml
- 20 个免费的 jQuery 的工具提示插件:
- 证券基金行业大数据实施痛点及选型关注点
- 微波天线及各种天线概念解析
- GitHub 优秀的 Android 开源项目
- python tkinter窗口组件Canva
- 已知从1970年1月1日0分0秒到目前的总秒数计算当前时间
- 一加6手机图片中的文字如何识别?
- 《STM32学习笔记》4——核心功能电路与编程(下)
- 5-(4-氨基苯基)-10,15.20-三苯基卟啉(TPP-NH2)/多金属氧酸盐-四苯基卟啉化合物TPP-PW齐岳供应
- 二维码生成器怎么在线制作?制作二维码其实很简单
- [Linux]线程概念_线程控制(线程与进程的区别与联系 | 线程创建 | 线程等待 | 线程终止 | 线程分离 | LWP)
- 一图秒懂 - MD文件语法标签,最简单的markdown入门
- 黑客也看《人民的名义》:把手机卡扔马桶有 bug
- {转}我们需要什么样的人生
- Mac下查看已安装的jdk版本及其安装目录
- 数字程控交换机的发展及工作原理
- 常用正则表达式及学习
- [OpenWRT]极路由B70(HC5962)刷OpenWRT笔记(一)