0

我有一个简单的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("**")但这也没有达到预期的效果。

4

1 回答 1

0

问题是一个 the.formLogin()并且.logout()它们以 . 结尾.permitAll()。这允许根localhost:8080无需经过身份验证即可通过。

通过删除它们,它已经解决了这个问题。

于 2018-04-26T08:50:41.327 回答