У меня есть простой 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("**")
, но это также не дает желаемого эффекта.