因此,我是Spring
的新手,并且在使用Spring-Boot
开发Web应用程序时以这种方式学习。
目前,我的页面由两个html
页面组成:index.html
和login.html
。我也在使用Spring-Security
。
这是我当前的MvcConfig
:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/login").setViewName("login");
}
}
网站的设计方式是,用户转到URL http://localhost:8080,然后为他/她显示初始页面,在那里有一个
login
选项卡,他/她可以登录并移动到dashboard
视图(稍后将添加)。但是,当我加载初始字母时,页面完全配置错误(未加载CSS / JS /图片资源)。我转到http://localhost:8080/login后,执行登录,然后一切再次正常。
因此,任何形式为http://localhost:8080的url(
index.html
)都是允许的,但其他任何内容都需要登录。这是我的
Spring-Security
配置:public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.regexMatchers("/", "/index").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
}
如何正确配置我的网页?
***注意事项:
*我目前没有任何Controller类。
最佳答案
我发现正则表达式匹配器的问题是从服务器加载的任何资源,您都需要在映射中说明。
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login", "/admin").hasRole('ADMIN') // e.g. for pages that need to be authenticated
.anyRequest().permitAll() // all the others will be accessable by all
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
}
进行匹配的最简单方法是执行以下步骤:
通过覆盖addResourceHandlers声明资源文件
使用antmatchers来处理URL安全性(更简单),除非您具有带有关键参数的极其动态的URL