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

ale*_*oid 7 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
}
Run Code Online (Sandbox Code Playgroud)

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

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

ale*_*xbt 7

这是一个社区答案:

问题:

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

我有这个代码:

...
http.authorizeRequests().antMatchers("/uri/**").hasAuthority("SOME_ROLE");
Run Code Online (Sandbox Code Playgroud)

根据童的建议更改:

...
http.exceptionHandling().authenticationEntryPoint(new Http403ForbiddenEntryPoint());
http.authorizeRequests().antMatchers("/uri/**").hasAuthority("SOME_ROLE");
Run Code Online (Sandbox Code Playgroud)