简体   繁体   English

映射错误 Thymeleaf Spring Boot Security 应用程序,不加载模板

[英]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.这是我第一次在这里问,我很关心这个,所以我使用Spring Securirty和Spring boot开发了一个Web应用程序,代码似乎很好并且应用程序运行,我只配置了登录,会话和注册页面,当我转到根 url localhost:8080/ 索引模板正确加载,但是当我转到其他 url 例如 /login 或 /register 时,我的模板不会显示,如果我更改主 url 的模板 (localhost:8080) /) 并返回 index 以外的内容,它会一直返回 index,总而言之,Web 仅加载模板文件夹下名为 index.html 的文件。 This is my file structure: hirearchy这是我的文件结构: hirarchy

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);
    }

}

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.所以我设法解决了这个问题,它是包层次结构和资源加载失败,spring 应用程序加载了它类下的所有内容和每个包,我的应用程序包(和其中的应用程序类)不是根包,也在控制器下面一。 This is the structure it has to follow:这是它必须遵循的结构:

  • root package根包
    • SpringApp.java SpringApp.java
    • WebConfig.java配置文件
    • controller package控制器包
      • MainController.java主控制器.java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM