Spring Boot自定义登录页面显示404

时间:2017-01-31 16:40:34

标签: java spring-boot spring-security freemarker

您好我正在尝试使用Spring Boot Security设置一个简单的登录页面,但每当我尝试访问登录视图时,我都会找到404"页面未找到"错误。

安全配置:

For a = 1 to 10
    if a = dumm then next a 'this statement should avoid running the subsequent codes if the if statement is true (similar to continue in c++
    end if
    'statements that need to run when the if statement is not true

next a 

登录视图,使用freemarker(位于main / resources / templates / login.ftl):

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
        .logout()
            .permitAll();
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN");
}
}

任何人都知道我做错了什么?谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

您需要为/ login提供视图控制器。要么为此写一个控制器,要么就是这样做。

@EnableWebMvc
@ComponentScan("package_name")
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {

    // ...

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("login");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }
}

设置视图rosolver:

@Bean  
public InternalResourceViewResolver viewResolver() {  
InternalResourceViewResolver resolver = new InternalResourceViewResolver();  
    resolver.setPrefix("/WEB-INF/pages/");  
    resolver.setSuffix(".ftl");
    return resolver;  
}

现在将您的ftl文件放在webapp / WEB-INF / pages目录中。你已经全部准备好了。