我有一个简单的WebSecurityConfiguration配置类,它定义了我的应用程序如何在安全级别上工作。
@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.antMatcher("/**")
.authorizeRequests()
.antMatchers("/register", "/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.failureUrl("/login?error")
.passwordParameter("password")
.usernameParameter("emailAddress")
.successHandler(authenticationSuccessHandler())
.permitAll()
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/")
.permitAll()
.and()
.httpBasic();
}
@Bean
public SavedRequestAwareAuthenticationSuccessHandler authenticationSuccessHandler() {
return new SavedRequestAwareAuthenticationSuccessHandler();
}
}
我还有一个@Controller定义了两个简单端点
@Controller
public class HomeController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String getHome() {
return "home";
}
@RequestMapping(value = "/test", method = RequestMethod.GET)
public void testEndpoint() throws CreateException {
return "test";
}
}
当加载应用程序并导航到时,localhost:8080/test我会按预期重定向到登录表单。但是,当我导航到localhost:8080/或localhost:8080(无正斜杠)时,我会看到我希望被重定向到的“主页”页面localhost:8080/login。
我尝试将其更改为.antMatcher("/**"),.antMatcher("**")但这也没有达到预期的效果。