-1

I am creating a basic Spring MVC + Repositories + Security configuration. I leveraged THIS GUIDE and got to the configuration I am reporting below. According to that configuration, I would expect some welcome page to show up at localhost:8080/HPLAN/welcome; I also get redirected to that URL after typing localhost:8080/HPLAN/, but I always get a 404 error. Server log does not receive any request (server log does not show any "no request mapping"). At the deployment location "HPLAN" folder is present. I also need to mention that database tables are NOT created.

So, my configuration is this:

  1. Security

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @SuppressWarnings("unused")
    @Autowired
    private DataSource dataSource;
    
    @Autowired
    private CustomUserDetailsService customUserDetailsService;
    
    @Override
    protected void configure(AuthenticationManagerBuilder registry) throws Exception {
        registry.userDetailsService(customUserDetailsService);
    }
    
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/resources/**"); // #3
    }
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests().antMatchers("/login", "/login/form**", "/register", "/logout")
                .permitAll() // #4
                .antMatchers("/admin", "/admin/**").hasRole("ADMIN") // #6
                .anyRequest().authenticated() // 7
                .and().formLogin() // #8
                .loginPage("/login/form") // #9
                .loginProcessingUrl("/login").failureUrl("/login/form?error").permitAll(); // #5
    }
    

    }

  2. Persistence

    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(basePackages = "com.tek4b.hplan.repositories")
    public class PersistenceConfig {
    
    @Autowired
    private Environment env;
    
    @Value("${init-db:false}")
    private String initDatabase;
    
    @Bean
    public PlatformTransactionManager transactionManager() {
        EntityManagerFactory factory = entityManagerFactory().getObject();
        return new JpaTransactionManager(factory);
    }
    
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(Boolean.TRUE);
        vendorAdapter.setShowSql(Boolean.TRUE);
    
        factory.setDataSource(dataSource());
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan("com.tek4b.hplan.entities");
    
        Properties jpaProperties = new Properties();
        jpaProperties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
        factory.setJpaProperties(jpaProperties);
    
        factory.afterPropertiesSet();
        factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());
        return factory;
    }
    
    @Bean
    public HibernateExceptionTranslator hibernateExceptionTranslator() {
        return new HibernateExceptionTranslator();
    }
    
    @Bean
    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
        dataSource.setUrl(env.getProperty("jdbc.url"));
        dataSource.setUsername(env.getProperty("jdbc.username"));
        dataSource.setPassword(env.getProperty("jdbc.password"));
        return dataSource;
    }
    
    @Bean
    public DataSourceInitializer dataSourceInitializer(DataSource dataSource) {
        DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
        dataSourceInitializer.setDataSource(dataSource);
        ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
        databasePopulator.addScript(new ClassPathResource("db.sql"));
        dataSourceInitializer.setDatabasePopulator(databasePopulator);
        dataSourceInitializer.setEnabled(Boolean.parseBoolean(initDatabase));
        return dataSourceInitializer;
    }
    

    }

  3. AppConfig

    @Configuration
    @ComponentScan(basePackages = {
        "com.tek4b.hplan" }, excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = {
                "com.tek4b.hplan.web.*" }) )
    @PropertySource(value = { "classpath:application.properties" })
    @EnableScheduling
    @EnableAspectJAutoProxy
    @EnableCaching
    public class AppConfig {
    
    @Autowired
    private Environment env;
    
    @Bean
    public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
    
    @Bean
    public CacheManager cacheManager() {
        return new ConcurrentMapCacheManager();
    }
    

    }

  4. MVC:

    @Configuration
    @ComponentScan(basePackages = { "com.tek4b.hplan.web" })
    @EnableWebMvc
    public class WebMvcConfig extends WebMvcConfigurerAdapter {
    
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        super.addViewControllers(registry);
        registry.addViewController("login/form").setViewName("login");
        registry.addViewController("welcome").setViewName("welcome");
        registry.addViewController("admin").setViewName("admin");
    }
    
    @Bean
    public ViewResolver resolver() {
        InternalResourceViewResolver url = new InternalResourceViewResolver();
        url.setPrefix("/WEB-INF/jsp/");
        url.setSuffix(".jsp");
        return url;
    }
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
    
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
    
    @Bean(name = "messageSource")
    public MessageSource configureMessageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:messages");
        messageSource.setCacheSeconds(5);
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }
    
    @Bean
    public SimpleMappingExceptionResolver simpleMappingExceptionResolver() {
        SimpleMappingExceptionResolver b = new SimpleMappingExceptionResolver();
        Properties mappings = new Properties();
        mappings.put("org.springframework.dao.DataAccessException", "error");
        b.setExceptionMappings(mappings);
        return b;
    }
    

    }

  5. Web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>HPLAN</display-name>
    

My project structure:

enter image description here

I created the database and the server seems to start correctly:

Aug 13, 2015 7:26:11 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: <hidden by me>;.
Aug 13, 2015 7:26:11 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:HPLAN' did not find a matching property.
Aug 13, 2015 7:26:11 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Aug 13, 2015 7:26:11 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Aug 13, 2015 7:26:11 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 694 ms
Aug 13, 2015 7:26:11 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Aug 13, 2015 7:26:11 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
Aug 13, 2015 7:26:12 PM org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [281] milliseconds.
Aug 13, 2015 7:26:14 PM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Aug 13, 2015 7:26:14 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Aug 13, 2015 7:26:14 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Aug 13, 2015 7:26:14 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2767 ms

Then it stands like that regardless of my requests. What's wrong with it?

============UPDATE 6. AppInitializer class:

public class SpringWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { AppConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebMvcConfig.class };
    }

    @Override
    protected String[] getServletMappings() {

        return new String[] { "/" };
    }

    @Override
    protected Filter[] getServletFilters() {
        return new Filter[] { new DelegatingFilterProxy("springSecurityFilterChain"),
                new OpenEntityManagerInViewFilter() };
    }

}
Manu
  • 4,019
  • 8
  • 50
  • 94
  • Did u checked your application deployed or not? – Subodh Joshi Aug 13 '15 at 17:50
  • @SubodhJoshi It is actually deployed at the tomcat deployment folder with its correct name (HPLAN). I updated my OT. – Manu Aug 13 '15 at 17:51
  • As u mentioned you are using Tomcat for deployment so it have to show which war its deployed did you saw that? Sometimes i also faces in eclipse+Tomcat or Eclipse+Jboss Server started but project nt deployed – Subodh Joshi Aug 13 '15 at 17:52
  • @SubodhJoshi I connected my Tomcat 7 to eclipse (STS), then I created an instance of it and selected "Run on server" to run my HPLAN application. This way, at the deployment location, I don't see a WAR, but a folder named HPLAN. – Manu Aug 13 '15 at 17:54
  • You right click on project then click on `Run on Sever` ? Do one thing if you war deploy manually in tomcat also read my previous comment – Subodh Joshi Aug 13 '15 at 17:56
  • @SubodhJoshi Yes. Also, I tried to drag the project to the server to deploy it. Nothing changes. – Manu Aug 13 '15 at 17:58
  • Let me also mention that database tables are not created, so something wrong during deployment. – Manu Aug 13 '15 at 17:59
  • Where is your `WebAppInitializer` class? – Don Bottstein Aug 13 '15 at 18:00
  • It show deployment not done, checks logs it will tell clear picture what exception come on deployment – Subodh Joshi Aug 13 '15 at 18:01
  • @DonBottstein I updated my OT. – Manu Aug 13 '15 at 18:06
  • @SubodhJoshi In the logs folder I only find localhost_access_log.2015-08-13, reporting GET methods and their corresponding 404 errors. – Manu Aug 13 '15 at 18:07
  • What about `server.log` file have any issue? – Subodh Joshi Aug 13 '15 at 18:11
  • @SubodhJoshi Where can I find it? There is no server.log in the logs folder at the deployment location. Also, there no file in the tomcat executable folder. – Manu Aug 13 '15 at 18:14
  • Whatever link you are using for your application ..Are you using same code or you are making changes in your application? – Subodh Joshi Aug 13 '15 at 18:21
  • no, no changes, except in the server log to hide my path variable – Manu Aug 13 '15 at 19:11

2 Answers2

0

In your function getServletMappings(), change

return new String[] { "/" };

to

return new String[] { "/*" };

Source : Spring Java Config vs Jboss 7

Community
  • 1
  • 1
-1

Your project doesn't have the "Dynanic Web Project" Facet.

Right-Click the project, and select "Properties". And then click on "Project Facet". Select the "Dynamic Web Project" and proceed with next steps there.

UPDATE If that is OK, then check the "Deployment Assembly" in Project Properties tab. The source folders should be having target of "WEB-INF/classes", and other such things related to deployment.

If that is also OK, then you need to go to the "Servers" view, and delete the server from there. Restart Eclipse. And then Add a new Server.

Mecon
  • 977
  • 1
  • 6
  • 17
  • It actually does, module v3.0. – Manu Aug 13 '15 at 17:55
  • 1
    If its wont web project then right click on project should not show `Run on Sever` – Subodh Joshi Aug 13 '15 at 17:58
  • 1
    Added an update. I think that should address the issue. I don't think this is Spring related at all. It all about Eclipse and the WTP stuff. – Mecon Aug 13 '15 at 18:21
  • Agree with @Mecon comment – Subodh Joshi Aug 13 '15 at 18:23
  • That doesn't work. It is Spring-related, as it cannot initialize the database for some reason. – Manu Aug 13 '15 at 19:08
  • @Manu I think it not Spring related because the tomcat log you posted has no line about the webapp being deployed. Usually if there is an issue with the app the Tomcat log will have msgs about webapp being attempted and then followed by msg about the app failing to deploy. – Mecon Aug 13 '15 at 19:21
  • @Manu i told you same thing your application not deployed – Subodh Joshi Aug 14 '15 at 06:05