本身的单独访问每个客户端服务的单点就没有问题。然后单点通过Fegin调用就不好使了!
<!--SA-Token SSO-->
<dependencyManagement>
<dependencies>
<!-- Sa-Token 权限认证, 在线文档:https://sa-token.cc -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot-starter</artifactId>
<version>1.33.0</version>
</dependency>
<!-- Sa-Token 插件:整合SSO -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-sso</artifactId>
<version>1.33.0</version>
</dependency>
<!-- Sa-Token 整合redis (使用jackson序列化方式) -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-dao-redis-jackson</artifactId>
<version>1.33.0</version>
</dependency>
<!-- Sa-Token插件:权限缓存与业务缓存分离 -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-alone-redis</artifactId>
<version>1.33.0</version>
</dependency>
</dependencies>
</dependencyManagement>
/**
* @description: SA-Token 认证中心登录状态校验拦截器
* @author: GuoTong
* @createTime: 2022-10-05 15:40
* @since JDK 1.8 OR 11
**/
public class AuthenticationInterceptor implements HandlerInterceptor {
/**
* Description: 主要流程:
* <p>
* 1.从 http 请求头中取出 token,
* 2.判断是否映射到方法
* 3.检查是否有SkipTokenByJWT注解注释,有则跳过认证
* 4.检查有没有需要用户登录的注解NeedTokenByJWT,有则需要取出并验证
* 5.认证通过则可以访问,不通过会报相关错误信息
*/
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws Exception {
// 如果不是映射到方法直接通过
if (!(object instanceof HandlerMethod)) {
return true;
}
String requestURI = httpServletRequest.getRequestURI();
// 判断是否是认证中心对外认证接口
if (requestURI.contains("/sso")) {
return true;
}
// 获取当前会话是否已经登录,返回true=已登录,false=未登录 ||只能获取本服务的登录状态 StpUtil.isLogin
boolean login = StpUtil.isLogin();
if (!login) {
// 跳转认证中心 httpServletResponse.sendRedirect("/sso/login?back=" + httpServletRequest.getRequestURL());
throw new NotLoginException(ContextCommonMsg.LOGIN_STATUS_EXPIRE);
}
return true;
}
}
@Bean
public RequestInterceptor requestInterceptor() {
return template -> {
// 获取请求进入的数据
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (requestAttributes != null) {
// fegin调用前的原始请求
HttpServletRequest request = requestAttributes.getRequest();
if (request != null) {
String cookie = request.getHeader("Cookie");
// 添加cookie
template.header("Cookie", cookie);
// 添加Content-Type
String contentType = request.getContentType();
template.header("Content-Type", contentType);
}
}
};
}