我使用Spring Boot框架处理JEE项目。 对于身份验证,我使用Spring Security,并在模板中指定了页面。
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/login").permitAll()
.antMatchers("/token", "/index", "/index.html", "/main", "/main.html", "/main2", "/main2.html", "/recent1", "/recent1.html", "/recent2", "/recent2.html").hasRole("USER");
http
.csrf()
.disable()
.formLogin()
.loginPage("/login")
.failureUrl("/login?error=true")
.defaultSuccessUrl("/index");
http
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/login");
}
问题在于,当我运行应用程序并使用大写字母编写URL时:localhost:8080/INDEX.HTML
或者我添加了两个字母localhost/index.httml
,页面确实显示没有自动识别。
答案 0 :(得分:-1)
如果我理解正确,这是理想的逻辑:
/login
不需要由Spring Security保护(不需要任何角色)要实现此目的,您可以尝试以下方法:
@Override
public void configure(WebSecurity web) throws Exception {
// configuring here URLs for which security filters
// will be disabled (this is equivalent to using
// security="none")
web
.ignoring()
.antMatchers(
"/login"
)
;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().hasRole("USER");
http.csrf().disable().formLogin()
.loginPage("/login").failureUrl("/login?error=true")
.defaultSuccessUrl("/index");
http.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/login");
}
因此,第1项(/login
上没有安全保护)被移至configure(WebSecurity)
,第2项保留在原始configure(HttpSecurity)
中。