Made lesson resolution with Thymeleaf dynamic

This commit is contained in:
Nanne Baars 2016-05-13 15:42:48 +02:00
parent 4a19ddf40a
commit 22d2255664
6 changed files with 23 additions and 15 deletions

View File

@ -1,7 +1,7 @@
package org.owasp.webgoat; package org.owasp.webgoat;
import org.owasp.webgoat.lessons.LessonEndpointMapping; import org.owasp.webgoat.lessons.LessonEndpointMapping;
import org.owasp.webgoat.plugins.PluginsLoader; import org.owasp.webgoat.plugins.PluginClassLoader;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConfigurableBeanFactory;
@ -30,23 +30,25 @@ public class LessonEndpointProvider {
private final String pluginBasePackage; private final String pluginBasePackage;
private final ApplicationContext parentContext; private final ApplicationContext parentContext;
private final PluginClassLoader classLoader;
private ListableBeanFactory context; private ListableBeanFactory context;
private DefaultListableBeanFactory providedBeans; private DefaultListableBeanFactory providedBeans;
private BeanFactory beanFactory; private BeanFactory beanFactory;
public LessonEndpointProvider(String pluginBasePackage, ApplicationContext parentContext, BeanFactory beanFactory) { public LessonEndpointProvider(String pluginBasePackage, ApplicationContext parentContext, BeanFactory beanFactory, PluginClassLoader cl) {
this.pluginBasePackage = pluginBasePackage; this.pluginBasePackage = pluginBasePackage;
this.parentContext = parentContext; this.parentContext = parentContext;
this.providedBeans = new DefaultListableBeanFactory(this.parentContext.getParentBeanFactory()); this.providedBeans = new DefaultListableBeanFactory(this.parentContext.getParentBeanFactory());
this.beanFactory = beanFactory; this.beanFactory = beanFactory;
this.classLoader = cl;
} }
public void registerEndpoints() { public void registerEndpoints() {
if (context == null) { if (context == null) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.setParent(parentContext); context.setParent(parentContext);
context.setClassLoader(PluginsLoader.classLoader); context.setClassLoader(classLoader);
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, false); ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context, false);
scanner.addIncludeFilter(new AnnotationTypeFilter(LessonEndpointMapping.class)); scanner.addIncludeFilter(new AnnotationTypeFilter(LessonEndpointMapping.class));

View File

@ -33,6 +33,7 @@ public class LessonTemplateResolver extends TemplateResolver {
@Override @Override
protected String computeResourceName(TemplateProcessingParameters params) { protected String computeResourceName(TemplateProcessingParameters params) {
String templateName = params.getTemplateName(); String templateName = params.getTemplateName();
return templateName.substring(PREFIX.length()); return templateName.substring(PREFIX.length());
} }
@ -46,7 +47,8 @@ public class LessonTemplateResolver extends TemplateResolver {
try { try {
return new ByteArrayInputStream(Files.toByteArray(lesson)); return new ByteArrayInputStream(Files.toByteArray(lesson));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); //no html yet
return new ByteArrayInputStream(new byte[0]);
} }
} }
return null; return null;

View File

@ -1,5 +1,6 @@
package org.owasp.webgoat; package org.owasp.webgoat;
import org.owasp.webgoat.plugins.PluginClassLoader;
import org.owasp.webgoat.plugins.PluginsLoader; import org.owasp.webgoat.plugins.PluginsLoader;
import org.owasp.webgoat.session.Course; import org.owasp.webgoat.session.Course;
import org.owasp.webgoat.session.WebSession; import org.owasp.webgoat.session.WebSession;
@ -41,8 +42,13 @@ public class WebGoat extends SpringBootServletInitializer {
} }
@Bean @Bean
public PluginsLoader pluginsLoader(@Qualifier("pluginTargetDirectory") File pluginTargetDirectory) { public PluginClassLoader pluginClassLoader() {
return new PluginsLoader(pluginTargetDirectory); return new PluginClassLoader(PluginClassLoader.class.getClassLoader());
}
@Bean
public PluginsLoader pluginsLoader(@Qualifier("pluginTargetDirectory") File pluginTargetDirectory, PluginClassLoader classLoader) {
return new PluginsLoader(pluginTargetDirectory, classLoader);
} }
@Bean @Bean
@ -52,8 +58,8 @@ public class WebGoat extends SpringBootServletInitializer {
} }
@Bean @Bean
public LessonEndpointProvider lessonEndpointProvider(ApplicationContext applicationContext, BeanFactory factory) { public LessonEndpointProvider lessonEndpointProvider(ApplicationContext applicationContext, BeanFactory factory, PluginClassLoader cl) {
LessonEndpointProvider lessonEndpointProvider = new LessonEndpointProvider("org.owasp.webgoat", applicationContext, factory); LessonEndpointProvider lessonEndpointProvider = new LessonEndpointProvider("org.owasp.webgoat", applicationContext, factory, cl);
return lessonEndpointProvider; return lessonEndpointProvider;
} }

View File

@ -29,8 +29,8 @@ public class Plugin {
private static final String NAME_LESSON_SOLUTION_DIRECTORY = "lessonSolutions"; private static final String NAME_LESSON_SOLUTION_DIRECTORY = "lessonSolutions";
private static final String NAME_LESSON_PLANS_DIRECTORY = "lessonPlans"; private static final String NAME_LESSON_PLANS_DIRECTORY = "lessonPlans";
public static PluginClassLoader classLoader;
private PluginClassLoader classLoader;
private Class<AbstractLesson> lesson; private Class<AbstractLesson> lesson;
private Map<String, File> solutionLanguageFiles = new HashMap<>(); private Map<String, File> solutionLanguageFiles = new HashMap<>();
private Map<String, File> lessonPlansLanguageFiles = new HashMap<>(); private Map<String, File> lessonPlansLanguageFiles = new HashMap<>();

View File

@ -6,7 +6,6 @@ import org.owasp.webgoat.util.LabelProvider;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils; import org.springframework.util.ResourceUtils;
import java.io.File; import java.io.File;
@ -37,18 +36,17 @@ import java.util.zip.ZipEntry;
* @author dm * @author dm
* @version $Id: $Id * @version $Id: $Id
*/ */
@Component
public class PluginsLoader { public class PluginsLoader {
private static final String WEBGOAT_PLUGIN_EXTENSION = "jar"; private static final String WEBGOAT_PLUGIN_EXTENSION = "jar";
private static final int BUFFER_SIZE = 32 * 1024; private static final int BUFFER_SIZE = 32 * 1024;
private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final File pluginTargetDirectory; private final File pluginTargetDirectory;
public static PluginClassLoader classLoader = new PluginClassLoader(PluginClassLoader.class.getClassLoader()); private final PluginClassLoader classLoader;
@Autowired @Autowired
public PluginsLoader(File pluginTargetDirectory) { public PluginsLoader(File pluginTargetDirectory, PluginClassLoader pluginClassLoader) {
this.classLoader = pluginClassLoader;
this.pluginTargetDirectory = pluginTargetDirectory; this.pluginTargetDirectory = pluginTargetDirectory;
} }

View File

@ -7,6 +7,6 @@
<div id="message" class="info" th:utext="${message}"></div> <div id="message" class="info" th:utext="${message}"></div>
<br/> <br/>
<div th:utext="${lesson.content}"></div> <div th:utext="${lesson.content}"></div>
<div th:replace="lesson:showLesson"></div> <div th:replace="lesson:__${lesson}__"></div>
</html> </html>