- 编写一个 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"
/**
* 资源安全配置
*
* @param resources
* @throws Exception
*/
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.authenticationEntryPoint(new AuthExceptionEntryPoint());
}