Skip to content

Commit

Permalink
Spring security added
Browse files Browse the repository at this point in the history
Conflicts:
	src/main/java/org/jboss/forge/spec/spring/mvc/impl/SpringFacetImpl.java
	src/main/java/org/jboss/forge/spec/spring/mvc/impl/SpringPlugin.java
  • Loading branch information
Tejas committed Feb 5, 2013
1 parent 4fe3a1e commit d4ca274
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,6 @@ public interface SpringFacet extends Facet
*/

Resource<?> getResourceForWebPath(String path);

boolean installSecurity();
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ public class SpringFacetImpl extends BaseFacet implements SpringFacet

private static final Dependency JAVA_VALIDATION = DependencyBuilder.create("javax.validation:validation-api");

private static final Dependency SPRING_SECURITY = DependencyBuilder.create("org.springframework.security:spring-security-core:${spring.version}");

private static final Dependency SPRING_SECURITY_CONFIG = DependencyBuilder.create("org.springframework.security:spring-security-config:${spring.version}");

private static final Dependency SPRING_SECURITY_WEB = DependencyBuilder.create("org.springframework.security:spring-security-web:${spring.version}");

@Inject
public SpringFacetImpl(final DependencyInstaller installer)
{
Expand Down Expand Up @@ -135,8 +141,41 @@ public boolean install()

return true;
}

@Override
public boolean installSecurity()
{
for (Dependency requirement : getRequiredSecurityDependencies())
{
if (!this.installer.isInstalled(project, requirement))
{
DependencyFacet deps = project.getFacet(DependencyFacet.class);

if (!deps.hasDirectManagedDependency(JAVAEE6))
{
this.installer.installManaged(project, JAVAEE6);
}

if (requirement.getGroupId().equals("org.springframework"))
{
deps.setProperty("spring.version", SPRING_VERSION);
}

if (!requirement.equals(JAVAEE6))
{
deps.addDirectDependency(requirement);
}
}
}

return true;
}

private List<Dependency> getRequiredSecurityDependencies() {
return Arrays.asList(SPRING_SECURITY, SPRING_SECURITY_CONFIG, SPRING_SECURITY_WEB);
}

protected List<Dependency> getRequiredDependencies()
protected List<Dependency> getRequiredDependencies()
{
return Arrays.asList(SPRING_ASM, SPRING_BEANS, SPRING_CONTEXT, SPRING_CONTEXT_SUPPORT,
SPRING_CORE, SPRING_EXPRESSION, SPRING_ORM, SPRING_TX, SPRING_WEB, SPRING_WEB_MVC, JAVA_VALIDATION);
Expand Down
180 changes: 165 additions & 15 deletions src/main/java/org/jboss/forge/spec/spring/mvc/impl/SpringPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ public void setup(PipeOut out, @Option(required=false, name="location", defaultV
{
ShellMessages.error(out, "Could not change application context location, no file found at src/main/resources/" + location);
}

if(this.prompt.promptBoolean("Would you like to add spring security?", false)){
MetadataFacet meta = project.getFacet(MetadataFacet.class);

String securityContext = "/WEB-INF/"
+ meta.getProjectName().replace(' ', '-').toLowerCase()
+ "-security-context.xml";
String targetDir = this.prompt.prompt("Target Dir? (Default is: " + securityContext + ")", "");
updateSecurity(targetDir);
}
}
}

Expand Down Expand Up @@ -303,26 +313,133 @@ public void updateMVC( @Option(required=false, name="mvcPackage", description="M
updateWebXML(targetDir, mvcContext);
generateMVCContext(mvcContext, mvcPackage);
}

protected void generateContextFiles(boolean overwrite, Map<Object, Object> context)

@Command("security")
public void updateSecurity(@Option(required=false, name="targetDir", description="Target Directory") String targetDir)
{
SpringFacet spring = project.getFacet(SpringFacet.class);
MetadataFacet meta = project.getFacet(MetadataFacet.class);
PersistenceFacet persistence = project.getFacet(PersistenceFacet.class);
ResourceFacet resources = project.getFacet(ResourceFacet.class);

if(spring.installSecurity()){
System.out.println("Sucessfully installed spring security");
}
if (targetDir == null)
{
targetDir = new String();
}

WebResourceFacet web = project.getFacet(WebResourceFacet.class);
targetDir = processTargetDir(targetDir);

String securityContext = new String();
if (targetDir.isEmpty()) {
securityContext = "/WEB-INF/"
+ meta.getProjectName().replace(' ', '-').toLowerCase()
+ "-security-context.xml";
} else {
if(!targetDir.endsWith("/")){
targetDir += "/";
}
securityContext = "/WEB-INF/" + targetDir + meta.getProjectName().replace(' ', '-').toLowerCase()
+ "-security-context.xml";
}

updateWebXML(securityContext);
generateSecurity(securityContext);
}

String filename = context.get("mvc-context-file").toString();
loadTemplates();
private void generateSecurity(String securityContext) {

context.put("projectName", meta.getProjectName());
context.put("persistenceUnit", persistence.getConfig().listUnits().get(0).getName());
context.put("mvcContextFile", filename);
WebResourceFacet web = project.getFacet(WebResourceFacet.class);

ScaffoldUtil.createOrOverwrite(this.prompt, resources.getResource("META-INF/spring/applicationContext.xml"),
applicationContextTemplate.render(context), overwrite);
ScaffoldUtil.createOrOverwrite(this.prompt, web.getWebResource("WEB-INF/web.xml"), webXmlTemplate.render(context), overwrite);
ScaffoldUtil.createOrOverwrite(this.prompt, web.getWebResource(filename), mvcContextTemplate.render(context), overwrite);
Node beans;

if (!web.getWebResource(securityContext).exists())
{
beans = new Node("beans:beans");
beans.attribute("xsi:schemaLocation", "http://www.springframework.org/schema/beans\nhttp://www.springframework.org/schema/security\n"
+ "http//www.springframework.org/schema/security/spring-security-3.0.xsd");
}
else
{
beans = XMLParser.parse(web.getWebResource(securityContext).getResourceInputStream());
}

beans = addXMLSchemaSecurity(beans, false);
if (!hasChildNamed(beans, "http"))
{
Node http = new Node("http", beans);
http.attribute("auto-config", "true");
http.createChild("intercept-url").attribute("pattern", "/**/create*")
.attribute("access", "ROLE_ADMIN");
http.createChild("intercept-url").attribute("pattern", "/**/edit*")
.attribute("access", "ROLE_ADMIN");
http.createChild("remember-me");
}
if(!hasChildNamed(beans, "user-service")){
Node userService = new Node("user-service", beans);
userService.attribute("id", "userService");
userService.createChild("user").attribute("name", "admin").attribute("password", "admin").attribute("authorities", "ROLE_ADMIN");
}

if(!hasChildNamed(beans, "authentication-manager")){
Node authentiation = new Node("authentication-manager", beans);
authentiation.createChild("authentication-provider").attribute("user-service-ref", "userService");
}
web.createWebResource(XMLParser.toXMLString(beans), securityContext);

}

private Node addXMLSchemaSecurity(Node beans, boolean b) {
beans.attribute(XMLNS_PREFIX + "beans", "http://www.springframework.org/schema/beans");
beans.attribute("xmlns", "http://www.springframework.org/schema/security");
beans.attribute(XMLNS_PREFIX + "xsi", "http://www.w3.org/2001/XMLSchema-instance");

String schemaLocation = beans.getAttribute("");
schemaLocation = (schemaLocation == null) ? new String() : schemaLocation;

if (!schemaLocation.contains("http://www.springframework.org/schema/beans " +
"http://www.springframework.org/schema/beans/spring-beans.xsd"))
{
schemaLocation += " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd";
}

if (!schemaLocation.contains("http://www.springframework.org/schema/security " +
"http://www.springframework.org/schema/security/spring-security-3.0.xsd"))
{
schemaLocation += " http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd";
}
beans.attribute("xsi:schemaLocation", schemaLocation);

return beans;
}

private boolean hasChildNamed(Node beans, String string) {
for(Node child : beans.getChildren()){
if(child.getName() != null && child.getName().equals(string)){
return true;
}
}
return false;
}

protected void generateContextFiles(boolean overwrite, Map<Object, Object> context)
{
MetadataFacet meta = project.getFacet(MetadataFacet.class);
PersistenceFacet persistence = project.getFacet(PersistenceFacet.class);
ResourceFacet resources = project.getFacet(ResourceFacet.class);
WebResourceFacet web = project.getFacet(WebResourceFacet.class);

String filename = context.get("mvc-context-file").toString();
loadTemplates();

context.put("projectName", meta.getProjectName());
context.put("persistenceUnit", persistence.getConfig().listUnits().get(0).getName());
context.put("mvcContextFile", filename);

ScaffoldUtil.createOrOverwrite(this.prompt, resources.getResource("META-INF/spring/applicationContext.xml"),
applicationContextTemplate.render(context), overwrite);
ScaffoldUtil.createOrOverwrite(this.prompt, web.getWebResource("WEB-INF/web.xml"), webXmlTemplate.render(context), overwrite);
ScaffoldUtil.createOrOverwrite(this.prompt, web.getWebResource(filename), mvcContextTemplate.render(context), overwrite);
}

protected void loadTemplates()
Expand Down Expand Up @@ -416,6 +533,22 @@ protected void generateMVCContext(String mvcContextFilename, String mvcPackage)

web.createWebResource(XMLParser.toXMLString(beans), mvcContextFilename);
}

protected void updateWebXML(String targetDir){
ServletFacet servlet = project.getFacet(ServletFacet.class);

WebAppDescriptor webXML = servlet.getConfig();
//Add security filter if asked for one
webXML = addSecurity(targetDir, webXML);
// Add to context param if not there
if(targetDir.startsWith("/")){
targetDir = targetDir.substring(1);
}
if(!webXML.getContextParam("contextConfigLocation").contains(targetDir)){
webXML.contextParam("contextConfigLocation", webXML.getContextParam("contextConfigLocation") + ", " + targetDir);
}
servlet.saveConfig(webXML);
}

protected void updateWebXML(String mvcContext, String targetDir)
{
Expand Down Expand Up @@ -489,6 +622,23 @@ protected void updateWebXML(String mvcContext, String targetDir)
}
}

private WebAppDescriptor addSecurity(String targetDir,
WebAppDescriptor webXML) {
String security = new String();
if (targetDir.contains("-security-context.xml")) {
for (FilterDef filter : webXML.getFilters()) {
if (filter.getFilterClass().contains("org.springframework.web.filter.DelegatingFilterProxy")) {
security = filter.getFilterClass();
break;
}
}
}
if (security.isEmpty() && targetDir.contains("-security-context.xml")) {
webXML = webXML.filter("springSecurityFilterChain", "org.springframework.web.filter.DelegatingFilterProxy", new String[] {"/*"});
}
return webXML;
}

private Node addContextComponentScan(Node beans, String basePackage)
{
if (hasContextComponentScan(beans, basePackage))
Expand Down Expand Up @@ -542,7 +692,7 @@ private Node addXMLSchema(Node beans, boolean applicationContext)
beans.attribute(XMLNS_PREFIX + "context", "http://www.springframework.org/schema/context");
beans.attribute(XMLNS_PREFIX + "xsi", "http://www.w3.org/2001/XMLSchema-instance");

String schemaLocation = beans.getAttribute("xsi:schemaLocation");
String schemaLocation = beans.getAttribute("");
schemaLocation = (schemaLocation == null) ? new String() : schemaLocation;

if (!schemaLocation.contains("http://www.springframework.org/schema/beans " +
Expand Down

0 comments on commit d4ca274

Please sign in to comment.