簡體   English   中英

Spring Boot自定義登錄頁面顯示404

[英]Spring Boot custom login page displays 404

嗨,我目前正在嘗試使用 Spring Boot Security 設置一個簡單的登錄頁面,但是每當我嘗試訪問登錄視圖時,我都會收到 404“找不到頁面”錯誤。

安全配置:

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

登錄視圖,使用 freemarker(位於 main/resources/templates/login.ftl):

<body class="login">
<div>
<div class="login_wrapper">
    <div class="animate form login_form">
        <section class="login_content">
            <form>
                <h1>Login Form</h1>
                <div>
                    <input type="text" class="form-control" placeholder="Username" required="" />
                </div>
                <div>
                    <input type="password" class="form-control" placeholder="Password" required="" />
                </div>
                <div>
                    <a class="btn btn-default submit" href="/units">Log in</a>
                </div>
                <div class="clearfix"></div>
            </form>
        </section>
    </div>
</div>
</div>
</body>

有誰知道我做錯了什么? 謝謝您的幫助!

您需要有一個用於 /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 目錄中。 你都准備好了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM