From fa3866854f67dda22081cd09dd48bc00fd534579 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 25 Feb 2014 16:13:23 -0500 Subject: [PATCH 1/7] Update booking-mvc to use Spring Java config --- booking-mvc/pom.xml | 2 +- .../booking/config/DataAccessConfig.java | 46 ++++++++ .../booking/config/SecurityConfig.java | 39 +++++++ .../samples/booking/config/WebFlowConfig.java | 59 ++++++++++ .../samples/booking/config/WebMvcConfig.java | 109 ++++++++++++++++++ .../WEB-INF/config/data-access-config.xml | 38 ------ .../webapp/WEB-INF/config/security-config.xml | 37 ------ .../WEB-INF/config/web-application-config.xml | 18 --- .../webapp/WEB-INF/config/webflow-config.xml | 37 ------ .../webapp/WEB-INF/config/webmvc-config.xml | 70 ----------- .../src/main/webapp/WEB-INF/login.html | 12 +- booking-mvc/src/main/webapp/WEB-INF/web.xml | 13 ++- 12 files changed, 267 insertions(+), 213 deletions(-) create mode 100644 booking-mvc/src/main/java/org/springframework/webflow/samples/booking/config/DataAccessConfig.java create mode 100644 booking-mvc/src/main/java/org/springframework/webflow/samples/booking/config/SecurityConfig.java create mode 100644 booking-mvc/src/main/java/org/springframework/webflow/samples/booking/config/WebFlowConfig.java create mode 100644 booking-mvc/src/main/java/org/springframework/webflow/samples/booking/config/WebMvcConfig.java delete mode 100644 booking-mvc/src/main/webapp/WEB-INF/config/data-access-config.xml delete mode 100644 booking-mvc/src/main/webapp/WEB-INF/config/security-config.xml delete mode 100644 booking-mvc/src/main/webapp/WEB-INF/config/web-application-config.xml delete mode 100644 booking-mvc/src/main/webapp/WEB-INF/config/webflow-config.xml delete mode 100644 booking-mvc/src/main/webapp/WEB-INF/config/webmvc-config.xml diff --git a/booking-mvc/pom.xml b/booking-mvc/pom.xml index fad2efe..c9f9448 100644 --- a/booking-mvc/pom.xml +++ b/booking-mvc/pom.xml @@ -10,7 +10,7 @@ 3.2.0.RELEASE - 2.3.3.RELEASE + 2.4.0.BUILD-SNAPSHOT 1.7.5 2.0.15 2.0.0 diff --git a/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/config/DataAccessConfig.java b/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/config/DataAccessConfig.java new file mode 100644 index 0000000..53166aa --- /dev/null +++ b/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/config/DataAccessConfig.java @@ -0,0 +1,46 @@ +package org.springframework.webflow.samples.booking.config; + +import java.util.Collections; + +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@Configuration +@EnableTransactionManagement +@ComponentScan(basePackages="org.springframework.webflow.samples.booking") +public class DataAccessConfig { + + @Bean + public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { + JpaTransactionManager txManager = new JpaTransactionManager(); + txManager.setEntityManagerFactory(emf); + return txManager; + } + + @Bean + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { + LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean(); + emf.setDataSource(dataSource()); + emf.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); + emf.setJpaPropertyMap(Collections.singletonMap("hibernate.session_factory_name", "mySessionFactory")); + return emf; + } + + @Bean + public DataSource dataSource() { + DriverManagerDataSource dataSource = new DriverManagerDataSource("jdbc:hsqldb:mem:booking", "sa", ""); + dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); + return dataSource; + } + +} diff --git a/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/config/SecurityConfig.java b/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/config/SecurityConfig.java new file mode 100644 index 0000000..cf1a71f --- /dev/null +++ b/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/config/SecurityConfig.java @@ -0,0 +1,39 @@ +package org.springframework.webflow.samples.booking.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.security.authentication.encoding.Md5PasswordEncoder; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +@Configuration +@EnableWebSecurity +public class SecurityConfig extends WebSecurityConfigurerAdapter { + + @Override + protected void configure(HttpSecurity http) throws Exception { + http + .csrf().disable() + .formLogin() + .loginPage("/login") + .loginProcessingUrl("/loginProcess") + .defaultSuccessUrl("/hotels/search") + .failureUrl("/login?login_error=1") + .and() + .logout() + .logoutUrl("/logout") + .logoutSuccessUrl("/logoutSuccess"); + } + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.inMemoryAuthentication() + .passwordEncoder(new Md5PasswordEncoder()) + .withUser("keith").password("417c7382b16c395bc25b5da1398cf076").roles("USER", "SUPERVISOR").and() + .withUser("erwin").password("12430911a8af075c6f41c6976af22b09").roles("USER", "SUPERVISOR").and() + .withUser("jeremy").password("57c6cbff0d421449be820763f03139eb").roles("USER").and() + .withUser("scott").password("942f2339bf50796de535a384f0d1af3e").roles("USER"); + } + +} diff --git a/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/config/WebFlowConfig.java b/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/config/WebFlowConfig.java new file mode 100644 index 0000000..d269d2b --- /dev/null +++ b/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/config/WebFlowConfig.java @@ -0,0 +1,59 @@ +package org.springframework.webflow.samples.booking.config; + +import java.util.Arrays; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.webflow.config.AbstractFlowConfiguration; +import org.springframework.webflow.definition.registry.FlowDefinitionRegistry; +import org.springframework.webflow.engine.builder.support.FlowBuilderServices; +import org.springframework.webflow.executor.FlowExecutor; +import org.springframework.webflow.mvc.builder.MvcViewFactoryCreator; +import org.springframework.webflow.security.SecurityFlowExecutionListener; + +@Configuration +public class WebFlowConfig extends AbstractFlowConfiguration { + + @Autowired + private WebMvcConfig webMvcConfig; + + @Bean + public FlowExecutor flowExecutor() { + return getFlowExecutorBuilder(flowRegistry()) + .addFlowExecutionListener(new SecurityFlowExecutionListener(), "*") + .build(); + } + + @Bean + public FlowDefinitionRegistry flowRegistry() { + return getFlowDefinitionRegistryBuilder(flowBuilderServices()) + .setBasePath("/WEB-INF") + .addFlowLocationPattern("/**/*-flow.xml").build(); + } + + @Bean + public FlowBuilderServices flowBuilderServices() { + return getFlowBuilderServicesBuilder() + .setViewFactoryCreator(mvcViewFactoryCreator()) + .setValidator(validator()) + .setDevelopmentMode(true) + .build(); + } + + @Bean + public MvcViewFactoryCreator mvcViewFactoryCreator() { + MvcViewFactoryCreator factoryCreator = new MvcViewFactoryCreator(); + factoryCreator.setViewResolvers(Arrays.asList(this.webMvcConfig.tilesViewResolver())); + factoryCreator.setUseSpringBeanBinding(true); + return factoryCreator; + } + + @Bean + public LocalValidatorFactoryBean validator() { + return new LocalValidatorFactoryBean(); + } + +} diff --git a/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/config/WebMvcConfig.java b/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/config/WebMvcConfig.java new file mode 100644 index 0000000..82ffca7 --- /dev/null +++ b/booking-mvc/src/main/java/org/springframework/webflow/samples/booking/config/WebMvcConfig.java @@ -0,0 +1,109 @@ +package org.springframework.webflow.samples.booking.config; + +import java.util.LinkedHashSet; +import java.util.Set; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.webflow.mvc.servlet.FlowHandlerAdapter; +import org.springframework.webflow.mvc.servlet.FlowHandlerMapping; +import org.springframework.webflow.samples.booking.BookingFlowHandler; +import org.thymeleaf.dialect.IDialect; +import org.thymeleaf.extras.conditionalcomments.dialect.ConditionalCommentsDialect; +import org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect; +import org.thymeleaf.extras.tiles2.dialect.TilesDialect; +import org.thymeleaf.extras.tiles2.spring.web.configurer.ThymeleafTilesConfigurer; +import org.thymeleaf.extras.tiles2.spring.web.view.FlowAjaxThymeleafTilesView; +import org.thymeleaf.spring3.SpringTemplateEngine; +import org.thymeleaf.spring3.view.AjaxThymeleafViewResolver; +import org.thymeleaf.templateresolver.ServletContextTemplateResolver; + +@EnableWebMvc +@Configuration +public class WebMvcConfig extends WebMvcConfigurerAdapter { + + @Autowired + private WebFlowConfig webFlowConfig; + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**").addResourceLocations("/", "classpath:/META-INF/web-resources/"); + } + + @Override + public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { + configurer.enable(); + } + + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("/").setViewName("intro"); + registry.addViewController("/login"); + registry.addViewController("/logoutSuccess"); + } + + @Bean + public FlowHandlerMapping flowHandlerMapping() { + FlowHandlerMapping handlerMapping = new FlowHandlerMapping(); + handlerMapping.setOrder(-1); + handlerMapping.setFlowRegistry(this.webFlowConfig.flowRegistry()); + return handlerMapping; + } + + @Bean + public FlowHandlerAdapter flowHandlerAdapter() { + FlowHandlerAdapter handlerAdapter = new FlowHandlerAdapter(); + handlerAdapter.setFlowExecutor(this.webFlowConfig.flowExecutor()); + handlerAdapter.setSaveOutputToFlashScopeOnRedirect(true); + return handlerAdapter; + } + + @Bean(name="hotels/booking") + public BookingFlowHandler BookingFlowHandler() { + return new BookingFlowHandler(); + } + + @Bean + public AjaxThymeleafViewResolver tilesViewResolver() { + AjaxThymeleafViewResolver viewResolver = new AjaxThymeleafViewResolver(); + viewResolver.setViewClass(FlowAjaxThymeleafTilesView.class); + viewResolver.setTemplateEngine(templateEngine()); + return viewResolver; + } + + @Bean + public SpringTemplateEngine templateEngine(){ + + Set dialects = new LinkedHashSet(); + dialects.add(new TilesDialect()); + dialects.add(new SpringSecurityDialect()); + dialects.add(new ConditionalCommentsDialect()); + + SpringTemplateEngine templateEngine = new SpringTemplateEngine(); + templateEngine.setTemplateResolver(templateResolver()); + templateEngine.setAdditionalDialects(dialects); + return templateEngine; + } + + @Bean + public ServletContextTemplateResolver templateResolver() { + ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(); + templateResolver.setPrefix("/WEB-INF/"); + templateResolver.setTemplateMode("HTML5"); + return templateResolver; + } + + @Bean + public ThymeleafTilesConfigurer tilesConfigurer() { + ThymeleafTilesConfigurer configurer = new ThymeleafTilesConfigurer(); + configurer.setDefinitions("/WEB-INF/**/views.xml"); + return configurer; + } + +} diff --git a/booking-mvc/src/main/webapp/WEB-INF/config/data-access-config.xml b/booking-mvc/src/main/webapp/WEB-INF/config/data-access-config.xml deleted file mode 100644 index 924ede3..0000000 --- a/booking-mvc/src/main/webapp/WEB-INF/config/data-access-config.xml +++ /dev/null @@ -1,38 +0,0 @@ - - - - - - - - - - - - - - - - - - - - hibernate.session_factory_name=mySessionFactory - - - - - - - - - - - - - \ No newline at end of file diff --git a/booking-mvc/src/main/webapp/WEB-INF/config/security-config.xml b/booking-mvc/src/main/webapp/WEB-INF/config/security-config.xml deleted file mode 100644 index 741607c..0000000 --- a/booking-mvc/src/main/webapp/WEB-INF/config/security-config.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/booking-mvc/src/main/webapp/WEB-INF/config/web-application-config.xml b/booking-mvc/src/main/webapp/WEB-INF/config/web-application-config.xml deleted file mode 100644 index 10ebcf5..0000000 --- a/booking-mvc/src/main/webapp/WEB-INF/config/web-application-config.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/booking-mvc/src/main/webapp/WEB-INF/config/webflow-config.xml b/booking-mvc/src/main/webapp/WEB-INF/config/webflow-config.xml deleted file mode 100644 index 06cb11a..0000000 --- a/booking-mvc/src/main/webapp/WEB-INF/config/webflow-config.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/booking-mvc/src/main/webapp/WEB-INF/config/webmvc-config.xml b/booking-mvc/src/main/webapp/WEB-INF/config/webmvc-config.xml deleted file mode 100644 index 6a3c53b..0000000 --- a/booking-mvc/src/main/webapp/WEB-INF/config/webmvc-config.xml +++ /dev/null @@ -1,70 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /WEB-INF/**/views.xml - - - - - diff --git a/booking-mvc/src/main/webapp/WEB-INF/login.html b/booking-mvc/src/main/webapp/WEB-INF/login.html index b1bf112..4d85943 100644 --- a/booking-mvc/src/main/webapp/WEB-INF/login.html +++ b/booking-mvc/src/main/webapp/WEB-INF/login.html @@ -77,31 +77,31 @@ Login Information

- +
-

- +
- +