mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-06-23 12:32:13 +00:00
Renaming and refactoring of web.xml converter.
This commit is contained in:
parent
118f6401d8
commit
d13faf0815
@ -1,108 +0,0 @@
|
|||||||
package net.sf.acegisecurity.util;
|
|
||||||
|
|
||||||
import org.w3c.dom.Document;
|
|
||||||
import org.w3c.dom.Node;
|
|
||||||
import org.w3c.dom.DOMImplementation;
|
|
||||||
import org.springframework.core.io.ClassPathResource;
|
|
||||||
import org.springframework.core.io.FileSystemResource;
|
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
|
||||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
|
||||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
|
||||||
import org.xml.sax.SAXException;
|
|
||||||
|
|
||||||
import javax.xml.transform.stream.StreamSource;
|
|
||||||
import javax.xml.transform.stream.StreamResult;
|
|
||||||
import javax.xml.transform.Source;
|
|
||||||
import javax.xml.transform.Result;
|
|
||||||
import javax.xml.transform.TransformerFactory;
|
|
||||||
import javax.xml.transform.Transformer;
|
|
||||||
import javax.xml.transform.TransformerException;
|
|
||||||
import javax.xml.transform.OutputKeys;
|
|
||||||
import javax.xml.transform.dom.DOMSource;
|
|
||||||
import javax.xml.transform.dom.DOMResult;
|
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.io.InputStream;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A utility to translate a web.xml file into a set of
|
|
||||||
* acegi security spring beans.
|
|
||||||
*
|
|
||||||
* <p>
|
|
||||||
* This class wraps the XSL transform which actually does
|
|
||||||
* most of the work. It also tests the result by
|
|
||||||
* loading it into a Spring bean factory.
|
|
||||||
* </p>
|
|
||||||
*
|
|
||||||
* @author Luke Taylor
|
|
||||||
* @version $Id$
|
|
||||||
*/
|
|
||||||
public class WebXmlSecurityToSpringBeansTranslator {
|
|
||||||
private String webToSpringXsltFile = "web-to-spring.xsl";
|
|
||||||
private String outputFileName = "applicationContext-acegi-security.xml";
|
|
||||||
private Transformer transformer, identityTransformer;
|
|
||||||
private DefaultListableBeanFactory beanFactory;
|
|
||||||
DocumentBuilderFactory dbf;
|
|
||||||
|
|
||||||
public WebXmlSecurityToSpringBeansTranslator() throws Exception {
|
|
||||||
ClassPathResource resource = new ClassPathResource(webToSpringXsltFile);
|
|
||||||
Source xsltSource = new StreamSource(resource.getInputStream());
|
|
||||||
dbf = DocumentBuilderFactory.newInstance();
|
|
||||||
dbf.setNamespaceAware(true);
|
|
||||||
TransformerFactory tf = TransformerFactory.newInstance();
|
|
||||||
transformer = tf.newTransformer(xsltSource);
|
|
||||||
identityTransformer = tf.newTransformer();
|
|
||||||
identityTransformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//SPRING//DTD BEAN//EN");
|
|
||||||
identityTransformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://www.springframework.org/dtd/spring-beans.dtd");
|
|
||||||
}
|
|
||||||
|
|
||||||
public void translate(InputStream in) throws TransformerException, IOException, ParserConfigurationException, SAXException {
|
|
||||||
DocumentBuilder db = dbf.newDocumentBuilder();
|
|
||||||
Document d = db.parse(in);
|
|
||||||
translate(d);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts the web.xml supplied as a DOM Node
|
|
||||||
*
|
|
||||||
* @param webXml the web application xml
|
|
||||||
*/
|
|
||||||
public void translate(Node webXml) throws IOException, TransformerException, ParserConfigurationException {
|
|
||||||
Source xmlSource = new DOMSource(webXml);
|
|
||||||
DOMResult domResult = new DOMResult();
|
|
||||||
|
|
||||||
transformer.transform(xmlSource, domResult);
|
|
||||||
|
|
||||||
// Obtain DOM for additional manipulation here.
|
|
||||||
Node document = domResult.getNode();
|
|
||||||
|
|
||||||
// Tranform DOM with identity transform to get the output file
|
|
||||||
Result streamResult = new StreamResult(new FileOutputStream(outputFileName));
|
|
||||||
xmlSource = new DOMSource(document);
|
|
||||||
identityTransformer.transform(xmlSource, streamResult);
|
|
||||||
beanFactory = new DefaultListableBeanFactory();
|
|
||||||
XmlBeanDefinitionReader beanReader = new XmlBeanDefinitionReader(beanFactory);
|
|
||||||
beanReader.loadBeanDefinitions(new FileSystemResource(outputFileName));
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getOutputFileName() {
|
|
||||||
return outputFileName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setOutputFileName(String outputFileName) {
|
|
||||||
this.outputFileName = outputFileName;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Mainly intended for testing
|
|
||||||
* @return the bean factory built from the created acegi security application context file
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public BeanFactory getBeanFactory() {
|
|
||||||
return beanFactory;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,115 @@
|
|||||||
|
package net.sf.acegisecurity.util;
|
||||||
|
|
||||||
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
import org.w3c.dom.Node;
|
||||||
|
|
||||||
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
import javax.xml.transform.Source;
|
||||||
|
import javax.xml.transform.Transformer;
|
||||||
|
import javax.xml.transform.TransformerException;
|
||||||
|
import javax.xml.transform.TransformerFactory;
|
||||||
|
import javax.xml.transform.dom.DOMSource;
|
||||||
|
import javax.xml.transform.stream.StreamResult;
|
||||||
|
import javax.xml.transform.stream.StreamSource;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A utility to translate a web.xml file into a set of acegi security spring beans.
|
||||||
|
*
|
||||||
|
* Also produces a new "acegified" web.xml file with the necessary filters installed
|
||||||
|
* and the security elements defined by the servlet DTD removed.
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* This class wraps the XSL transform which actually does most of the work.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author Luke Taylor
|
||||||
|
* @version $Id$
|
||||||
|
*/
|
||||||
|
public class WebXmlToAcegiSecurityConverter {
|
||||||
|
private static final String WEB_TO_SPRING_XSL_FILE = "web-to-spring.xsl";
|
||||||
|
private static final String NEW_WEB_XSLT_FILE = "acegi-web.xsl";
|
||||||
|
|
||||||
|
private Transformer acegiSecurityTransformer, newWebXmlTransformer;
|
||||||
|
private DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name of the spring-beans file which the beans will be stored in.
|
||||||
|
* This is required when writing the new web.xml file.
|
||||||
|
*/
|
||||||
|
private String acegiOutputFileName = "applicationContext-acegi-security.xml";
|
||||||
|
|
||||||
|
/** The web.xml content to be converted */
|
||||||
|
private DOMSource xmlSource;
|
||||||
|
/** The results of the conversion */
|
||||||
|
private String newWebXml, acegiBeansXml;
|
||||||
|
|
||||||
|
public WebXmlToAcegiSecurityConverter() throws Exception {
|
||||||
|
TransformerFactory tf = TransformerFactory.newInstance();
|
||||||
|
|
||||||
|
acegiSecurityTransformer = tf.newTransformer(createTransformerSource(WEB_TO_SPRING_XSL_FILE));
|
||||||
|
newWebXmlTransformer = tf.newTransformer(createTransformerSource(NEW_WEB_XSLT_FILE));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Source createTransformerSource(String fileName) throws IOException {
|
||||||
|
ClassPathResource resource = new ClassPathResource(fileName);
|
||||||
|
return new StreamSource(resource.getInputStream());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs the transformations on the input source.
|
||||||
|
* Creates new web.xml content and a set of acegi-security Spring beans which can be
|
||||||
|
* accessed through the appropriate getter methods.
|
||||||
|
*/
|
||||||
|
public void doConversion() throws IOException, TransformerException {
|
||||||
|
Assert.notNull(xmlSource, "The XML input must be set, either as a Node or an InputStream");
|
||||||
|
|
||||||
|
// Create the modified web.xml file
|
||||||
|
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||||
|
newWebXmlTransformer.transform(xmlSource, new StreamResult(output));
|
||||||
|
newWebXml = output.toString();
|
||||||
|
output.reset();
|
||||||
|
|
||||||
|
// acegiSecurityTransformer.setParameter("cas-proxy-url", "http://localhost:8433/cas/proxy");
|
||||||
|
acegiSecurityTransformer.setParameter("acegi-security-context-file", acegiOutputFileName);
|
||||||
|
acegiSecurityTransformer.transform(xmlSource, new StreamResult(output));
|
||||||
|
acegiBeansXml = output.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** set the input as an InputStream */
|
||||||
|
public void setInput(InputStream xmlIn) throws Exception {
|
||||||
|
DocumentBuilder db = dbf.newDocumentBuilder();
|
||||||
|
setInput(db.parse(xmlIn));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** set the input as an XML node */
|
||||||
|
public void setInput(Node webXml) {
|
||||||
|
this.xmlSource = new DOMSource(webXml);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAcegiOutputFileName() {
|
||||||
|
return acegiOutputFileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAcegiOutputFileName(String acegiOutputFileName) {
|
||||||
|
this.acegiOutputFileName = acegiOutputFileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns the converted web.xml content */
|
||||||
|
public String getNewWebXml() {
|
||||||
|
return newWebXml;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the created spring-beans xml content which should be used in
|
||||||
|
* the application context file.
|
||||||
|
*/
|
||||||
|
public String getAcegiBeansXml() {
|
||||||
|
return acegiBeansXml;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -3,7 +3,10 @@ package net.sf.acegisecurity.util;
|
|||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.core.io.AbstractResource;
|
||||||
|
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||||
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||||
|
import org.w3c.dom.Node;
|
||||||
|
|
||||||
import net.sf.acegisecurity.providers.ProviderManager;
|
import net.sf.acegisecurity.providers.ProviderManager;
|
||||||
import net.sf.acegisecurity.providers.dao.DaoAuthenticationProvider;
|
import net.sf.acegisecurity.providers.dao.DaoAuthenticationProvider;
|
||||||
@ -13,22 +16,30 @@ import net.sf.acegisecurity.intercept.web.SecurityEnforcementFilter;
|
|||||||
|
|
||||||
import net.sf.acegisecurity.intercept.web.FilterSecurityInterceptor;
|
import net.sf.acegisecurity.intercept.web.FilterSecurityInterceptor;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests the WebXmlSecurityToSpringBeansTranslator by applying it
|
* Tests the WebXmlToAcegiSecurityConverter by applying it to a sample web.xml file.
|
||||||
* to a test sample web.xml file.
|
|
||||||
*
|
*
|
||||||
* @author Luke Taylor
|
* @author Luke Taylor
|
||||||
* @version $Id$
|
* @version $Id$
|
||||||
*/
|
*/
|
||||||
public class WebXmlSecurityToSpringBeansTranslatorTests extends TestCase {
|
public class WebXmlToAcegiSecurityConverterTests extends TestCase {
|
||||||
|
|
||||||
public void testFileTranslation() throws Exception {
|
public void testFileConversion() throws Exception {
|
||||||
WebXmlSecurityToSpringBeansTranslator t = new WebXmlSecurityToSpringBeansTranslator();
|
WebXmlToAcegiSecurityConverter t = new WebXmlToAcegiSecurityConverter();
|
||||||
|
|
||||||
Resource r = new ClassPathResource("test-web.xml");
|
Resource r = new ClassPathResource("test-web.xml");
|
||||||
t.translate(r.getInputStream());
|
t.setInput(r.getInputStream());
|
||||||
|
t.doConversion();
|
||||||
|
|
||||||
BeanFactory bf = t.getBeanFactory();
|
|
||||||
|
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||||
|
XmlBeanDefinitionReader beanReader = new XmlBeanDefinitionReader(bf);
|
||||||
|
|
||||||
|
int nBeans = beanReader.loadBeanDefinitions(new InMemoryResource(t.getAcegiBeansXml()));
|
||||||
assertNotNull(bf.getBean("filterChainProxy"));
|
assertNotNull(bf.getBean("filterChainProxy"));
|
||||||
|
|
||||||
ProviderManager pm = (ProviderManager) bf.getBean("authenticationManager");
|
ProviderManager pm = (ProviderManager) bf.getBean("authenticationManager");
|
||||||
@ -56,4 +67,24 @@ public class WebXmlSecurityToSpringBeansTranslatorTests extends TestCase {
|
|||||||
FilterSecurityInterceptor fsi = sef.getFilterSecurityInterceptor();
|
FilterSecurityInterceptor fsi = sef.getFilterSecurityInterceptor();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class InMemoryResource extends AbstractResource {
|
||||||
|
ByteArrayInputStream in;
|
||||||
|
|
||||||
|
public InMemoryResource(ByteArrayInputStream in) {
|
||||||
|
this.in = in;
|
||||||
|
}
|
||||||
|
|
||||||
|
public InMemoryResource(String source) {
|
||||||
|
in = new ByteArrayInputStream(source.getBytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return in.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public InputStream getInputStream() throws IOException {
|
||||||
|
return in;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user