mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-07-04 17:52:15 +00:00
Changes to exception handling, and some additional validation of web.xml content.
This commit is contained in:
parent
e51c38aec9
commit
28e8c93beb
@ -5,17 +5,16 @@ import org.springframework.util.Assert;
|
|||||||
import org.dom4j.Document;
|
import org.dom4j.Document;
|
||||||
import org.dom4j.DocumentHelper;
|
import org.dom4j.DocumentHelper;
|
||||||
import org.dom4j.DocumentException;
|
import org.dom4j.DocumentException;
|
||||||
|
import org.dom4j.Node;
|
||||||
import org.dom4j.io.SAXReader;
|
import org.dom4j.io.SAXReader;
|
||||||
import org.dom4j.io.DocumentSource;
|
import org.dom4j.io.DocumentSource;
|
||||||
import org.dom4j.io.DocumentResult;
|
import org.dom4j.io.DocumentResult;
|
||||||
|
|
||||||
import javax.xml.transform.Source;
|
import javax.xml.transform.*;
|
||||||
import javax.xml.transform.Transformer;
|
|
||||||
import javax.xml.transform.TransformerException;
|
|
||||||
import javax.xml.transform.TransformerFactory;
|
|
||||||
import javax.xml.transform.stream.StreamSource;
|
import javax.xml.transform.stream.StreamSource;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A utility to translate a web.xml file into a set of acegi security spring beans.
|
* A utility to translate a web.xml file into a set of acegi security spring beans.
|
||||||
@ -47,7 +46,7 @@ public class WebXmlConverter {
|
|||||||
/** The results of the conversion */
|
/** The results of the conversion */
|
||||||
private Document newWebXml, acegiBeansXml;
|
private Document newWebXml, acegiBeansXml;
|
||||||
|
|
||||||
public WebXmlConverter() throws Exception {
|
public WebXmlConverter() throws IOException, TransformerConfigurationException {
|
||||||
TransformerFactory tf = TransformerFactory.newInstance();
|
TransformerFactory tf = TransformerFactory.newInstance();
|
||||||
|
|
||||||
acegiSecurityTransformer = tf.newTransformer(createTransformerSource(WEB_TO_SPRING_XSL_FILE));
|
acegiSecurityTransformer = tf.newTransformer(createTransformerSource(WEB_TO_SPRING_XSL_FILE));
|
||||||
@ -81,15 +80,35 @@ public class WebXmlConverter {
|
|||||||
|
|
||||||
/** Set the input as an xml string */
|
/** Set the input as an xml string */
|
||||||
public void setInput(String xml) throws DocumentException {
|
public void setInput(String xml) throws DocumentException {
|
||||||
Document document = DocumentHelper.parseText(xml);
|
setInput(DocumentHelper.parseText(xml));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Set the input as a stream */
|
||||||
|
public void setInput(InputStream in) throws DocumentException {
|
||||||
|
SAXReader reader = new SAXReader();
|
||||||
|
setInput(reader.read(in));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** set the input as a dom4j document */
|
||||||
|
public void setInput(Document document) throws DocumentException {
|
||||||
|
validateWebXml(document);
|
||||||
xmlSource = new DocumentSource(document);
|
xmlSource = new DocumentSource(document);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** set the input as an InputStream */
|
/** Checks the web.xml to make sure it contains correct data */
|
||||||
public void setInput(InputStream xmlIn) throws Exception {
|
private void validateWebXml(Document document) throws DocumentException {
|
||||||
SAXReader reader = new SAXReader();
|
Node authMethodNode =
|
||||||
Document document = reader.read(xmlIn);
|
document.selectSingleNode("/web-app/login-config/auth-method");
|
||||||
xmlSource = new DocumentSource(document);
|
if(authMethodNode == null)
|
||||||
|
throw new DocumentException("login-config and auth-method must be present");
|
||||||
|
String authMethod = authMethodNode.getStringValue().toUpperCase();
|
||||||
|
if(!authMethod.equals("BASIC") && !authMethod.equals("FORM")) {
|
||||||
|
throw new DocumentException("unsupported auth-method: " + authMethod);
|
||||||
|
}
|
||||||
|
List roles = document.selectNodes("/web-app/security-role");
|
||||||
|
if(roles.isEmpty()) {
|
||||||
|
throw new DocumentException("Each role used must be defined in a security-role element");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAcegiOutputFileName() {
|
public String getAcegiOutputFileName() {
|
||||||
@ -112,5 +131,4 @@ public class WebXmlConverter {
|
|||||||
public Document getAcegiBeans() {
|
public Document getAcegiBeans() {
|
||||||
return acegiBeansXml;
|
return acegiBeansXml;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,14 +8,15 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
|||||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import net.sf.acegisecurity.util.InMemoryResource;
|
import net.sf.acegisecurity.util.InMemoryResource;
|
||||||
import org.xml.sax.SAXParseException;
|
|
||||||
import org.dom4j.Document;
|
import org.dom4j.Document;
|
||||||
|
import org.dom4j.DocumentException;
|
||||||
import org.dom4j.io.XMLWriter;
|
import org.dom4j.io.XMLWriter;
|
||||||
import org.dom4j.io.OutputFormat;
|
import org.dom4j.io.OutputFormat;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.ByteArrayInputStream;
|
import javax.xml.transform.TransformerException;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -41,20 +42,20 @@ public class AcegifierController extends SimpleFormController {
|
|||||||
throws Exception {
|
throws Exception {
|
||||||
|
|
||||||
AcegifierForm conversion = (AcegifierForm)command;
|
AcegifierForm conversion = (AcegifierForm)command;
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(conversion.getWebXml().getBytes());
|
WebXmlConverter converter = new WebXmlConverter();
|
||||||
WebXmlConverter converter = null;
|
|
||||||
int nBeans = 0;
|
int nBeans = 0;
|
||||||
Document newWebXml = null, acegiBeans = null;
|
Document newWebXml = null, acegiBeans = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
converter = new WebXmlConverter();
|
converter.setInput(conversion.getWebXml());
|
||||||
converter.setInput(in);
|
|
||||||
converter.doConversion();
|
converter.doConversion();
|
||||||
newWebXml = converter.getNewWebXml();
|
newWebXml = converter.getNewWebXml();
|
||||||
acegiBeans = converter.getAcegiBeans();
|
acegiBeans = converter.getAcegiBeans();
|
||||||
nBeans = validateAcegiBeans(conversion, acegiBeans, errors);
|
nBeans = validateAcegiBeans(conversion, acegiBeans, errors);
|
||||||
} catch (SAXParseException spe) {
|
} catch (DocumentException de) {
|
||||||
errors.rejectValue("webXml","parseFailure","Your Web XML Document failed to parse: " + spe.getMessage());
|
errors.rejectValue("webXml","webXmlDocError","There was a problem with your web.xml: " + de.getMessage());
|
||||||
|
} catch (TransformerException te) {
|
||||||
|
errors.rejectValue("webXml","transFailure","There was an error during the XSL transformation: " + te.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
if(errors.hasErrors()) {
|
if(errors.hasErrors()) {
|
||||||
@ -85,7 +86,7 @@ public class AcegifierController extends SimpleFormController {
|
|||||||
* Validates the acegi beans, based on the input form data, and returns the number
|
* Validates the acegi beans, based on the input form data, and returns the number
|
||||||
* of spring beans defined in the document.
|
* of spring beans defined in the document.
|
||||||
*/
|
*/
|
||||||
private int validateAcegiBeans(AcegifierForm conversion, Document beans, Errors errors) throws IOException {
|
private int validateAcegiBeans(AcegifierForm conversion, Document beans, Errors errors) {
|
||||||
DefaultListableBeanFactory bf = createBeanFactory(beans);
|
DefaultListableBeanFactory bf = createBeanFactory(beans);
|
||||||
|
|
||||||
//TODO: actually do some proper validation!
|
//TODO: actually do some proper validation!
|
||||||
|
@ -64,6 +64,7 @@ public class WebXmlConverterTests extends TestCase {
|
|||||||
assertNotNull(sef);
|
assertNotNull(sef);
|
||||||
assertNotNull(sef.getAuthenticationEntryPoint());
|
assertNotNull(sef.getAuthenticationEntryPoint());
|
||||||
FilterSecurityInterceptor fsi = sef.getFilterSecurityInterceptor();
|
FilterSecurityInterceptor fsi = sef.getFilterSecurityInterceptor();
|
||||||
|
System.out.println(prettyPrint(converter.getNewWebXml()));
|
||||||
System.out.println(prettyPrint(converter.getAcegiBeans()));
|
System.out.println(prettyPrint(converter.getAcegiBeans()));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -88,24 +88,6 @@
|
|||||||
</auth-constraint>
|
</auth-constraint>
|
||||||
</security-constraint>
|
</security-constraint>
|
||||||
|
|
||||||
<security-constraint>
|
|
||||||
<web-resource-collection>
|
|
||||||
<url-pattern>/acegilogin.jsp*</url-pattern>
|
|
||||||
</web-resource-collection>
|
|
||||||
<auth-constraint>
|
|
||||||
<role-name>*</role-name>
|
|
||||||
</auth-constraint>
|
|
||||||
</security-constraint>
|
|
||||||
|
|
||||||
<security-constraint>
|
|
||||||
<web-resource-collection>
|
|
||||||
<url-pattern>/*</url-pattern>
|
|
||||||
</web-resource-collection>
|
|
||||||
<auth-constraint>
|
|
||||||
<role-name>user</role-name>
|
|
||||||
</auth-constraint>
|
|
||||||
</security-constraint>
|
|
||||||
|
|
||||||
<login-config>
|
<login-config>
|
||||||
<auth-method>form</auth-method>
|
<auth-method>form</auth-method>
|
||||||
<form-login-config>
|
<form-login-config>
|
||||||
|
@ -248,6 +248,7 @@
|
|||||||
<xsl:text>
 CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON</xsl:text>
|
<xsl:text>
 CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON</xsl:text>
|
||||||
<xsl:text>
 PATTERN_TYPE_APACHE_ANT</xsl:text>
|
<xsl:text>
 PATTERN_TYPE_APACHE_ANT</xsl:text>
|
||||||
<xsl:apply-templates select="security-constraint"/>
|
<xsl:apply-templates select="security-constraint"/>
|
||||||
|
<xsl:text>
 /*=ROLE_ANONYMOUS</xsl:text> <!-- by default allow anonymous access to top level urls -->
|
||||||
<xsl:text>
 </xsl:text>
|
<xsl:text>
 </xsl:text>
|
||||||
</value>
|
</value>
|
||||||
</property>
|
</property>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user