Search code examples
javaspringspring-bootspring-securitythymeleaf

Mapping error Thymeleaf Spring Boot Security app, doesn't load templateS


This is my first time asking here, I'm concerned about this, so Im developing a web application using Spring Securirty and Spring boot, code seems to be fine and application runs, I only configured the login, session and registration page, when I go to the root url localhost:8080/ the index template loads correctly but when I go to other url for example /login or /register my templates don'y show up, also if I change the template for the main url (localhost:8080/) and return other than index it keeps returning index, in summary the web only loads the file called index.html under templates folder. This is my file structure: hirearchy

CONTROLLER CLASS

@Controller
public class UserController {
    
    @Autowired
    private UserService userService;

    @Autowired
    private SecurityService securityService;

    @Autowired
    private UserValidator userValidator;
    
    @GetMapping("/register")
    public String registration(Model model) {
        model.addAttribute("userForm", new User());

        return "register";
    }
    
    @PostMapping("/register")
    public String registration(@ModelAttribute("userForm") User userForm, BindingResult bindingResult) {
        userValidator.validate(userForm, bindingResult);

        if (bindingResult.hasErrors()) {
            return "register";
        }

        userService.save(userForm);

        securityService.autoLogin(userForm.getEmail(), userForm.getPass());

        return "redirect:/main";
    }
    
    @GetMapping("/login")
    public String login(Model model, String error, String logout) {
        if (error != null)
            model.addAttribute("error", "Your username and password is invalid.");

        if (logout != null)
            model.addAttribute("message", "You have been logged out successfully.");

        return "login";
    }
    
    @GetMapping({"/"})
    public String welcome(Model model) {
        return "index";
    }
    
    @GetMapping({"/main"})
    public String main(Model model) {
        return "main";
    }
}

WEB SECURITY CONFIG

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{

    
    @Autowired
    private UserDetailsService userDetailsService;

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }
    
    @Bean
    public UserDetailsService userDetailsService() {
        return super.userDetailsService();
    }
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/",
                        "/register",
                        "/js/**",
                        "/css/**",
                        "/img/**",
                        "/webjars/**",
                        "/h2/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();

    }

    @Bean
    public AuthenticationManager customAuthenticationManager() throws Exception {
        return authenticationManager();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
    }
}

APPLICATION CLASS

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

Solution

  • So i managed to figure this out and it was package hirerarchy and resource loading failure, spring app loads everything and every package under it class, my app package (and app class inside of it) was not the root one and also was below the controller one. This is the structure it has to follow:

    • root package
      • SpringApp.java
      • WebConfig.java
      • controller package
        • MainController.java