我的Spring Boot应用程序基本上由两个主要的“模块”组成:
基本的app结构是:
index.html
:主页,从http://localhost:8080/
about.html
:关于页面,从http://localhost:8080/about
contact.html
:联系页面,从http://localhost:8080/contact
login.html
:登录页面,从http://localhost:8080/login
dashboard.html
:登录后访问的仪表板/登录页面,从http://localhost:8080/account
http://localhost:8080/account/*
下的所有其他网页都是具有典型@RequestMapping
映射的MVC /动态网页对我来说,不清楚的是,对于静态(“公共网页”)HTML页面,我是否:
@RequestMappings
来渲染一些Thymeleaf / Handlebars模板(由于内容是静态的,因此根本没有数据模型)?或者我:http://localhost:8080/contact
而不是http://localhost:8080/contact.html
)?答案 0 :(得分:1)
@Configuration
public class WebMVCconfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
registry.addViewController("/contact").setViewName("contact");
}
}
在映射您的视图后,我们需要在/login
中添加antMatchers
之类的视图,所以假设您的配置方法如下所示:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/contact","/about", ...).permitAll() //add your static pages here ( public pages )
.anyRequest()
.authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.successHandler(successHandler) // i don't think you'll be needing this it's to redirect the user if it's admin or a simple user
.and() // this is for the logout
.logout()
.invalidateHttpSession(true)
.logoutUrl("/logout")
.permitAll();
}
在antMatchers
spring security中添加您的视图后,不会处理这些页面的身份验证并解决您的问题