From 1c11d7ccc125366ff12efa4770749e389aba01fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Fri, 9 Jul 2021 15:30:40 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E8=BD=AC=E7=A7=BB=20springboot-admin?= =?UTF-8?q?=20=E5=88=B0=E6=89=A9=E5=B1=95=E6=A8=A1=E5=9D=97=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 1 + .../src/main/resources/application.yml | 11 ++-- ruoyi-common/pom.xml | 4 -- ruoyi-extend/pom.xml | 18 ++++++ ruoyi-extend/ruoyi-monitor-admin/pom.xml | 33 ++++++++++ .../admin/MonitorAdminApplication.java | 19 ++++++ .../admin/config/AdminServerConfig.java | 31 +++++++++ .../monitor/admin/config/SecurityConfig.java | 48 ++++++++++++++ .../src/main/resources/application.yml | 11 ++++ .../framework/config/AdminServerConfig.java | 63 ------------------- .../framework/config/SecurityConfig.java | 7 --- ruoyi-ui/src/views/monitor/admin/index.vue | 2 +- 12 files changed, 168 insertions(+), 80 deletions(-) create mode 100644 ruoyi-extend/pom.xml create mode 100644 ruoyi-extend/ruoyi-monitor-admin/pom.xml create mode 100644 ruoyi-extend/ruoyi-monitor-admin/src/main/java/com/ruoyi/monitor/admin/MonitorAdminApplication.java create mode 100644 ruoyi-extend/ruoyi-monitor-admin/src/main/java/com/ruoyi/monitor/admin/config/AdminServerConfig.java create mode 100644 ruoyi-extend/ruoyi-monitor-admin/src/main/java/com/ruoyi/monitor/admin/config/SecurityConfig.java create mode 100644 ruoyi-extend/ruoyi-monitor-admin/src/main/resources/application.yml delete mode 100644 ruoyi-framework/src/main/java/com/ruoyi/framework/config/AdminServerConfig.java diff --git a/pom.xml b/pom.xml index d71181b1..34be51b0 100644 --- a/pom.xml +++ b/pom.xml @@ -198,6 +198,7 @@ ruoyi-generator ruoyi-common ruoyi-demo + ruoyi-extend pom diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index 4d48a976..3352d1d7 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -64,6 +64,8 @@ logging: # Spring配置 spring: + application: + name: ${ruoyi.name} # 资源信息 messages: # 国际化资源文件路径 @@ -317,18 +319,16 @@ spring: --- # 监控配置 spring: - application: - name: ruoyi-vue-plus boot: admin: # Spring Boot Admin Client 客户端的相关配置 client: # 设置 Spring Boot Admin Server 地址 - url: http://localhost:${server.port}${spring.boot.admin.context-path} + url: http://localhost:9090/admin instance: prefer-ip: true # 注册实例时,优先使用 IP - # Spring Boot Admin Server 服务端的相关配置 - context-path: /admin # 配置 Spring + username: ruoyi + password: 123456 # Actuator 监控端点的配置项 management: @@ -338,6 +338,7 @@ management: base-path: /actuator exposure: # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。 + # 生产环境不建议放开所有 根据项目需求放开即可 include: '*' endpoint: logfile: diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index aeafac81..47b9a55f 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -116,10 +116,6 @@ feign-okhttp - - de.codecentric - spring-boot-admin-starter-server - de.codecentric spring-boot-admin-starter-client diff --git a/ruoyi-extend/pom.xml b/ruoyi-extend/pom.xml new file mode 100644 index 00000000..99a2594e --- /dev/null +++ b/ruoyi-extend/pom.xml @@ -0,0 +1,18 @@ + + + + ruoyi-vue-plus + com.ruoyi + 2.4.0 + + 4.0.0 + ruoyi-extend + pom + + + ruoyi-monitor-admin + + + diff --git a/ruoyi-extend/ruoyi-monitor-admin/pom.xml b/ruoyi-extend/ruoyi-monitor-admin/pom.xml new file mode 100644 index 00000000..c6ef4850 --- /dev/null +++ b/ruoyi-extend/ruoyi-monitor-admin/pom.xml @@ -0,0 +1,33 @@ + + + + ruoyi-extend + com.ruoyi + 2.4.0 + + 4.0.0 + jar + ruoyi-monitor-admin + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-security + + + + de.codecentric + spring-boot-admin-starter-server + + + + diff --git a/ruoyi-extend/ruoyi-monitor-admin/src/main/java/com/ruoyi/monitor/admin/MonitorAdminApplication.java b/ruoyi-extend/ruoyi-monitor-admin/src/main/java/com/ruoyi/monitor/admin/MonitorAdminApplication.java new file mode 100644 index 00000000..1d1cbca5 --- /dev/null +++ b/ruoyi-extend/ruoyi-monitor-admin/src/main/java/com/ruoyi/monitor/admin/MonitorAdminApplication.java @@ -0,0 +1,19 @@ +package com.ruoyi.monitor.admin; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Admin 监控启动程序 + * + * @author Lion Li + */ +@SpringBootApplication +public class MonitorAdminApplication { + + public static void main(String[] args) { + SpringApplication.run(MonitorAdminApplication.class, args); + System.out.println("Admin 监控启动成功" ); + } + +} diff --git a/ruoyi-extend/ruoyi-monitor-admin/src/main/java/com/ruoyi/monitor/admin/config/AdminServerConfig.java b/ruoyi-extend/ruoyi-monitor-admin/src/main/java/com/ruoyi/monitor/admin/config/AdminServerConfig.java new file mode 100644 index 00000000..e2a9c51d --- /dev/null +++ b/ruoyi-extend/ruoyi-monitor-admin/src/main/java/com/ruoyi/monitor/admin/config/AdminServerConfig.java @@ -0,0 +1,31 @@ +package com.ruoyi.monitor.admin.config; + +import de.codecentric.boot.admin.server.config.EnableAdminServer; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration; +import org.springframework.boot.task.TaskExecutorBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.util.concurrent.Executor; + +/** + * springboot-admin server配置类 + * + * @author Lion Li + */ +@Configuration +@EnableAdminServer +public class AdminServerConfig { + + @Lazy + @Bean(name = TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME) + @ConditionalOnMissingBean(Executor.class) + public ThreadPoolTaskExecutor applicationTaskExecutor(TaskExecutorBuilder builder) { + return builder.build(); + } + + +} diff --git a/ruoyi-extend/ruoyi-monitor-admin/src/main/java/com/ruoyi/monitor/admin/config/SecurityConfig.java b/ruoyi-extend/ruoyi-monitor-admin/src/main/java/com/ruoyi/monitor/admin/config/SecurityConfig.java new file mode 100644 index 00000000..98834a9b --- /dev/null +++ b/ruoyi-extend/ruoyi-monitor-admin/src/main/java/com/ruoyi/monitor/admin/config/SecurityConfig.java @@ -0,0 +1,48 @@ +package com.ruoyi.monitor.admin.config; + +import de.codecentric.boot.admin.server.config.AdminServerProperties; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; + +/** + * spring security配置 + * + * @author ruoyi + */ +@Configuration +@EnableWebSecurity +@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, proxyTargetClass = true) +public class SecurityConfig extends WebSecurityConfigurerAdapter { + + private final String adminContextPath; + + public SecurityConfig(AdminServerProperties adminServerProperties) { + this.adminContextPath = adminServerProperties.getContextPath(); + } + + @Override + protected void configure(HttpSecurity httpSecurity) throws Exception { + SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); + successHandler.setTargetUrlParameter("redirectTo"); + successHandler.setDefaultTargetUrl(adminContextPath + "/"); + + httpSecurity.authorizeRequests() + //授予对所有静态资产和登录页面的公共访问权限。 + .antMatchers(adminContextPath + "/assets/**").permitAll() + .antMatchers(adminContextPath + "/login").permitAll() + //必须对每个其他请求进行身份验证 + .anyRequest().authenticated().and() + //配置登录和注销 + .formLogin().loginPage(adminContextPath + "/login") + .successHandler(successHandler).and() + .logout().logoutUrl(adminContextPath + "/logout").and() + //启用HTTP-Basic支持。这是Spring Boot Admin Client注册所必需的 + .httpBasic().and().csrf().disable() + .headers().frameOptions().disable(); + } + +} diff --git a/ruoyi-extend/ruoyi-monitor-admin/src/main/resources/application.yml b/ruoyi-extend/ruoyi-monitor-admin/src/main/resources/application.yml new file mode 100644 index 00000000..631f3e77 --- /dev/null +++ b/ruoyi-extend/ruoyi-monitor-admin/src/main/resources/application.yml @@ -0,0 +1,11 @@ +server: + port: 9090 + +spring: + security: + user: + name: ruoyi + password: 123456 + boot: + admin: + context-path: /admin diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/AdminServerConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/AdminServerConfig.java deleted file mode 100644 index 59fed507..00000000 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/AdminServerConfig.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.ruoyi.framework.config; - -import de.codecentric.boot.admin.server.config.EnableAdminServer; -import org.springframework.beans.factory.ObjectProvider; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration; -import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties; -import org.springframework.boot.task.TaskExecutorBuilder; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Lazy; -import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; -import org.thymeleaf.dialect.IDialect; -import org.thymeleaf.spring5.ISpringTemplateEngine; -import org.thymeleaf.spring5.SpringTemplateEngine; -import org.thymeleaf.templateresolver.ITemplateResolver; - -import java.util.Comparator; -import java.util.LinkedHashSet; -import java.util.Set; -import java.util.concurrent.Executor; -import java.util.stream.Collectors; - -/** - * springboot-admin server配置类 - * - * @author Lion Li - */ -@Configuration -@EnableAdminServer -public class AdminServerConfig { - - @Lazy - @Bean(name = TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME) - @ConditionalOnMissingBean(Executor.class) - public ThreadPoolTaskExecutor applicationTaskExecutor(TaskExecutorBuilder builder) { - return builder.build(); - } - - /** - * 解决 admin 与 项目 页面的交叉引用 将 admin 的路由放到最后 - * @param properties - * @param templateResolvers - * @param dialects - * @return - */ - @Bean - @ConditionalOnMissingBean(ISpringTemplateEngine.class) - SpringTemplateEngine templateEngine(ThymeleafProperties properties, - ObjectProvider templateResolvers, ObjectProvider dialects) { - SpringTemplateEngine engine = new SpringTemplateEngine(); - engine.setEnableSpringELCompiler(properties.isEnableSpringElCompiler()); - engine.setRenderHiddenMarkersBeforeCheckboxes(properties.isRenderHiddenMarkersBeforeCheckboxes()); - templateResolvers.orderedStream().forEach(engine::addTemplateResolver); - dialects.orderedStream().forEach(engine::addDialect); - Set templateResolvers1 = engine.getTemplateResolvers(); - templateResolvers1 = templateResolvers1.stream() - .sorted(Comparator.comparing(ITemplateResolver::getOrder)) - .collect(Collectors.toCollection(LinkedHashSet::new)); - engine.setTemplateResolvers(templateResolvers1); - return engine; - } -} diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java index 79ade787..920aa888 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/SecurityConfig.java @@ -3,7 +3,6 @@ package com.ruoyi.framework.config; import com.ruoyi.framework.security.filter.JwtAuthenticationTokenFilter; import com.ruoyi.framework.security.handle.AuthenticationEntryPointImpl; import com.ruoyi.framework.security.handle.LogoutSuccessHandlerImpl; -import de.codecentric.boot.admin.server.config.AdminServerProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.http.HttpMethod; @@ -57,9 +56,6 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter @Autowired private CorsFilter corsFilter; - @Autowired - private AdminServerProperties adminServerProperties; - /** * 解决 无法直接注入 AuthenticationManager * @@ -117,9 +113,6 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter .antMatchers("/webjars/**").anonymous() .antMatchers("/*/api-docs").anonymous() .antMatchers("/druid/**").anonymous() - // Spring Boot Admin Server 的安全配置 - .antMatchers(adminServerProperties.getContextPath()).anonymous() - .antMatchers(adminServerProperties.getContextPath() + "/**").anonymous() // Spring Boot Actuator 的安全配置 .antMatchers("/actuator").anonymous() .antMatchers("/actuator/**").anonymous() diff --git a/ruoyi-ui/src/views/monitor/admin/index.vue b/ruoyi-ui/src/views/monitor/admin/index.vue index f1d48b0a..012d92c5 100644 --- a/ruoyi-ui/src/views/monitor/admin/index.vue +++ b/ruoyi-ui/src/views/monitor/admin/index.vue @@ -8,7 +8,7 @@ export default { name: "Admin", data() { return { - src: process.env.VUE_APP_BASE_API + "/admin", + src: "http://localhost:9090/admin/login", height: document.documentElement.clientHeight - 94.5 + "px;", loading: true };