【问题标题】:SpringBoot and SpringSecurity Configuration about 404 error pageSpring Boot 和 Spring Security 配置关于 404 错误页面
【发布时间】:2018-01-08 16:55:18
【问题描述】:

我遇到了 Spring Security 和错误页面的问题,因为当我登录应用程序时,我可以在页面不存在时显示。

但是当我退出应用程序时,我的 spring security 默认显示登录页面。

这是我的 spring 安全配置。

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    private DataSource dataSource;

    @Value("${spring.queries.users-query}")
    private String usersQuery;

    @Value("${spring.queries.roles-query}")
    private String rolesQuery;

    @Override
    protected void configure(AuthenticationManagerBuilder auth)
            throws Exception {
        auth.
            jdbcAuthentication()
                .usersByUsernameQuery(usersQuery)
                .authoritiesByUsernameQuery(rolesQuery)
                .dataSource(dataSource)
                .passwordEncoder(bCryptPasswordEncoder);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.
            authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/login").permitAll()
                .antMatchers("/registration").permitAll()
                .antMatchers("/admin/**").hasAuthority("ADMIN")
                .antMatchers("/user_login").hasAuthority("USER").anyRequest()
                .authenticated().and().csrf().disable().formLogin()
                .loginPage("/login").failureUrl("/login?error=true")
                .defaultSuccessUrl("/user_login")
                .usernameParameter("email")
                .passwordParameter("password")
                .and().logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .logoutSuccessUrl("/")
                .and().exceptionHandling()
                .accessDeniedPage("/access-denied");


    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web
           .ignoring()
           .antMatchers("/resources/**", "/static/**", "/css/**", "/js/**", "/images/**");
    }

}

这可以正常工作,但我不知道为什么当我退出应用程序时我会重定向到登录页面。

有什么解决办法吗?

问候!

【问题讨论】:

  • 因为除 / 、 /login 和 /registration 以外的任何请求都需要认证。 (anyRequest().authenticated()) 。因此,如果您请求 /abcdef spring 将检查是否经过身份验证,如果没有则重定向到登录。我不知道你为什么要改变这个。

标签: java spring


【解决方案1】:

//login/registration 之外的所有请求都需要对用户进行身份验证(anyRequest().authenticated()),并且当您启用formLogin() 时,spring 的过滤器会将所有未经过身份验证的请求重定向到登录页面即使页面没有退出,这就是为什么您被重定向到登录页面并且没有收到 404 错误。

出于测试目的,您可以添加测试匹配器,而无需在控制器中添加实际端点,如下所示: .antMatchers("/test").permitAll() 并尝试在未经身份验证的情况下访问此端点,您将得到 404 错误页面。

附言确保 404 响应也没有被阻止(如果它是控制器响应,那么也启用它,因为每个人都允许您的 js)。

【讨论】:

  • 您好!我试图解决这个问题,但我找不到任何关于 Spring Filters 的解决方案,你能帮帮我吗?
  • @jc1992 阅读了有关 servlet 过滤器的文档,并且 hot spring 正在使用它们,一开始会有点混乱,但最终你会明白的。并阅读有关 HttpSecurity 对象的更多信息,它将帮助您了解您的问题。
猜你喜欢
  • 2014-07-25
  • 2017-10-06
  • 2021-02-15
  • 2017-01-27
  • 2019-04-02
  • 2018-07-22
  • 1970-01-01
  • 2014-11-30
  • 2013-11-01
相关资源
最近更新 更多