Title

oauth2中没有权限访问重定向自定义页面

  • 编写一个 AuthExceptionEntryPoint 实现 AuthenticationEntryPoint
package com.zl.stu.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 无权访问跳转登录页面
 *
 * @author z
 * @date 2022-01-06 14:38
 */
public class AuthExceptionEntryPoint implements AuthenticationEntryPoint {
    @Value("${user.redirectUrl}")
    private String redirectUrl;

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        // 配置没有权限跳转到首页
        response.sendRedirect(redirectUrl);
    }
}
  • 在 springboot 配置文件中增加重定向地址配置
user:
  redirectUrl: "http://localhost:8097/index"
  • 最后再 oauth2 的资源服务器中添加配置
    /**
     * 资源安全配置
     *
     * @param resources
     * @throws Exception
     */
    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.authenticationEntryPoint(new AuthExceptionEntryPoint());
    }
  • 然后访问没有权限的地址就会跳转到自定义的地址了
posted @ 2022-01-06 15:00  快乐小洋人  阅读(285)  评论(0编辑  收藏  举报