|
@@ -3,10 +3,6 @@ package cn.iocoder.yudao.ssodemo.framework.core;
|
|
|
import cn.iocoder.yudao.ssodemo.client.OAuth2Client;
|
|
|
import cn.iocoder.yudao.ssodemo.client.dto.CommonResult;
|
|
|
import cn.iocoder.yudao.ssodemo.client.dto.oauth2.OAuth2CheckTokenRespDTO;
|
|
|
-import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
-import org.springframework.security.core.Authentication;
|
|
|
-import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
-import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.filter.OncePerRequestFilter;
|
|
@@ -17,7 +13,6 @@ import javax.servlet.ServletException;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
-import java.util.Collections;
|
|
|
|
|
|
/**
|
|
|
* Token 过滤器,验证 token 的有效性
|
|
@@ -35,13 +30,13 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {
|
|
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
|
|
FilterChain filterChain) throws ServletException, IOException {
|
|
|
// 1. 获得访问令牌
|
|
|
- String token = obtainAuthorization(request);
|
|
|
+ String token = SecurityUtils.obtainAuthorization(request, "Authentication");
|
|
|
if (StringUtils.hasText(token)) {
|
|
|
// 2. 基于 token 构建登录用户
|
|
|
LoginUser loginUser = buildLoginUserByToken(token);
|
|
|
// 3. 设置当前用户
|
|
|
if (loginUser != null) {
|
|
|
- setLoginUser(loginUser, request);
|
|
|
+ SecurityUtils.setLoginUser(loginUser, request);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -58,50 +53,12 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {
|
|
|
}
|
|
|
// 构建登录用户
|
|
|
return new LoginUser().setId(accessToken.getUserId()).setUserType(accessToken.getUserType())
|
|
|
- .setTenantId(accessToken.getTenantId()).setScopes(accessToken.getScopes());
|
|
|
+ .setTenantId(accessToken.getTenantId()).setScopes(accessToken.getScopes())
|
|
|
+ .setAccessToken(accessToken.getAccessToken());
|
|
|
} catch (Exception exception) {
|
|
|
// 校验 Token 不通过时,考虑到一些接口是无需登录的,所以直接返回 null 即可
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 从请求 Header 中,获得访问令牌
|
|
|
- *
|
|
|
- * @param request 请求
|
|
|
- * @return 访问令牌
|
|
|
- */
|
|
|
- private static String obtainAuthorization(HttpServletRequest request) {
|
|
|
- String authorization = request.getHeader("Authentication");
|
|
|
- if (!StringUtils.hasText(authorization)) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- int index = authorization.indexOf("Bearer ");
|
|
|
- if (index == -1) { // 未找到
|
|
|
- return null;
|
|
|
- }
|
|
|
- return authorization.substring(index + 7).trim();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 设置当前用户
|
|
|
- *
|
|
|
- * @param loginUser 登录用户
|
|
|
- * @param request 请求
|
|
|
- */
|
|
|
- private static void setLoginUser(LoginUser loginUser, HttpServletRequest request) {
|
|
|
- // 创建 Authentication,并设置到上下文
|
|
|
- Authentication authentication = buildAuthentication(loginUser, request);
|
|
|
- SecurityContextHolder.getContext().setAuthentication(authentication);
|
|
|
- }
|
|
|
-
|
|
|
- private static Authentication buildAuthentication(LoginUser loginUser, HttpServletRequest request) {
|
|
|
- // 创建 UsernamePasswordAuthenticationToken 对象
|
|
|
- UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
|
|
|
- loginUser, null, Collections.emptyList());
|
|
|
- authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
|
|
|
- return authenticationToken;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
}
|