From 5d949331cffd6cc1ab849b67b991ce835fd8a350 Mon Sep 17 00:00:00 2001 From: yihui Date: Mon, 29 Aug 2022 16:20:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=90=AF=E5=85=AC=E4=BC=97=E5=8F=B7?= =?UTF-8?q?=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 + ...11\350\243\205\347\216\257\345\242\203.md" | 205 ++++++++++++++++++ forum-core/pom.xml | 5 +- .../liuyueyi/forum/core/util/EnvUtil.java | 50 +++++ .../liuyueyi/forum/core/util/SpringUtil.java | 53 +++++ .../src/main/resources/static/js/login.js | 37 ++++ forum-ui/src/main/resources/static/js/mock.js | 19 ++ .../resources/templates/layout/navbar.html | 64 +----- .../forum/web/front/user/LoginController.java | 15 ++ .../interceptor/GlobalViewInterceptor.java | 4 + .../src/main/resources/logback-spring.xml | 1 + launch.sh | 6 +- 12 files changed, 400 insertions(+), 63 deletions(-) create mode 100644 "docs/\345\256\211\350\243\205\347\216\257\345\242\203.md" create mode 100644 forum-core/src/main/java/com/github/liuyueyi/forum/core/util/EnvUtil.java create mode 100644 forum-core/src/main/java/com/github/liuyueyi/forum/core/util/SpringUtil.java create mode 100644 forum-ui/src/main/resources/static/js/login.js create mode 100644 forum-ui/src/main/resources/static/js/mock.js diff --git a/README.md b/README.md index c0f98d09..e8e70cb6 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,10 @@ quick-forum - 创建数据库, 命名为 forum - 初始化表结构和demo数据, 可以直接导入 [test-data.sql](forum-web/src/main/resources/test-data.sql) +## 部署教程 + +- [环境搭建 & 基于源码的部署教程](docs/安装环境.md) + ## todo 1. 权限限制(包括菜单权限) diff --git "a/docs/\345\256\211\350\243\205\347\216\257\345\242\203.md" "b/docs/\345\256\211\350\243\205\347\216\257\345\242\203.md" new file mode 100644 index 00000000..5c90caf1 --- /dev/null +++ "b/docs/\345\256\211\350\243\205\347\216\257\345\242\203.md" @@ -0,0 +1,205 @@ +环境安装 +--- + +## jdk安装 + +```bash +# ubuntu +apt install openjdk-8-jdk + +# centos +yum install openjdk-8-jdk +``` + +安装完毕之后,执行 `java` , `javac`命令进行验证 + +## maven安装 + +```bash +cd ~ +mkdir soft +cd soft +wget https://dlcdn.apache.org/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz +tar -zxvf apache-maven-3.8.6-bin.tar.gz + +vim ~/.bashrc + +# 在最后添加环境变量 +export M2_HOME=/home/admin/soft/apache-maven-3.8.6 +PATH=$M2_HOME/bin:$PATH + +# 配置生效 +source ~/.bashrc +``` + +配置完成之后执行命令 `mvn --version` 进行验证 + +国内添加阿里的镜像源,加快下载速度 + +```bash +vim ~/soft/apache-maven-3.8.6/conf/settings.xml + +# 在标签中,添加下面的镜像源 + + + alimaven + aliyun-maven + http://maven.aliyun.com/nexus/content/groups/public/ + central + +``` + +## nginx配置 + +配置访问域名 + +```bash +cd /usr/local/nginx/conf/ + +vim nginx.conf + +# 添加子域名解析,每个域名一个独立的配置文件 +# 在http的一级标签中,添加如下一行配置,表示在conf.d文件下的所有conf结尾的文件,都属于我们需要使用的nginx配置信息 +include /usr/local/nginx/conf/conf.d/*.conf; +``` + +添加论坛的域名解析规则 + +```conf +vim conf.d/forum.conf + + +# 内容如下 +upstream forum_host { + server 127.0.0.1:8080; +} +server { + server_name forum.hhui.top; + + gzip on; + gzip_buffers 32 4K; + gzip_comp_level 6; + gzip_min_length 100; + gzip_types application/javascript text/css text/xml; + gzip_disable "MSIE [1-6]\."; #配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持) + gzip_vary on; + + location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ { + access_log off; + expires 1d; + proxy_pass http://forum_host; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + + location ~* ^.+\.(css|js|txt|xml|swf|wav|pptx)$ { + access_log off; + expires 10m; + proxy_pass http://forum_host; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + } + + location / { + proxy_set_header X-real-ip $remote_addr; + proxy_pass http://127.0.0.1:8080/; + proxy_redirect default; + } + + listen 443 ssl; # managed by Certbot + ssl_certificate /usr/local/nginx/conf/conf.d/cert.pem; + ssl_certificate_key /usr/local/nginx/conf/conf.d/key.pem; + ssl_stapling on; + ssl_stapling_verify on; + resolver 8.8.8.8 8.8.4.4 1.1.1.1 valid=60s; + resolver_timeout 2s; +} + + +server { + if ($host = forum.hhui.top) { + return 301 https://$host$request_uri; + } # managed by Certbot + + + listen 80; + server_name forum.hhui.top; + return 404; # managed by Certbot +} +``` + +证书使用let's encrypt生成 + +## 配置调整 + +线上部署时,选择prod环境,因此需要设置对应的数据库相关配置信息 + +resources-env/prod/application-dal.yml + +```yml +spring: + datasource: + url: jdbc:mysql://xxx/forum?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai + username: xxx + password: xxx +``` + +根据实际的情况进行修改 + +## 启动脚本 + +基于源码的部署脚本 + +```bash +#!/usr/bin/env bash + +WEB_PATH="forum-web" +JAR_NAME="forum-web-0.0.1-SNAPSHOT.jar" + +# 部署 +function start() { + git pull + + # 杀掉之前的进程 + cat pid.log| xargs -I {} kill {} + mv ${JAR_NAME} ${JAR_NAME}_bk + + mvn clean install -Dmaven.test.skip=True -Pprod + cd ${WEB_PATH} + mvn clean package spring-boot:repackage -Dmaven.test.skip=true -Pprod + cd - + + mv ${WEB_PATH}/target/${JAR_NAME} ./ + nohup java -server -Xms512m -Xmx512m -Xmn512m -XX:NativeMemoryTracking=detail -XX:-OmitStackTraceInFastThrow -jar ${JAR_NAME} > /dev/null 2>&1 & + echo $! 1> pid.log +} + +# 重启 +function restart() { + # 杀掉之前的进程 + cat pid.log| xargs -I {} kill {} + # 重新启动 + nohup java -server -Xms512m -Xmx512m -Xmn512m -XX:NativeMemoryTracking=detail -XX:-OmitStackTraceInFastThrow -jar ${JAR_NAME} > /dev/null 2>&1 & + echo $! 1> pid.log +} + +if [ $# == 0 ]; then + echo "miss command: start | restart" +elif [ $1 == 'start' ]; then + start +elif [ $1 == 'restart' ];then + restart +else + echo 'illegal command, support cmd: start | restart' +fi +``` + +启动命令 + +```bash +# 进入项目根目录,执行命令 +# chmod +x launch.sh # 若脚本没有执行权限,则取消这行命令的注释,用于添加执行权限 +./launch.sh start +``` \ No newline at end of file diff --git a/forum-core/pom.xml b/forum-core/pom.xml index b2f0cf00..2bef089d 100644 --- a/forum-core/pom.xml +++ b/forum-core/pom.xml @@ -21,7 +21,10 @@ com.github.liuyueyi.quick-forum form-api - + + org.springframework + spring-context + javax.servlet javax.servlet-api diff --git a/forum-core/src/main/java/com/github/liuyueyi/forum/core/util/EnvUtil.java b/forum-core/src/main/java/com/github/liuyueyi/forum/core/util/EnvUtil.java new file mode 100644 index 00000000..1e49c786 --- /dev/null +++ b/forum-core/src/main/java/com/github/liuyueyi/forum/core/util/EnvUtil.java @@ -0,0 +1,50 @@ +package com.github.liuyueyi.forum.core.util; + +import org.springframework.util.Assert; + +/** + * @author YiHui + * @date 2022/8/29 + */ +public class EnvUtil { + private static volatile EnvEnum env; + + public enum EnvEnum { + DEV("dev", false), + TEST("test", false), + PRE("pre", false), + PROD("prod", true); + private String env; + private boolean prod; + + EnvEnum(String env, boolean prod) { + this.env = env; + this.prod = prod; + } + + public static EnvEnum nameOf(String name) { + for (EnvEnum env : values()) { + if (env.env.equalsIgnoreCase(name)) { + return env; + } + } + return null; + } + } + + public static boolean isPro() { + return getEnv().prod; + } + + public static EnvEnum getEnv() { + if (env == null) { + synchronized (EnvUtil.class) { + if (env == null) { + env = EnvEnum.nameOf(SpringUtil.getConfig("env.name")); + } + } + } + Assert.isTrue(env != null, "env.name环境配置必然存在!"); + return env; + } +} diff --git a/forum-core/src/main/java/com/github/liuyueyi/forum/core/util/SpringUtil.java b/forum-core/src/main/java/com/github/liuyueyi/forum/core/util/SpringUtil.java new file mode 100644 index 00000000..9ea909b4 --- /dev/null +++ b/forum-core/src/main/java/com/github/liuyueyi/forum/core/util/SpringUtil.java @@ -0,0 +1,53 @@ +package com.github.liuyueyi.forum.core.util; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.EnvironmentAware; +import org.springframework.core.env.Environment; +import org.springframework.stereotype.Component; + +/** + * @author YiHui + * @date 2022/8/29 + */ +@Component +public class SpringUtil implements ApplicationContextAware, EnvironmentAware { + private static ApplicationContext context; + private static Environment environment; + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + SpringUtil.context = applicationContext; + } + + @Override + public void setEnvironment(Environment environment) { + SpringUtil.environment = environment; + } + + /** + * 获取bean + * + * @param bean + * @param + * @return + */ + public static T getBean(Class bean) { + return context.getBean(bean); + } + + public static Object getBean(String beanName) { + return context.getBean(beanName); + } + + /** + * 获取配置 + * + * @param key + * @return + */ + public static String getConfig(String key) { + return environment.getProperty(key); + } +} diff --git a/forum-ui/src/main/resources/static/js/login.js b/forum-ui/src/main/resources/static/js/login.js new file mode 100644 index 00000000..1ab68132 --- /dev/null +++ b/forum-ui/src/main/resources/static/js/login.js @@ -0,0 +1,37 @@ +$('#logoutBtn').click(function () { + $.ajax({ + url: "/logout", + dataType: "json", + type: "get", + success: function (data) { + toastr.success("已退出登录") + window.location.href = "/"; + } + }) +}) + +$('#loginBtn').click(function () { + const code = $('#loginCode').val(); + console.log("开始登录:" + code); + $.ajax({ + url: "/login?code=" + code, //请求的url地址 + dataType: "json", //返回格式为json + async: true,//请求是否异步,默认为异步,这也是ajax重要特性 + type: "GET", //请求方式 + success: function (data) { + //请求成功时处理 + console.log("response data:", data); + if (!data || !data.status || data.status.code !== 0) { + toastr.error(data.status.msg); + } else { + // 登录成功,刷新 + window.location.href = "/"; + toastr.success("登录成功"); + } + }, + error: function () { + //请求出错处理 + toastr.error("登录错误"); + } + }); +}); \ No newline at end of file diff --git a/forum-ui/src/main/resources/static/js/mock.js b/forum-ui/src/main/resources/static/js/mock.js new file mode 100644 index 00000000..6ebe3d14 --- /dev/null +++ b/forum-ui/src/main/resources/static/js/mock.js @@ -0,0 +1,19 @@ +$('#getToken').click(function () { + $.ajax({ + method: 'POST', + url: "/wx/callback", + contentType: 'application/xml', + data: "165570057911111111", + success: function (data) { + console.log("data", data); + if (!data) { + toastr.error(data.message); + } else { + $('#testOutput').html("" + $(data).find("Content").text() + ""); + } + }, + error: function (data) { + toastr.error(data); + } + }); +}) \ No newline at end of file diff --git a/forum-ui/src/main/resources/templates/layout/navbar.html b/forum-ui/src/main/resources/templates/layout/navbar.html index d1ad2a3b..482044fb 100644 --- a/forum-ui/src/main/resources/templates/layout/navbar.html +++ b/forum-ui/src/main/resources/templates/layout/navbar.html @@ -117,7 +117,8 @@
@@ -125,64 +126,7 @@ - + + \ No newline at end of file diff --git a/forum-web/src/main/java/com/github/liuyueyi/forum/web/front/user/LoginController.java b/forum-web/src/main/java/com/github/liuyueyi/forum/web/front/user/LoginController.java index 372d4621..0a106221 100644 --- a/forum-web/src/main/java/com/github/liuyueyi/forum/web/front/user/LoginController.java +++ b/forum-web/src/main/java/com/github/liuyueyi/forum/web/front/user/LoginController.java @@ -49,6 +49,21 @@ public ResVo logOut() { return ResVo.ok(true); } + /** + * 微信的公众号接入 token 验证,即返回echostr的参数值 + * + * @param request + * @return + */ + @GetMapping(path = "msg/callback") + public String check(HttpServletRequest request) { + String echoStr = request.getParameter("echostr"); + if (StringUtils.isNoneEmpty(echoStr)) { + return echoStr; + } + return ""; + } + /** * 微信的响应返回 * 本地测试访问: curl -X POST 'http://localhost:8080/wx/callback' -H 'content-type:application/xml' -d '165570057911111111' -i diff --git a/forum-web/src/main/java/com/github/liuyueyi/forum/web/hook/interceptor/GlobalViewInterceptor.java b/forum-web/src/main/java/com/github/liuyueyi/forum/web/hook/interceptor/GlobalViewInterceptor.java index fe3321d8..36331fd6 100644 --- a/forum-web/src/main/java/com/github/liuyueyi/forum/web/hook/interceptor/GlobalViewInterceptor.java +++ b/forum-web/src/main/java/com/github/liuyueyi/forum/web/hook/interceptor/GlobalViewInterceptor.java @@ -8,6 +8,7 @@ import lombok.Data; import lombok.experimental.Accessors; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.util.ObjectUtils; import org.springframework.web.method.HandlerMethod; @@ -33,6 +34,8 @@ public class GlobalViewInterceptor implements AsyncHandlerInterceptor { private GlobalViewConfig globalViewConfig; @Resource private UserService userService; + @Value("${env.name}") + private String env; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { @@ -63,6 +66,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { // 重定向请求不需要添加 if (!ObjectUtils.isEmpty(modelAndView)) { + modelAndView.getModel().put("env", env); modelAndView.getModel().put("siteInfo", globalViewConfig); if (ReqInfoContext.getReqInfo() == null || ReqInfoContext.getReqInfo().getUserId() == null) { modelAndView.getModel().put("isLogin", false); diff --git a/forum-web/src/main/resources/logback-spring.xml b/forum-web/src/main/resources/logback-spring.xml index ba0a33df..843a9d37 100644 --- a/forum-web/src/main/resources/logback-spring.xml +++ b/forum-web/src/main/resources/logback-spring.xml @@ -1,5 +1,6 @@ + diff --git a/launch.sh b/launch.sh index 06790193..d7cae0ac 100644 --- a/launch.sh +++ b/launch.sh @@ -33,7 +33,9 @@ function restart() { if [ $# == 0 ]; then echo "miss command: start | restart" elif [ $1 == 'start' ]; then - start + start elif [ $1 == 'restart' ];then - restart + restart +else + echo 'illegal command, support cmd: start | restart' fi \ No newline at end of file