SpringBoot+SpringSecurity之如何forword到登录页面

当我们在项目中引入了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。

posted @ 2018-11-15 15:52  柚子苹果果  阅读(811)  评论(0编辑  收藏  举报