Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持前后端统一打包 #54

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ruoyi-admin/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM openjdk:8-jdk
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ADD target/ruoyi-admin.jar /ruoyi-admin.jar
EXPOSE 8080
ENTRYPOINT ["java","-XX:+UseContainerSupport","-XX:InitialRAMPercentage=50.0","-XX:MaxRAMPercentage=50.0","-Duser.timezone=Asia/Shanghai","-jar","/ruoyi-admin.jar"]
81 changes: 75 additions & 6 deletions ruoyi-admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
web服务入口
</description>

<properties>
<frontend-maven-plugin.version>1.12.1</frontend-maven-plugin.version>
<ui.path>${project.parent.basedir}/ruoyi-ui</ui.path>
</properties>

<dependencies>

<!-- spring-boot-devtools -->
Expand Down Expand Up @@ -63,6 +68,70 @@

</dependencies>

<profiles>
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>${frontend-maven-plugin.version}</version>
<configuration>
<workingDirectory>${ui.path}</workingDirectory>
<nodeVersion>v12.22.12</nodeVersion>
</configuration>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install --registry=https://registry.npmmirror.com</arguments>
</configuration>
</execution>
<execution>
<id>npm run build:prod</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>run build:prod</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
<outputDirectory>${project.build.directory}</outputDirectory>
<resources>
<resource>
<targetPath>${basedir}/target/classes/public</targetPath>
<directory>${ui.path}/dist</directory>
</resource>
<resource>
<targetPath>${basedir}/target/classes</targetPath>
<directory>src/main/resources</directory>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>

</profile>
</profiles>

<build>
<plugins>
<plugin>
Expand All @@ -80,15 +149,15 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>${project.artifactId}</warName>
</configuration>
</plugin>
</configuration>
</plugin>
</plugins>
<finalName>${project.artifactId}</finalName>
</build>
Expand Down
65 changes: 65 additions & 0 deletions ruoyi-admin/src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
ruoyi:
pathPrefix: /prod-api
frontendPrefix:
- /index
- /login
- /system
- /monitor
- /tool
# 数据源配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
druid:
# 主库数据源
master:
url: jdbc:mysql://${mysql.host:localhost:3306}/ry-vue?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: ${mysql.username:root}
password: ${mysql.password:123456}
# 从库数据源
slave:
# 从数据源开关/默认关闭
enabled: false
url:
username:
password:
# 初始连接数
initialSize: 5
# 最小连接池数量
minIdle: 10
# 最大连接池数量
maxActive: 20
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
# 配置一个连接在池中最大生存的时间,单位是毫秒
maxEvictableIdleTimeMillis: 900000
# 配置检测连接是否有效
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
webStatFilter:
enabled: true
statViewServlet:
enabled: true
# 设置白名单,不填则允许所有访问
allow:
url-pattern: /druid/*
# 控制台管理用户名和密码
login-username: ruoyi
login-password: 123456
filter:
stat:
enabled: true
# 慢SQL记录
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: true
wall:
config:
multi-statement-allow: true
2 changes: 1 addition & 1 deletion ruoyi-admin/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- 日志存放路径 -->
<property name="log.path" value="/home/ruoyi/logs" />
<property name="log.path" value="/opt/logs" />
<!-- 日志输出格式 -->
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n" />

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.ruoyi.common.config;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;

/**
* 读取项目相关配置
*
Expand Down Expand Up @@ -32,9 +35,17 @@ public class RuoYiConfig

/** 验证码类型 */
private static String captchaType;
/**
* 后端接口前缀
*/
private static String pathPrefix;

public String getName()
{
/**
* 前端前缀
*/
private static List<String> frontendPrefix;

public String getName() {
return name;
}

Expand Down Expand Up @@ -101,11 +112,27 @@ public void setCaptchaType(String captchaType) {
RuoYiConfig.captchaType = captchaType;
}


public static String getPathPrefix() {
return StringUtils.nvl(pathPrefix, "");
}

public void setPathPrefix(String pathPrefix) {
RuoYiConfig.pathPrefix = pathPrefix;
}

public static List<String> getFrontendPrefix() {
return frontendPrefix;
}

public void setFrontendPrefix(List<String> frontendPrefix) {
RuoYiConfig.frontendPrefix = frontendPrefix;
}

/**
* 获取导入上传路径
*/
public static String getImportPath()
{
public static String getImportPath() {
return getProfile() + "/import";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.ruoyi.framework.config;

import com.ruoyi.common.config.RuoYiConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
* mvc配置
*
* @author nikyotensai
* @since 2023-02-04
*/
@Configuration
public class MvcConfig implements WebMvcConfigurer {

@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.addPathPrefix(RuoYiConfig.getPathPrefix(), c -> c.isAnnotationPresent(RestController.class));
}

}
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package com.ruoyi.framework.config;

import java.util.concurrent.TimeUnit;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.interceptor.RepeatSubmitInterceptor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.framework.interceptor.RepeatSubmitInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
* 通用配置
Expand Down Expand Up @@ -46,6 +52,30 @@ public void addResourceHandlers(ResourceHandlerRegistry registry)
public void addInterceptors(InterceptorRegistry registry)
{
registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
addUiInterceptor(registry);
}


private void addUiInterceptor(InterceptorRegistry registry) {
if (StringUtils.isEmpty(RuoYiConfig.getPathPrefix()) || CollectionUtils.isEmpty(RuoYiConfig.getFrontendPrefix())) {
return;
}
registry.addInterceptor(new HandlerInterceptor() {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String servletPath = request.getServletPath();
if (servletPath.equals("/")) {
request.getRequestDispatcher("/index.html").forward(request, response);
}
for (String prefix : RuoYiConfig.getFrontendPrefix()) {
if (request.getServletPath().startsWith(prefix) && !request.getServletPath().equals("/index.html")) {
request.getRequestDispatcher("/index.html").forward(request, response);
break;
}
}
return true;
}
}).addPathPatterns("/**");
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.ruoyi.framework.config;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.framework.config.properties.PermitAllUrlProperties;
import com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter;
import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl;
import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
Expand All @@ -15,10 +20,6 @@
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.logout.LogoutFilter;
import org.springframework.web.filter.CorsFilter;
import com.ruoyi.framework.config.properties.PermitAllUrlProperties;
import com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter;
import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl;
import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl;

/**
* spring security配置
Expand Down Expand Up @@ -111,16 +112,16 @@ protected void configure(HttpSecurity httpSecurity) throws Exception
// 过滤请求
.authorizeRequests()
// 对于登录login 注册register 验证码captchaImage 允许匿名访问
.antMatchers("/login", "/register", "/captchaImage").permitAll()
.antMatchers(RuoYiConfig.getPathPrefix()+"/login", RuoYiConfig.getPathPrefix()+"/register", RuoYiConfig.getPathPrefix()+"/captchaImage").permitAll()
// 静态资源,可匿名访问
.antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()
// 除上面外的所有请求全部需要鉴权认证
.anyRequest().authenticated()
.antMatchers(RuoYiConfig.getPathPrefix()+"/**").authenticated()
.and()
.headers().frameOptions().disable();
// 添加Logout filter
httpSecurity.logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler);
httpSecurity.logout().logoutUrl(RuoYiConfig.getPathPrefix()+"/logout").logoutSuccessHandler(logoutSuccessHandler);
// 添加JWT filter
httpSecurity.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
// 添加CORS filter
Expand Down