Spring Boot: accessDeniedHandler does not work

前端 未结 4 951
北荒
北荒 2020-12-14 20:44

I\'ve got the following Spring Security configuration:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter          


        
4条回答
  •  情话喂你
    2020-12-14 20:56

    The normal Spring Security behavior is to redirect unauthenticated users to your login page as configured below. Authenticates users who are not authorized (dont have the ADMIN role) will be directed to the access denied page:

    http.authorizeRequests().antMatchers("/admin/**")
        .access("hasRole('ADMIN')")
        .and().formLogin().loginPage("/login")
        .and().exceptionHandling().accessDeniedPage("/403");
    

    If you have implemented your own authentication mechanism and you are not counting on the Spring Security configuration to deliver unauthenticated users to your login page, you can game the Spring Security configuration as follows - to serve your custom 403 page instead of a real login page:

    http.authorizeRequests().antMatchers("/admin/**")
        .access("hasRole('ADMIN')")
        .and().formLogin().loginPage("/403")
        .and().exceptionHandling().accessDeniedPage("/403");
    

提交回复
热议问题