javaMap错误,spring boot安全应用程序未加载模板

hi3rlvi2  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(359)

这是我第一次问这个问题,我很关心这个问题,所以我用spring Security和spring boot开发了一个web应用程序,代码似乎很好,应用程序运行,我只配置了登录、会话和注册页面,当我转到根url时localhost:8080/ 索引模板加载正确,但当我转到其他url(例如/login或/register)时,也不会显示我的模板,如果我更改了主url的模板(localhost:8080/)返回的不是索引它一直返回索引,总之,web只加载templates文件夹下名为index.html的文件。这是我的文件结构:hirearchy
控制器类

@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安全配置

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

应用程序类

@SpringBootApplication
public class Application {

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

}
uyto3xhc

uyto3xhc1#

所以我设法弄明白了这一点,它是包hierarchy和资源加载失败,spring app加载it类下的所有东西和每个包,我的app包(和其中的app类)不是根包,也低于controller包。这是它必须遵循的结构:
根程序包
springapp.java版本
网络配置.java
控制器包
主控制器.java

相关问题