Page not loading

32 views Asked by At

I have a springboot application. I thymeleaf dependency. Have the following controller class:

@Controller
public class OSController {

    @GetMapping("/search")
    public String getSearchForm() {
        return "index";
    }
}

The index.html is stored under templates in resources. But the page is not loading. It just returns the string "index" on the page. I am using the below dependencies in my application:

    // spring-boot
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-configuration-processor'
    implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

I suspect that the oauth might be causing the issue. In my security configuration I have allowed this particular page to be displayed without authentication.

@Bean
    public CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(List.of("*"));
        configuration.setAllowedMethods(List.of("*"));
        configuration.setAllowedHeaders(List.of("*"));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }


    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
        return httpSecurity
                .headers().frameOptions().disable()
                .and()
                .csrf(AbstractHttpConfigurer::disable)
                .cors()
                .and() // added for CORS size use CorsConfiguration source bean config
                .authorizeRequests()
                .antMatchers("/auth/**").permitAll()
                .antMatchers("/search**").permitAll()
                .antMatchers("/**", "/auth/logout").authenticated()
                .and()
                //.userDetailsService(jpaUserDetailsService)
                .oauth2ResourceServer().jwt(jwt -> jwt.jwtAuthenticationConverter(new CustomJwtAuthConverter()))
                .and()
                //.httpBasic(Customizer.withDefaults())
                .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
                .build();
    }
1

There are 1 answers

0
Manoj Krishna On

you need to add the following to configuration file

@Configuration
@EnableWebMvc
@ComponentScan
 public class SpringWebConfig
    extends WebMvcConfigurerAdapter implements ApplicationContextAware {

  /* **************************************************************** */
/*  THYMELEAF-SPECIFIC ARTIFACTS                                    */
/*  TemplateResolver <- TemplateEngine <- ViewResolver              */
/* **************************************************************** */

@Bean
public SpringResourceTemplateResolver templateResolver(){
    // SpringResourceTemplateResolver automatically integrates with Spring's own
    // resource resolution infrastructure, which is highly recommended.
    SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
    templateResolver.setApplicationContext(this.applicationContext);
    templateResolver.setPrefix("/WEB-INF/templates/");
    templateResolver.setSuffix(".html");
    // HTML is the default value, added here for the sake of clarity.
    templateResolver.setTemplateMode(TemplateMode.HTML);
    // Template cache is true by default. Set to false if you want
    // templates to be automatically updated when modified.
    templateResolver.setCacheable(true);
    return templateResolver;
}

@Bean
public SpringTemplateEngine templateEngine(){
    // SpringTemplateEngine automatically applies SpringStandardDialect and
    // enables Spring's own MessageSource message resolution mechanisms.
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver());
    // Enabling the SpringEL compiler with Spring 4.2.4 or newer can
    // speed up execution in most scenarios, but might be incompatible
    // with specific cases when expressions in one template are reused
    // across different data types, so this flag is "false" by default
    // for safer backwards compatibility.
    templateEngine.setEnableSpringELCompiler(true);
    return templateEngine;
}

@Bean
public ThymeleafViewResolver viewResolver(){
    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    viewResolver.setTemplateEngine(templateEngine());
    return viewResolver;
}
}

you can refer for the entire config in thymeleaf + spring configuration https://www.thymeleaf.org/doc/tutorials/3.1/thymeleafspring.html