Commit 53fbd985c89fdbbedb4c1b8475f43d5d1bc5ecd0

Authored by YunaiV
1 parent bbe71ec2

引入 spring boot admin 监控

... ... @@ -50,6 +50,7 @@
50 50 <redisson.version>3.14.1</redisson.version>
51 51 <!-- 监控相关 -->
52 52 <skywalking.version>8.3.0</skywalking.version>
  53 + <spring-boot-admin.version>2.3.1</spring-boot-admin.version>
53 54 <!-- 工具类相关 -->
54 55 <lombok.version>1.16.14</lombok.version>
55 56 <mapstruct.version>1.4.1.Final</mapstruct.version>
... ... @@ -172,10 +173,10 @@
172 173 <version>${redisson.version}</version>
173 174 </dependency>
174 175  
175   - <!-- -->
  176 + <!-- Config 配置中心相关 -->
176 177 <dependency>
177 178 <groupId>com.ctrip.framework.apollo</groupId>
178   - <artifactId>apollo-client</artifactId>
  179 + <artifactId>apollo-client</artifactId> <!-- 引入 Apollo Client 库,实现内嵌的配置中心 -->
179 180 <version>1.7.0</version>
180 181 </dependency>
181 182  
... ... @@ -186,6 +187,17 @@
186 187 <version>${skywalking.version}</version>
187 188 </dependency>
188 189  
  190 + <dependency>
  191 + <groupId>de.codecentric</groupId>
  192 + <artifactId>spring-boot-admin-starter-server</artifactId> <!-- 实现 Spring Boot Admin Server 服务端 -->
  193 + <version>${spring-boot-admin.version}</version>
  194 + </dependency>
  195 + <dependency>
  196 + <groupId>de.codecentric</groupId>
  197 + <artifactId>spring-boot-admin-starter-client</artifactId> <!-- 实现 Spring Boot Admin Server 服务端 -->
  198 + <version>${spring-boot-admin.version}</version>
  199 + </dependency>
  200 +
189 201 <!-- 工具类相关 -->
190 202 <dependency>
191 203 <groupId>org.projectlombok</groupId>
... ...
ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/PermissionService.java deleted
1   -package com.ruoyi.framework.web.service;
2   -
3   -import java.util.Set;
4   -
5   -import org.springframework.beans.factory.annotation.Autowired;
6   -import org.springframework.stereotype.Service;
7   -import org.springframework.util.CollectionUtils;
8   -import com.ruoyi.common.core.domain.entity.SysRole;
9   -import com.ruoyi.common.core.domain.model.LoginUser;
10   -import com.ruoyi.common.utils.ServletUtils;
11   -import com.ruoyi.common.utils.StringUtils;
12   -
13   -/**
14   - * RuoYi首创 自定义权限实现,ss取自SpringSecurity首字母
15   - *
16   - * @author ruoyi
17   - */
18   -@Service("ss")
19   -public class PermissionService {
20   -
21   - /**
22   - * 判断用户是否拥有某个角色
23   - *
24   - * @param role 角色字符串
25   - * @return 用户是否具备某角色
26   - */
27   - public boolean hasRole(String role) {
28   - if (StringUtils.isEmpty(role)) {
29   - return false;
30   - }
31   - LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
32   - if (StringUtils.isNull(loginUser) || CollectionUtils.isEmpty(loginUser.getUser().getRoles())) {
33   - return false;
34   - }
35   - for (SysRole sysRole : loginUser.getUser().getRoles()) {
36   - String roleKey = sysRole.getRoleKey();
37   - if (SUPER_ADMIN.equals(roleKey) || roleKey.equals(StringUtils.trim(role))) {
38   - return true;
39   - }
40   - }
41   - return false;
42   - }
43   -
44   - /**
45   - * 验证用户是否具有以下任意一个角色
46   - *
47   - * @param roles 以 ROLE_NAMES_DELIMETER 为分隔符的角色列表
48   - * @return 用户是否具有以下任意一个角色
49   - */
50   - public boolean hasAnyRoles(String roles) {
51   - if (StringUtils.isEmpty(roles)) {
52   - return false;
53   - }
54   - LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
55   - if (StringUtils.isNull(loginUser) || CollectionUtils.isEmpty(loginUser.getUser().getRoles())) {
56   - return false;
57   - }
58   - for (String role : roles.split(ROLE_DELIMETER)) {
59   - if (hasRole(role)) {
60   - return true;
61   - }
62   - }
63   - return false;
64   - }
65   -
66   -}
src/main/java/cn/iocoder/dashboard/DashboardApplication.java
1 1 package cn.iocoder.dashboard;
2 2  
  3 +import de.codecentric.boot.admin.server.config.EnableAdminServer;
3 4 import org.springframework.boot.SpringApplication;
4 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
5 6  
6 7 @SpringBootApplication
  8 +@EnableAdminServer
7 9 public class DashboardApplication {
8 10  
9 11 public static void main(String[] args) {
... ...
src/main/java/cn/iocoder/dashboard/framework/security/config/SecurityConfiguration.java
... ... @@ -3,6 +3,7 @@ package cn.iocoder.dashboard.framework.security.config;
3 3 import cn.iocoder.dashboard.framework.security.core.filter.JwtAuthenticationTokenFilter;
4 4 import cn.iocoder.dashboard.framework.security.core.handler.LogoutSuccessHandlerImpl;
5 5 import cn.iocoder.dashboard.framework.web.config.WebProperties;
  6 +import de.codecentric.boot.admin.server.config.AdminServerProperties;
6 7 import org.springframework.boot.context.properties.EnableConfigurationProperties;
7 8 import org.springframework.context.annotation.Bean;
8 9 import org.springframework.http.HttpMethod;
... ... @@ -60,6 +61,8 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
60 61  
61 62 @Resource
62 63 private WebProperties webProperties;
  64 + @Resource
  65 + private AdminServerProperties adminServerProperties;
63 66  
64 67 /**
65 68 * 由于 Spring Security 创建 AuthenticationManager 对象时,没声明 @Bean 注解,导致无法被注入
... ... @@ -134,6 +137,13 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
134 137 .antMatchers("/swagger-resources/**").anonymous()
135 138 .antMatchers("/webjars/**").anonymous()
136 139 .antMatchers("/*/api-docs").anonymous()
  140 + // Spring Boot Admin Server 的安全配置
  141 + .antMatchers(adminServerProperties.getContextPath()).anonymous()
  142 + .antMatchers(adminServerProperties.getContextPath() + "/**").anonymous()
  143 + // Spring Boot Actuator 的安全配置
  144 + .antMatchers("/actuator").anonymous()
  145 + .antMatchers("/actuator/**").anonymous()
  146 + // TODO
137 147 .antMatchers("/druid/**").hasAnyAuthority("druid") // TODO 芋艿,未来需要在拓展下
138 148 // 除上面外的所有请求全部需要鉴权认证
139 149 .anyRequest().authenticated()
... ...
src/main/java/cn/iocoder/dashboard/framework/security/core/service/SecurityPermissionFrameworkService.java
1 1 package cn.iocoder.dashboard.framework.security.core.service;
2 2  
  3 +import cn.iocoder.dashboard.modules.system.dal.mysql.dataobject.permission.SysRoleDO;
  4 +
3 5 /**
4 6 * Security 框架 Permission Service 接口,定义 security 组件需要的功能
5 7 *
... ... @@ -23,4 +25,22 @@ public interface SecurityPermissionFrameworkService {
23 25 */
24 26 boolean hasAnyPermissions(String... permissions);
25 27  
  28 + /**
  29 + * 判断是否有角色
  30 + *
  31 + * 注意,角色使用的是 {@link SysRoleDO#getCode()} 标识
  32 + *
  33 + * @param role 角色
  34 + * @return 是否
  35 + */
  36 + boolean hasRole(String role);
  37 +
  38 + /**
  39 + * 判断是否有角色,任一一个即可
  40 + *
  41 + * @param roles 角色数组
  42 + * @return 是否
  43 + */
  44 + boolean hasAnyRoles(String... roles);
  45 +
26 46 }
... ...
src/main/java/cn/iocoder/dashboard/framework/web/config/WebConfiguration.java
1 1 package cn.iocoder.dashboard.framework.web.config;
2 2  
3   -import com.alibaba.fastjson.serializer.SerializerFeature;
4   -import com.alibaba.fastjson.support.config.FastJsonConfig;
5   -import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
6 3 import org.springframework.boot.context.properties.EnableConfigurationProperties;
7 4 import org.springframework.context.annotation.Bean;
8 5 import org.springframework.context.annotation.Configuration;
9 6 import org.springframework.core.annotation.Order;
10   -import org.springframework.http.MediaType;
11   -import org.springframework.http.converter.HttpMessageConverter;
12 7 import org.springframework.web.bind.annotation.RestController;
13 8 import org.springframework.web.cors.CorsConfiguration;
14 9 import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
... ... @@ -17,10 +12,6 @@ import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
17 12 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
18 13  
19 14 import javax.annotation.Resource;
20   -import java.nio.charset.Charset;
21   -import java.util.Collections;
22   -import java.util.List;
23   -import java.util.function.Predicate;
24 15  
25 16 /**
26 17 * Web 配置类
... ... @@ -41,21 +32,21 @@ public class WebConfiguration implements WebMvcConfigurer {
41 32  
42 33 // ========== MessageConverter 相关 ==========
43 34  
44   - @Override
45   - public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
46   - // 创建 FastJsonHttpMessageConverter 对象
47   - FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
48   - // 自定义 FastJson 配置
49   - FastJsonConfig fastJsonConfig = new FastJsonConfig();
50   - fastJsonConfig.setCharset(Charset.defaultCharset()); // 设置字符集
51   - fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect, // 剔除循环引用
52   - SerializerFeature.WriteNonStringKeyAsString); // 解决 Integer 作为 Key 时,转换为 String 类型,避免浏览器报错
53   - fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
54   - // 设置支持的 MediaType
55   - fastJsonHttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
56   - // 添加到 converters 中
57   - converters.add(0, fastJsonHttpMessageConverter); // 注意,添加到最开头,放在 MappingJackson2XmlHttpMessageConverter 前面
58   - }
  35 +// @Override
  36 +// public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  37 +// // 创建 FastJsonHttpMessageConverter 对象
  38 +// FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
  39 +// // 自定义 FastJson 配置
  40 +// FastJsonConfig fastJsonConfig = new FastJsonConfig();
  41 +// fastJsonConfig.setCharset(Charset.defaultCharset()); // 设置字符集
  42 +// fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect, // 剔除循环引用
  43 +// SerializerFeature.WriteNonStringKeyAsString); // 解决 Integer 作为 Key 时,转换为 String 类型,避免浏览器报错
  44 +// fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
  45 +// // 设置支持的 MediaType
  46 +// fastJsonHttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
  47 +// // 添加到 converters 中
  48 +// converters.add(0, fastJsonHttpMessageConverter); // 注意,添加到最开头,放在 MappingJackson2XmlHttpMessageConverter 前面
  49 +// }
59 50  
60 51 // ========== Filter 相关 ==========
61 52  
... ...
src/main/java/cn/iocoder/dashboard/modules/system/service/permission/impl/SysPermissionServiceImpl.java
... ... @@ -19,7 +19,9 @@ import cn.iocoder.dashboard.util.collection.CollectionUtils;
19 19 import cn.iocoder.dashboard.util.collection.MapUtils;
20 20 import com.google.common.collect.ImmutableMultimap;
21 21 import com.google.common.collect.Multimap;
  22 +import com.google.common.collect.Sets;
22 23 import lombok.extern.slf4j.Slf4j;
  24 +import org.springframework.scheduling.annotation.Scheduled;
23 25 import org.springframework.stereotype.Service;
24 26 import org.springframework.transaction.annotation.Transactional;
25 27 import org.springframework.transaction.support.TransactionSynchronization;
... ... @@ -104,6 +106,11 @@ public class SysPermissionServiceImpl implements SysPermissionService {
104 106 log.info("[initLocalCache][初始化角色与菜单的关联数量为 {}]", roleMenuList.size());
105 107 }
106 108  
  109 + @Scheduled(fixedDelay = SCHEDULER_PERIOD, initialDelay = SCHEDULER_PERIOD)
  110 + public void schedulePeriodicRefresh() {
  111 + initLocalCache();
  112 + }
  113 +
107 114 /**
108 115 * 如果角色与菜单的关联发生变化,从数据库中获取最新的全量角色与菜单的关联。
109 116 * 如果未发生变化,则返回空
... ... @@ -277,4 +284,30 @@ public class SysPermissionServiceImpl implements SysPermissionService {
277 284 });
278 285 }
279 286  
  287 + @Override
  288 + public boolean hasRole(String role) {
  289 + return hasAnyRoles(role);
  290 + }
  291 +
  292 + @Override
  293 + public boolean hasAnyRoles(String... roles) {
  294 + // 如果为空,说明已经有权限
  295 + if (ArrayUtil.isEmpty(roles)) {
  296 + return true;
  297 + }
  298 +
  299 + // 获得当前登陆的角色。如果为空,说明没有权限
  300 + Set<Long> roleIds = SecurityUtils.getLoginUserRoleIds();
  301 + if (CollUtil.isEmpty(roleIds)) {
  302 + return false;
  303 + }
  304 + // 判断是否是超管。如果是,当然符合条件
  305 + if (roleService.hasAnyAdmin(roleIds)) {
  306 + return true;
  307 + }
  308 + Set<String> userRoles = CollectionUtils.convertSet(roleService.listRolesFromCache(roleIds),
  309 + SysRoleDO::getCode);
  310 + return CollUtil.containsAny(userRoles, Sets.newHashSet(roles));
  311 + }
  312 +
280 313 }
... ...
src/main/resources/application.yaml
... ... @@ -23,6 +23,14 @@ spring:
23 23 multipart:
24 24 max-file-size: 16MB # 单个文件大小
25 25 max-request-size: 32MB # 设置总上传的文件大小
  26 + # Spring Boot Admin 配置项
  27 + boot:
  28 + admin:
  29 + # Spring Boot Admin Client 客户端的相关配置
  30 + client:
  31 + url: http://127.0.0.1:8080/${spring.boot.admin.context-path} # 设置 Spring Boot Admin Server 地址
  32 + # Spring Boot Admin Server 服务端的相关配置
  33 + context-path: /admin # 配置 Spring
26 34  
27 35 # 芋道配置项,设置当前项目所有自定义的配置
28 36 yudao:
... ... @@ -70,3 +78,10 @@ mybatis-plus:
70 78 logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
71 79 mapper-locations: classpath*:mapper/*.xml
72 80 type-aliases-package: cn.iocoder.dashboard.modules.*.dal.mysql.dataobject
  81 +
  82 +# Actuator 监控端点的配置项
  83 +management:
  84 + endpoints:
  85 + web:
  86 + exposure:
  87 + include: '*' # 需要开放的端点。默认值只打开 health 和 info 两个端点。通过设置 * ,可以开放所有端点。
... ...