添加启用配置.

This commit is contained in:
lijiahang
2023-07-08 00:59:19 +08:00
parent 36193f56fc
commit b417503a9e
5 changed files with 45 additions and 14 deletions

View File

@@ -1,9 +1,12 @@
package com.orion.ops.framework.security.core.filter; package com.orion.ops.framework.security.core.filter;
import com.orion.lang.utils.Strings; import com.orion.lang.utils.Strings;
import com.orion.ops.framework.common.constant.ErrorCode;
import com.orion.ops.framework.common.security.LoginUser; import com.orion.ops.framework.common.security.LoginUser;
import com.orion.ops.framework.security.core.service.SecurityFrameworkService; import com.orion.ops.framework.security.core.service.SecurityFrameworkService;
import com.orion.ops.framework.security.core.utils.SecurityUtils; import com.orion.ops.framework.security.core.utils.SecurityUtils;
import com.orion.web.servlet.web.Servlets;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.FilterChain; import javax.servlet.FilterChain;
@@ -20,6 +23,7 @@ import java.io.IOException;
* @version 1.0.0 * @version 1.0.0
* @since 2023/7/6 18:39 * @since 2023/7/6 18:39
*/ */
@Slf4j
public class TokenAuthenticationFilter extends OncePerRequestFilter { public class TokenAuthenticationFilter extends OncePerRequestFilter {
private final SecurityFrameworkService securityFrameworkService; private final SecurityFrameworkService securityFrameworkService;
@@ -30,17 +34,22 @@ public class TokenAuthenticationFilter extends OncePerRequestFilter {
@Override @Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
// 获取请求头 token try {
String token = SecurityUtils.obtainAuthorization(request); // 获取请求头 token
if (!Strings.isBlank(token)) { String token = SecurityUtils.obtainAuthorization(request);
// 通过 token 获取用户信息 if (!Strings.isBlank(token)) {
LoginUser loginUser = securityFrameworkService.getUserByToken(token); // 通过 token 获取用户信息
// 设置上下文 LoginUser loginUser = securityFrameworkService.getUserByToken(token);
if (loginUser != null) { // 设置上下文
SecurityUtils.setLoginUser(loginUser, request); if (loginUser != null) {
SecurityUtils.setLoginUser(loginUser, request);
}
} }
} catch (Exception e) {
log.error("TokenAuthenticationFilter.doFilterInternal error", e);
Servlets.writeHttpWrapper(response, ErrorCode.INTERNAL_SERVER_ERROR.getWrapper());
return;
} }
// todo 全局异常返回 mock模式
// 继续执行 // 继续执行
chain.doFilter(request, response); chain.doFilter(request, response);
} }

View File

@@ -3,10 +3,13 @@ package com.orion.ops.framework.storage.config;
import com.orion.ops.framework.storage.core.client.FileClient; import com.orion.ops.framework.storage.core.client.FileClient;
import com.orion.ops.framework.storage.core.client.local.LocalFileClient; import com.orion.ops.framework.storage.core.client.local.LocalFileClient;
import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
import javax.annotation.Resource;
/** /**
* 存储配置类 * 存储配置类
* *
@@ -20,12 +23,16 @@ import org.springframework.context.annotation.Primary;
@EnableConfigurationProperties(StorageConfig.class) @EnableConfigurationProperties(StorageConfig.class)
public class OrionStorageAutoConfiguration { public class OrionStorageAutoConfiguration {
@Resource
private StorageConfig config;
/** /**
* 本地文件客户端 * 本地文件客户端
*/ */
@Bean @Bean
@Primary @Primary
public FileClient localFileClient(StorageConfig config) { @ConditionalOnProperty(value = "orion.storage.local.enabled", havingValue = "true")
public FileClient localFileClient() {
return new LocalFileClient(config.getLocal()); return new LocalFileClient(config.getLocal());
} }

View File

@@ -12,6 +12,11 @@ import lombok.Data;
@Data @Data
public class FileClientConfig { public class FileClientConfig {
/**
* 是否启用
*/
protected boolean enabled;
/** /**
* 是否自动拼接 traceId 前缀. 没有则使用 UUID * 是否自动拼接 traceId 前缀. 没有则使用 UUID
*/ */

View File

@@ -4,23 +4,34 @@
"name": "orion.storage", "name": "orion.storage",
"type": "com.orion.ops.framework.storage.config.StorageConfig", "type": "com.orion.ops.framework.storage.config.StorageConfig",
"sourceType": "com.orion.ops.framework.storage.config.StorageConfig" "sourceType": "com.orion.ops.framework.storage.config.StorageConfig"
},
{
"name": "orion.storage.local",
"type": "com.orion.ops.framework.storage.core.client.local.LocalFileClientConfig",
"sourceType": "com.orion.ops.framework.storage.core.client.local.LocalFileClientConfig"
} }
], ],
"properties": [ "properties": [
{ {
"name": "orion.storage.local.nameAppendTraceId", "name": "orion.storage.local.enabled",
"type": "java.lang.Boolean",
"description": "是否启用.",
"defaultValue": false
},
{
"name": "orion.storage.local.name-append-trace-id",
"type": "java.lang.Boolean", "type": "java.lang.Boolean",
"description": "是否自动拼接 traceId 前缀. 没有则使用 UUID.", "description": "是否自动拼接 traceId 前缀. 没有则使用 UUID.",
"defaultValue": false "defaultValue": false
}, },
{ {
"name": "orion.storage.local.storagePath", "name": "orion.storage.local.storage-path",
"type": "java.lang.String", "type": "java.lang.String",
"description": "存储路径.", "description": "存储路径.",
"defaultValue": "" "defaultValue": ""
}, },
{ {
"name": "orion.storage.local.basePath", "name": "orion.storage.local.base-path",
"type": "java.lang.String", "type": "java.lang.String",
"description": "基础路径.", "description": "基础路径.",
"defaultValue": "" "defaultValue": ""

View File

@@ -27,7 +27,6 @@ public class EmptySecurityImpl implements SecurityFrameworkService {
@Override @Override
public LoginUser getUserByToken(String token) { public LoginUser getUserByToken(String token) {
// TODO MOCK
LoginUser user = new LoginUser(); LoginUser user = new LoginUser();
user.setId(123L); user.setId(123L);
user.setUsername("username"); user.setUsername("username");