当我们在项目中引入了SpringSecurity框架进行身份校验的时候,如果某个请求需要用户身份认证,那么SpringSecurity会将用户redirect到登录页面。但是有些时候我们希望是forward到登录页面而不是redirect到登录页面,这种情况下可做如下配置:
@Bean public AuthenticationEntryPoint loginUrlAuthenticationEntryPoint() { LoginUrlAuthenticationEntryPoint AuthenticationEntryPoint = new LoginUrlAuthenticationEntryPoint("/oauth/login"); AuthenticationEntryPoint.setUseForward(true); return AuthenticationEntryPoint; } @Bean public RequestCache requestCache() { return new HttpSessionRequestCache(); } @Bean public ExceptionTranslationFilter exceptionTranslationFilter(AuthenticationEntryPoint authenticationEntryPoint, RequestCache requestCache) { return new ExceptionTranslationFilter(authenticationEntryPoint, requestCache); }
关键代码就是标红这部分,但是为了实现这个功能,我们需要创建三个bean。