Spring boot - 返回403 Forbidden而不是重定向到登录页面

时间:2016-04-22 20:26:35

标签: java spring spring-mvc spring-security spring-boot

在Spring Boot Web应用程序中,我有以下安全配置:

@Override
public void configure(HttpSecurity http) throws Exception {
    // @formatter:off   
    http
        .headers().frameOptions().disable()
        .and()
            .antMatcher("/**").authorizeRequests()
            .antMatchers("/actuator/health").permitAll()
            .antMatchers("/actuator/**").hasAuthority(Authority.Type.ROLE_ADMIN.getName())
            .antMatchers("/login/**").permitAll()
            .anyRequest().authenticated()
        .and()
            .formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/login")
                .failureUrl("/login?error").permitAll()
        .and()
            .logout()
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
            .logoutSuccessUrl("/login?logout")
        .and()
            .csrf().csrfTokenRepository(csrfTokenRepository())
        .and()
            .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class)
            .addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
    // @formatter:on
}

现在,当我尝试访问例如以下网址:/api/v1.0/user时,我会将我重定向到/api/login页面。

如何配置此项以返回403 Forbidden而不是重定向到登录页面?

1 个答案:

答案 0 :(得分:4)

这是一个社区答案:

<强>问题:

禁止的网址返回登录页面内容(而不是403状态代码)。

我有这段代码:

...
http.authorizeRequests().antMatchers("/uri/**").hasAuthority("SOME_ROLE");

改变了Tong的建议:

...
http.exceptionHandling().authenticationEntryPoint(new Http403ForbiddenEntryPoint());
http.authorizeRequests().antMatchers("/uri/**").hasAuthority("SOME_ROLE");
相关问题