refactor(satoken): 适配单体版本

This commit is contained in:
冷冷 2024-11-13 14:34:39 +08:00
parent 5fbf844d99
commit 391d332b57
4 changed files with 81 additions and 78 deletions

View File

@ -35,10 +35,9 @@ security:
- /swagger-ui.html
- /swagger-ui/**
- /swagger-resources
- /token/check_token
- /error
- /token/**
- /actuator/**
- /oauth2/**
- /code/**
#--------------如下配置尽量不要变动-------------

View File

@ -18,8 +18,8 @@ package com.pig4cloud.pig.common.security.annotation;
import com.pig4cloud.pig.common.security.config.PermissionAspect;
import com.pig4cloud.pig.common.security.config.PermitAllUrlProperties;
import com.pig4cloud.pig.common.security.config.ResourceServerConfiguration;
import com.pig4cloud.pig.common.security.config.SaPermissionImpl;
import com.pig4cloud.pig.common.security.config.SecurityConfiguration;
import com.pig4cloud.pig.common.security.feign.PigFeignClientConfiguration;
import org.springframework.context.annotation.Import;
@ -33,10 +33,10 @@ import java.lang.annotation.*;
*/
@Documented
@Inherited
@Target({ ElementType.TYPE })
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Import({ SecurityConfiguration.class, SaPermissionImpl.class, PermissionAspect.class, PermitAllUrlProperties.class,
PigFeignClientConfiguration.class })
@Import({ResourceServerConfiguration.class, SaPermissionImpl.class, PermissionAspect.class, PermitAllUrlProperties.class,
PigFeignClientConfiguration.class})
public @interface EnablePigResourceServer {
}

View File

@ -0,0 +1,76 @@
package com.pig4cloud.pig.common.security.config;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.filter.SaServletFilter;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.oauth2.data.model.AccessTokenModel;
import cn.dev33.satoken.oauth2.template.SaOAuth2Util;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.SaLoginConfig;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.core.util.WebUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* Resource Server 配置
*
* @author lengleng
* @date 2024/11/13
*/
@RequiredArgsConstructor
public class ResourceServerConfiguration implements WebMvcConfigurer {
@Autowired
@Lazy
private PermitAllUrlProperties permitAllUrlProperties;
/**
* 添加 sa-token 注解拦截器
*
* @param registry 注册表
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
SaInterceptor saInterceptor = new SaInterceptor();
saInterceptor.isAnnotation(true);
registry.addInterceptor(saInterceptor).addPathPatterns("/**");
}
/**
* 校验是否从网关转发
*/
@Bean
public SaServletFilter getSaServletFilter() {
SaServletFilter saServletFilter = new SaServletFilter().addInclude("/**").setAuth(obj -> {
SaRouter.match("/**").check(() -> {
// Authorization: bearer token
String token = WebUtils.getToken();
AccessTokenModel accessTokenModel = SaOAuth2Util.checkAccessToken(token);
// 登录
String loginId = accessTokenModel.loginId.toString();
StpUtil.login(loginId, SaLoginConfig.setToken(token));
});
}).setError(e -> {
SaHolder.getResponse().setHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8");
SaHolder.getResponse().setStatus(HttpStatus.FAILED_DEPENDENCY.value());
// 使用封装的 JSON 工具类转换数据格式
return JSONUtil.toJsonStr(R.failed("认证失败,无法访问系统资源"));
});
for (String url : permitAllUrlProperties.getUrls()) {
saServletFilter.addExclude(url);
}
return saServletFilter;
}
}

View File

@ -1,72 +0,0 @@
package com.pig4cloud.pig.common.security.config;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.filter.SaServletFilter;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.oauth2.data.model.AccessTokenModel;
import cn.dev33.satoken.oauth2.template.SaOAuth2Util;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.SaLoginConfig;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.core.util.WebUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author lengleng
* @date 2024/7/22
*/
@RequiredArgsConstructor
public class SecurityConfiguration implements WebMvcConfigurer {
@Autowired
@Lazy
private PermitAllUrlProperties permitAllUrlProperties;
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 注解拦截器
SaInterceptor saInterceptor = new SaInterceptor();
saInterceptor.isAnnotation(true);
registry.addInterceptor(saInterceptor).addPathPatterns("/**");
}
/**
* 校验是否从网关转发
*/
@Bean
public SaServletFilter getSaServletFilter() {
SaServletFilter saServletFilter = new SaServletFilter().addInclude("/**").setAuth(obj -> {
SaRouter.match("/**").check(() -> {
// Authorization: bearer token
String token = WebUtils.getToken();
AccessTokenModel accessTokenModel = SaOAuth2Util.checkAccessToken(token);
// 登录
String loginId = accessTokenModel.loginId.toString();
StpUtil.login(loginId, SaLoginConfig.setToken(token));
});
}).setError(e -> {
SaHolder.getResponse().setHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8");
SaHolder.getResponse().setStatus(HttpStatus.FAILED_DEPENDENCY.value());
// 使用封装的 JSON 工具类转换数据格式
return JSONUtil.toJsonStr(R.failed("认证失败,无法访问系统资源"));
});
for (String url : permitAllUrlProperties.getUrls()) {
saServletFilter.addExclude(url);
}
return saServletFilter;
}
}