Skip to content

Commit

Permalink
build-项目增加前缀gw,方便后期代理
Browse files Browse the repository at this point in the history
  • Loading branch information
aruis committed Dec 9, 2024
1 parent 6f8e2d6 commit fe29a3c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
5 changes: 3 additions & 2 deletions gateway-ui/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import react from '@vitejs/plugin-react'
// https://vite.dev/config/
export default defineConfig({
plugins: [react()],
base: '/gw/',
build: {
outDir: '../src/main/resources/META-INF/resources/', // 替换为你想要的输出路径
outDir: '../src/main/resources/META-INF/resources/gw/', // 替换为你想要的输出路径
assetsDir: 'static', // 指定静态资源存放的文件夹
sourcemap: true, // 是否生成 sourcemap 文件
},
server: {
proxy: {
'/api': {
'/gw/api': {
target: 'http://localhost:8888/', // 后端服务器地址
changeOrigin: true, // 是否改变请求源
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@
import jakarta.ws.rs.ext.Provider;
import net.ximatai.muyun.gateway.GatewayServer;
import net.ximatai.muyun.gateway.config.Management;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;

@Provider
@ApplicationScoped
public class GatewayAuthFilter implements ContainerRequestFilter {

private static final Logger LOGGER = LoggerFactory.getLogger(GatewayAuthFilter.class);

@Inject
Management management;

Expand All @@ -30,12 +34,14 @@ public void filter(ContainerRequestContext requestContext) {

// 验证 IP 地址
if (!management.allowedIps().contains("*") && !management.allowedIps().contains(clientIp)) {
LOGGER.warn("Unauthorized IP:{}", clientIp);
requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED)
.entity("Unauthorized IP").build());
return;
}

if (management.checkToken() && !Objects.equals(token, GatewayServer.token)) {
LOGGER.warn("Invalid token:{}", token);
requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED)
.entity("Invalid token").build());
return;
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ quarkus:
http:
port: 8888
enable-compression: true
non-application-root-path: /gw
rest:
path: /api
path: /gw/api
banner:
path: banner.txt
log:
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/net/ximatai/muyun/gateway/TestSecurity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class TestSecurity {
@Test
public void testSecurityBad() {
given()
.get("/api/config")
.get("/gw/api/config")
.then()
.statusCode(401);

Expand All @@ -22,7 +22,7 @@ public void testSecurityOk() {
String token = GatewayServer.token;
given()
.header("token", token)
.get("/api/config")
.get("/gw/api/config")
.then()
.statusCode(200);
}
Expand Down

0 comments on commit fe29a3c

Please sign in to comment.