From cf961a133e30bcd3b680071c73f27d295e5c97ea Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Sun, 31 Jul 2022 20:10:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=99=BB=E5=BD=95=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=AE=9A=E4=B9=89.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../neutrino/core/web/HttpRequestHandler.java | 2 +- .../core/web/annotation/RestController.java | 1 - .../RestControllerAdviceHandler.java | 5 +- .../core/web/test1/GlobalAdviceHandler.java | 3 +- .../proxy/server/base/rest/Authorization.java | 38 ++++++++++++ .../proxy/server/base/rest/DBInitialize.java | 2 + .../server/base/rest/ExceptionConstant.java | 42 ++++++++++++++ .../proxy/server/base/rest/ResponseBody.java | 39 +++++++++++++ ...urerAdapter.java => ServiceException.java} | 29 ++++++---- .../base/rest/{ => config}/SqliteConfig.java | 2 +- .../rest/config/WebConfigurerAdapter.java | 58 +++++++++++++++++++ .../BaseAuthInterceptor.java | 3 +- .../{ => interceptor}/CorsInterceptor.java | 2 +- .../rest/interceptor/GlobalAdviceHandler.java | 48 +++++++++++++++ .../interceptor/GlobalExceptionHandler.java | 53 +++++++++++++++++ .../server/controller/IndexController.java | 55 ++++++++++++++++++ .../server/controller/UserController.java | 41 +++++++++++++ .../proxy/server/controller/req/LoginReq.java | 41 +++++++++++++ .../proxy/server/controller/res/LoginRes.java | 47 +++++++++++++++ .../proxy/server/service/UserService.java | 34 +++++++++++ .../src/main/resources/sql/user.data.sql | 3 + 21 files changed, 529 insertions(+), 19 deletions(-) create mode 100644 neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/Authorization.java create mode 100644 neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ExceptionConstant.java create mode 100644 neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ResponseBody.java rename neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/{WebConfigurerAdapter.java => ServiceException.java} (69%) rename neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/{ => config}/SqliteConfig.java (96%) create mode 100644 neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/config/WebConfigurerAdapter.java rename neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/{ => interceptor}/BaseAuthInterceptor.java (95%) rename neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/{ => interceptor}/CorsInterceptor.java (97%) create mode 100644 neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/interceptor/GlobalAdviceHandler.java create mode 100644 neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/interceptor/GlobalExceptionHandler.java create mode 100644 neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/IndexController.java create mode 100644 neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/UserController.java create mode 100644 neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/req/LoginReq.java create mode 100644 neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/res/LoginRes.java create mode 100644 neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/UserService.java diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpRequestHandler.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpRequestHandler.java index 12ee9483..064827a4 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpRequestHandler.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpRequestHandler.java @@ -97,7 +97,7 @@ public class HttpRequestHandler { Object invokeResult = invoke(httpRouteResult.getInstance(), httpRouteResult.getMethod()); if (null != WebContextHolder.getAdviceHandler()) { - invokeResult = WebContextHolder.getAdviceHandler().advice(context, requestParser, routePath, httpRouteResult.getMethod(), invokeResult); + invokeResult = WebContextHolder.getAdviceHandler().advice(requestParser, HttpContextHolder.getHttpResponseWrapper(), routePath, httpRouteResult.getMethod(), invokeResult); } String res = String.valueOf(invokeResult); diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/annotation/RestController.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/annotation/RestController.java index a66ef072..af9dcc74 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/annotation/RestController.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/annotation/RestController.java @@ -38,7 +38,6 @@ import java.lang.annotation.Target; @Target(ElementType.TYPE) @Component @Intercept(ignoreGlobal = true) -//@NonIntercept public @interface RestController { } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/RestControllerAdviceHandler.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/RestControllerAdviceHandler.java index 37d6e219..920252de 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/RestControllerAdviceHandler.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/RestControllerAdviceHandler.java @@ -22,6 +22,7 @@ package fun.asgc.neutrino.core.web.interceptor; import fun.asgc.neutrino.core.web.context.HttpRequestWrapper; +import fun.asgc.neutrino.core.web.context.HttpResponseWrapper; import io.netty.channel.ChannelHandlerContext; import java.lang.reflect.Method; @@ -34,12 +35,12 @@ import java.lang.reflect.Method; public interface RestControllerAdviceHandler { /** * 返回结果处理 - * @param context * @param requestParser + * @param responseWrapper * @param route * @param targetMethod * @param res * @return */ - Object advice(ChannelHandlerContext context, HttpRequestWrapper requestParser, String route, Method targetMethod, Object res); + Object advice(HttpRequestWrapper requestParser, HttpResponseWrapper responseWrapper, String route, Method targetMethod, Object res); } diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/GlobalAdviceHandler.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/GlobalAdviceHandler.java index 497d9d61..b9dd7c9c 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/GlobalAdviceHandler.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/GlobalAdviceHandler.java @@ -22,6 +22,7 @@ package fun.asgc.neutrino.core.web.test1; import fun.asgc.neutrino.core.web.context.HttpRequestWrapper; +import fun.asgc.neutrino.core.web.context.HttpResponseWrapper; import fun.asgc.neutrino.core.web.interceptor.RestControllerAdviceHandler; import io.netty.channel.ChannelHandlerContext; @@ -35,7 +36,7 @@ import java.lang.reflect.Method; public class GlobalAdviceHandler implements RestControllerAdviceHandler { @Override - public Object advice(ChannelHandlerContext context, HttpRequestWrapper requestParser, String route, Method targetMethod, Object res) { + public Object advice(HttpRequestWrapper requestParser, HttpResponseWrapper responseWrapper, String route, Method targetMethod, Object res) { if (res instanceof JsonResult) { return res; } diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/Authorization.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/Authorization.java new file mode 100644 index 00000000..409418b5 --- /dev/null +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/Authorization.java @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.proxy.server.base.rest; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * @author: aoshiguchen + * @date: 2022/7/31 + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD}) +public @interface Authorization { + boolean login() default true; +} diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/DBInitialize.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/DBInitialize.java index 0851a9f5..6d8e3118 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/DBInitialize.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/DBInitialize.java @@ -26,6 +26,7 @@ import com.google.common.collect.Lists; import fun.asgc.neutrino.core.annotation.PreLoad; import fun.asgc.neutrino.core.db.template.JdbcTemplate; import fun.asgc.neutrino.core.util.*; +import fun.asgc.neutrino.proxy.server.base.rest.config.SqliteConfig; import lombok.extern.slf4j.Slf4j; import java.sql.DriverManager; @@ -97,6 +98,7 @@ public class DBInitialize { } sql += "\r\n" + line.trim(); if (sql.endsWith(";")) { + log.debug("初始化数据[table={}] sql:{}", tableName, sql); jdbcTemplate.update(sql); sql = ""; } diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ExceptionConstant.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ExceptionConstant.java new file mode 100644 index 00000000..3066c27f --- /dev/null +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ExceptionConstant.java @@ -0,0 +1,42 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.proxy.server.base.rest; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * + * @author: aoshiguchen + * @date: 2022/7/31 + */ +@Getter +@AllArgsConstructor +public enum ExceptionConstant { + SUCCESS(0, "成功"), + PARAMS_INVALID(1, "参数不正确"), + USER_NOT_LOGIN(2, "用户未登录"), + SYSTEM_ERROR(500, "系统异常"); + + private int code; + private String msg; +} diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ResponseBody.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ResponseBody.java new file mode 100644 index 00000000..f625d0fe --- /dev/null +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ResponseBody.java @@ -0,0 +1,39 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.proxy.server.base.rest; + +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * + * @author: aoshiguchen + * @date: 2022/7/31 + */ +@Accessors(chain = true) +@Data +public class ResponseBody { + private Integer code; + private String msg; + private T data; + private String stack; +} diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/WebConfigurerAdapter.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ServiceException.java similarity index 69% rename from neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/WebConfigurerAdapter.java rename to neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ServiceException.java index 42e1d47a..f55b1ba5 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/WebConfigurerAdapter.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/ServiceException.java @@ -21,22 +21,29 @@ */ package fun.asgc.neutrino.proxy.server.base.rest; -import fun.asgc.neutrino.core.annotation.Configuration; -import fun.asgc.neutrino.core.web.config.WebMvcConfigurer; -import fun.asgc.neutrino.core.web.interceptor.InterceptorRegistry; +import lombok.Getter; /** * * @author: aoshiguchen - * @date: 2022/7/30 + * @date: 2022/7/31 */ -@Configuration -public class WebConfigurerAdapter implements WebMvcConfigurer { +@Getter +public class ServiceException extends RuntimeException { + /** + * 错误码 + */ + private int code; + /** + * 异常消息 + */ + private String msg; - @Override - public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(new BaseAuthInterceptor()) - .addPathPatterns("/**").excludePathPatterns("/**/*.html", "/**/*.js", "/**/*.ico"); - registry.addInterceptor(new CorsInterceptor()); + public ServiceException(int code, String msg) { + this.code = code; + } + + public static ServiceException create(ExceptionConstant constant) { + return new ServiceException(constant.getCode(), constant.getMsg()); } } diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/SqliteConfig.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/config/SqliteConfig.java similarity index 96% rename from neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/SqliteConfig.java rename to neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/config/SqliteConfig.java index be34cdf6..20afa6ef 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/SqliteConfig.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/config/SqliteConfig.java @@ -19,7 +19,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package fun.asgc.neutrino.proxy.server.base.rest; +package fun.asgc.neutrino.proxy.server.base.rest.config; import fun.asgc.neutrino.core.annotation.Configuration; import fun.asgc.neutrino.core.annotation.NonIntercept; diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/config/WebConfigurerAdapter.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/config/WebConfigurerAdapter.java new file mode 100644 index 00000000..7bc79a53 --- /dev/null +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/config/WebConfigurerAdapter.java @@ -0,0 +1,58 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.proxy.server.base.rest.config; + +import fun.asgc.neutrino.core.annotation.Configuration; +import fun.asgc.neutrino.core.web.config.WebMvcConfigurer; +import fun.asgc.neutrino.core.web.interceptor.ExceptionHandlerRegistry; +import fun.asgc.neutrino.core.web.interceptor.InterceptorRegistry; +import fun.asgc.neutrino.core.web.interceptor.RestControllerAdviceHandler; +import fun.asgc.neutrino.proxy.server.base.rest.interceptor.BaseAuthInterceptor; +import fun.asgc.neutrino.proxy.server.base.rest.interceptor.CorsInterceptor; +import fun.asgc.neutrino.proxy.server.base.rest.interceptor.GlobalAdviceHandler; +import fun.asgc.neutrino.proxy.server.base.rest.interceptor.GlobalExceptionHandler; + +/** + * + * @author: aoshiguchen + * @date: 2022/7/30 + */ +@Configuration +public class WebConfigurerAdapter implements WebMvcConfigurer { + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new BaseAuthInterceptor()) + .addPathPatterns("/**").excludePathPatterns("/**/*.html", "/**/*.js", "/**/*.ico"); + registry.addInterceptor(new CorsInterceptor()); + } + + @Override + public RestControllerAdviceHandler adviceHandler() { + return new GlobalAdviceHandler(); + } + + @Override + public void addExceptionHandler(ExceptionHandlerRegistry registry) { + registry.addExceptionHandler(new GlobalExceptionHandler()); + } +} diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/BaseAuthInterceptor.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/interceptor/BaseAuthInterceptor.java similarity index 95% rename from neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/BaseAuthInterceptor.java rename to neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/interceptor/BaseAuthInterceptor.java index 8a0a8ee9..ba1ccf19 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/BaseAuthInterceptor.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/interceptor/BaseAuthInterceptor.java @@ -19,7 +19,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package fun.asgc.neutrino.proxy.server.base.rest; +package fun.asgc.neutrino.proxy.server.base.rest.interceptor; import fun.asgc.neutrino.core.web.context.HttpRequestWrapper; import fun.asgc.neutrino.core.web.context.HttpResponseWrapper; @@ -36,6 +36,7 @@ public class BaseAuthInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpRequestWrapper requestParser, HttpResponseWrapper responseWrapper, String route, Method targetMethod) throws Exception { + // TODO return true; } diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/CorsInterceptor.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/interceptor/CorsInterceptor.java similarity index 97% rename from neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/CorsInterceptor.java rename to neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/interceptor/CorsInterceptor.java index 7ad3cbbb..fee5778f 100644 --- a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/CorsInterceptor.java +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/interceptor/CorsInterceptor.java @@ -19,7 +19,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package fun.asgc.neutrino.proxy.server.base.rest; +package fun.asgc.neutrino.proxy.server.base.rest.interceptor; import fun.asgc.neutrino.core.web.context.HttpRequestWrapper; import fun.asgc.neutrino.core.web.context.HttpResponseWrapper; diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/interceptor/GlobalAdviceHandler.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/interceptor/GlobalAdviceHandler.java new file mode 100644 index 00000000..06ccd1e7 --- /dev/null +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/interceptor/GlobalAdviceHandler.java @@ -0,0 +1,48 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.proxy.server.base.rest.interceptor; + +import fun.asgc.neutrino.core.web.context.HttpRequestWrapper; +import fun.asgc.neutrino.core.web.context.HttpResponseWrapper; +import fun.asgc.neutrino.core.web.interceptor.RestControllerAdviceHandler; +import fun.asgc.neutrino.proxy.server.base.rest.ResponseBody; + +import java.lang.reflect.Method; + +/** + * 全局返回处理 + * @author: aoshiguchen + * @date: 2022/7/31 + */ +public class GlobalAdviceHandler implements RestControllerAdviceHandler { + + @Override + public Object advice(HttpRequestWrapper requestParser, HttpResponseWrapper responseWrapper, String route, Method targetMethod, Object res) { + if (res instanceof ResponseBody) { + return res; + } + return new ResponseBody<>() + .setCode(0) + .setData(res); + } + +} diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/interceptor/GlobalExceptionHandler.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/interceptor/GlobalExceptionHandler.java new file mode 100644 index 00000000..1af408c1 --- /dev/null +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/base/rest/interceptor/GlobalExceptionHandler.java @@ -0,0 +1,53 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.proxy.server.base.rest.interceptor; + +import fun.asgc.neutrino.core.web.context.HttpRequestWrapper; +import fun.asgc.neutrino.core.web.context.HttpResponseWrapper; +import fun.asgc.neutrino.core.web.interceptor.RestControllerExceptionHandler; +import fun.asgc.neutrino.proxy.server.base.rest.ExceptionConstant; +import fun.asgc.neutrino.proxy.server.base.rest.ResponseBody; +import fun.asgc.neutrino.proxy.server.base.rest.ServiceException; +import org.apache.commons.lang3.exception.ExceptionUtils; + +/** + * + * @author: aoshiguchen + * @date: 2022/7/31 + */ +public class GlobalExceptionHandler implements RestControllerExceptionHandler { + + @Override + public Object handle(HttpRequestWrapper requestParser, HttpResponseWrapper responseWrapper, Throwable e) { + if (e instanceof ServiceException) { + ServiceException serviceException = (ServiceException)e; + return new ResponseBody<>() + .setCode(serviceException.getCode()) + .setMsg(serviceException.getMsg()); + } + return new ResponseBody<>() + .setCode(ExceptionConstant.SYSTEM_ERROR.getCode()) + .setMsg(ExceptionConstant.SYSTEM_ERROR.getMsg()) + .setStack(ExceptionUtils.getStackTrace(e)); + } + +} diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/IndexController.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/IndexController.java new file mode 100644 index 00000000..393d6011 --- /dev/null +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/IndexController.java @@ -0,0 +1,55 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.proxy.server.controller; + +import fun.asgc.neutrino.core.annotation.NonIntercept; +import fun.asgc.neutrino.core.web.annotation.PostMapping; +import fun.asgc.neutrino.core.web.annotation.RequestBody; +import fun.asgc.neutrino.core.web.annotation.RequestMapping; +import fun.asgc.neutrino.core.web.annotation.RestController; +import fun.asgc.neutrino.proxy.server.base.rest.ExceptionConstant; +import fun.asgc.neutrino.proxy.server.base.rest.ServiceException; +import fun.asgc.neutrino.proxy.server.controller.req.LoginReq; +import fun.asgc.neutrino.proxy.server.controller.res.LoginRes; + +/** + * + * @author: aoshiguchen + * @date: 2022/7/31 + */ +@NonIntercept +@RequestMapping +@RestController +public class IndexController { + + @PostMapping("login") + public LoginRes login(@RequestBody LoginReq req) { + if (null == req) { + throw ServiceException.create(ExceptionConstant.PARAMS_INVALID); + } + return new LoginRes() + .setToken("1111") + .setUserId(1) + .setUserName("张三"); + } + +} diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/UserController.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/UserController.java new file mode 100644 index 00000000..549666a2 --- /dev/null +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/UserController.java @@ -0,0 +1,41 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.proxy.server.controller; + +import fun.asgc.neutrino.core.annotation.Autowired; +import fun.asgc.neutrino.core.annotation.NonIntercept; +import fun.asgc.neutrino.core.web.annotation.RequestMapping; +import fun.asgc.neutrino.core.web.annotation.RestController; +import fun.asgc.neutrino.proxy.server.service.UserService; + +/** + * 用户管理 + * @author: aoshiguchen + * @date: 2022/7/31 + */ +@NonIntercept +@RequestMapping("user") +@RestController +public class UserController { + @Autowired + private UserService userService; +} diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/req/LoginReq.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/req/LoginReq.java new file mode 100644 index 00000000..7ba3b461 --- /dev/null +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/req/LoginReq.java @@ -0,0 +1,41 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.proxy.server.controller.req; + +import lombok.Data; + +/** + * 登录请求参数 + * @author: aoshiguchen + * @date: 2022/7/31 + */ +@Data +public class LoginReq { + /** + * 登录名 + */ + private String loginName; + /** + * 登录密码 + */ + private String loginPassword; +} diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/res/LoginRes.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/res/LoginRes.java new file mode 100644 index 00000000..14ce115b --- /dev/null +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/controller/res/LoginRes.java @@ -0,0 +1,47 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.proxy.server.controller.res; + +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * 登录响应参数 + * @author: aoshiguchen + * @date: 2022/7/31 + */ +@Accessors(chain = true) +@Data +public class LoginRes { + /** + * token + */ + private String token; + /** + * 用户ID + */ + private Integer userId; + /** + * 用户名 + */ + private String userName; +} diff --git a/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/UserService.java b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/UserService.java new file mode 100644 index 00000000..42c28d48 --- /dev/null +++ b/neutrino-proxy-server/src/main/java/fun/asgc/neutrino/proxy/server/service/UserService.java @@ -0,0 +1,34 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.proxy.server.service; + +import fun.asgc.neutrino.core.annotation.Component; + +/** + * + * @author: aoshiguchen + * @date: 2022/7/31 + */ +@Component +public class UserService { + +} diff --git a/neutrino-proxy-server/src/main/resources/sql/user.data.sql b/neutrino-proxy-server/src/main/resources/sql/user.data.sql index e69de29b..12f63503 100644 --- a/neutrino-proxy-server/src/main/resources/sql/user.data.sql +++ b/neutrino-proxy-server/src/main/resources/sql/user.data.sql @@ -0,0 +1,3 @@ +#用户表 +insert into `user`(`id`, `name`,`login_name`,`login_password`,`create_time`, `update_time`) values +(1, '管理员', 'admin', '123456', datetime('now', 'localtime'), datetime('now', 'localtime'));