From 74355e14088df2c60da6e9a2b026fa0513709de9 Mon Sep 17 00:00:00 2001 From: yangys <y_ys79@sina.com> Date: 星期一, 17 二月 2025 14:19:45 +0800 Subject: [PATCH] 增加授权认证。 --- smart-man-boot/src/main/java/com/qianwen/smartman/common/config/InterceptorConfig.java | 19 +++ smart-man-boot/pom.xml | 24 +++- smart-man-boot/src/main/java/com/qianwen/smartman/common/license/LicenseWrapper.java | 27 +++++ smart-man-boot/src/main/java/com/qianwen/smartman/modules/workinghour/controller/PartWorkHourController.java | 2 smart-man-boot/src/main/java/com/qianwen/smartman/common/runner/InitRunner.java | 65 +++++++++++++ smart-man-boot/src/main/java/com/qianwen/smartman/modules/smis/service/impl/WorkstationServiceImpl.java | 22 ++++ smart-man-boot/src/main/resources/application.yml | 43 ++------ smart-man-boot/src/main/java/com/qianwen/smartman/modules/workinghour/controller/LicenseTestController.java | 40 ++++++++ smart-man-boot/src/main/java/com/qianwen/smartman/common/interceptor/LicenseCheckInterceptor.java | 39 +++++++ 9 files changed, 240 insertions(+), 41 deletions(-) diff --git a/smart-man-boot/pom.xml b/smart-man-boot/pom.xml index 5b64850..d3612b7 100644 --- a/smart-man-boot/pom.xml +++ b/smart-man-boot/pom.xml @@ -134,16 +134,7 @@ <artifactId>smart-starter-social</artifactId> <version>1.0.0</version> </dependency> - <!-- - - --> - - <!-- - - - - --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-ui</artifactId> @@ -397,6 +388,13 @@ <!--<version>1.1.0-SNAPSHOT</version>--> <version>1.0.4</version> </dependency> + <!--鍗冩枃瀵嗛挜璁よ瘉--> + <dependency> + <groupId>com.qianwen</groupId> + <artifactId>license-common</artifactId> + <version>1.0.0</version> + </dependency> + <!-- 鍥剧墖鍘嬬缉 --> <dependency> <groupId>net.coobird</groupId> @@ -511,6 +509,14 @@ <artifactId>jfreechart</artifactId> <version>1.5.5</version> </dependency> + + <!--lincense楠岃瘉--> + <dependency> + <groupId>de.schlichtherle.truelicense</groupId> + <artifactId>truelicense-core</artifactId> + <version>1.33</version> + </dependency> + </dependencies> <build> diff --git a/smart-man-boot/src/main/java/com/qianwen/smartman/common/config/InterceptorConfig.java b/smart-man-boot/src/main/java/com/qianwen/smartman/common/config/InterceptorConfig.java new file mode 100644 index 0000000..5fe05bd --- /dev/null +++ b/smart-man-boot/src/main/java/com/qianwen/smartman/common/config/InterceptorConfig.java @@ -0,0 +1,19 @@ +package com.qianwen.smartman.common.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import com.qianwen.smartman.common.interceptor.LicenseCheckInterceptor; + +@Configuration +public class InterceptorConfig implements WebMvcConfigurer{ + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new LicenseCheckInterceptor()); + } + + + +} diff --git a/smart-man-boot/src/main/java/com/qianwen/smartman/common/interceptor/LicenseCheckInterceptor.java b/smart-man-boot/src/main/java/com/qianwen/smartman/common/interceptor/LicenseCheckInterceptor.java new file mode 100644 index 0000000..ef94901 --- /dev/null +++ b/smart-man-boot/src/main/java/com/qianwen/smartman/common/interceptor/LicenseCheckInterceptor.java @@ -0,0 +1,39 @@ +package com.qianwen.smartman.common.interceptor; + +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; + +import com.alibaba.fastjson.JSON; +import com.qianwen.license.common.LicenseVerify; + +/** + * license鏍¢獙鎷︽埅 + */ +public class LicenseCheckInterceptor extends HandlerInterceptorAdapter{ + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + LicenseVerify licenseVerify = new LicenseVerify(); + //鏍¢獙璇佷功鏄惁鏈夋晥 + boolean verifyResult = licenseVerify.verify(); + + if(verifyResult){ + return true; + }else{ + response.setContentType("application/json;charset=utf8"); + response.setCharacterEncoding("utf-8"); + Map<String,String> result = new HashMap<>(1); + result.put("success", "false"); + result.put("msg","鎮ㄧ殑璇佷功鏃犳晥锛岃鏍告煡鏈嶅姟鍣ㄦ槸鍚﹀彇寰楁巿鏉冩垨閲嶆柊鐢宠璇佷功锛�"); + + response.getWriter().write(JSON.toJSONString(result));//throw new ServiceException("鎮ㄧ殑璇佷功鏃犳晥锛岃鏍告煡鏈嶅姟鍣ㄦ槸鍚﹀彇寰楁巿鏉冩垨閲嶆柊鐢宠璇佷功锛�"); + + return false; + } + } +} diff --git a/smart-man-boot/src/main/java/com/qianwen/smartman/common/license/LicenseWrapper.java b/smart-man-boot/src/main/java/com/qianwen/smartman/common/license/LicenseWrapper.java new file mode 100644 index 0000000..81e56cb --- /dev/null +++ b/smart-man-boot/src/main/java/com/qianwen/smartman/common/license/LicenseWrapper.java @@ -0,0 +1,27 @@ +package com.qianwen.smartman.common.license; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.JSONValidator; +import com.qianwen.license.common.LicenseExtraModel; +import com.qianwen.license.common.LicenseVerify; + +@Component +public class LicenseWrapper { + //@Autowired + //private LicenseVerify licenseVerify; + + public long getWorkstationAmount() { + LicenseVerify licenseVerify = new LicenseVerify(); + LicenseExtraModel model = (LicenseExtraModel)licenseVerify.getContent().getExtra(); + + if(JSONValidator.from(model.getExtData()).validate()) { + JSONObject jsonObj = JSONObject.parseObject(model.getExtData()) ; + return jsonObj.getLong("workstationAmount"); + }else { + return 0; + } + } +} diff --git a/smart-man-boot/src/main/java/com/qianwen/smartman/common/runner/InitRunner.java b/smart-man-boot/src/main/java/com/qianwen/smartman/common/runner/InitRunner.java new file mode 100644 index 0000000..159e1f9 --- /dev/null +++ b/smart-man-boot/src/main/java/com/qianwen/smartman/common/runner/InitRunner.java @@ -0,0 +1,65 @@ +package com.qianwen.smartman.common.runner; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.stereotype.Component; + +import com.qianwen.license.common.LicenseVerify; +import com.qianwen.license.common.LicenseVerifyParam; + +@Component +public class InitRunner implements ApplicationRunner { + private static final Logger log = LoggerFactory.getLogger(InitRunner.class); + @Value("${license.subject}") + private String subject; + + /** + * 鍏挜鍒О + */ + @Value("${license.publicAlias}") + private String publicAlias; + + /** + * 璁块棶鍏挜搴撶殑瀵嗙爜 + */ + @Value("${license.storePass}") + private String storePass; + + /** + * 璇佷功鐢熸垚璺緞 + */ + @Value("${license.licensePath}") + private String licensePath; + + /** + * 瀵嗛挜搴撳瓨鍌ㄨ矾寰� + */ + @Value("${license.publicKeysStorePath}") + private String publicKeysStorePath; + + @Override + public void run(ApplicationArguments args) throws Exception { + + + //瀹夎璇佷功 + initLicense(); + } + + void initLicense() { + LicenseVerifyParam param = new LicenseVerifyParam(); + param.setSubject(subject); + param.setPublicAlias(publicAlias); + param.setStorePass(storePass); + param.setLicensePath(licensePath); + param.setPublicKeysStorePath(publicKeysStorePath); + + LicenseVerify licenseVerify = new LicenseVerify(); + //瀹夎璇佷功 + licenseVerify.install(param); + + log.info("++++++++ 璇佷功瀹夎缁撴潫 ++++++++"); + } +} diff --git a/smart-man-boot/src/main/java/com/qianwen/smartman/modules/smis/service/impl/WorkstationServiceImpl.java b/smart-man-boot/src/main/java/com/qianwen/smartman/modules/smis/service/impl/WorkstationServiceImpl.java index bfa8b73..6ad77b4 100644 --- a/smart-man-boot/src/main/java/com/qianwen/smartman/modules/smis/service/impl/WorkstationServiceImpl.java +++ b/smart-man-boot/src/main/java/com/qianwen/smartman/modules/smis/service/impl/WorkstationServiceImpl.java @@ -29,6 +29,7 @@ import com.alibaba.excel.write.merge.AbstractMergeStrategy; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; @@ -42,10 +43,11 @@ import com.qianwen.core.mp.support.Query; import com.qianwen.core.oss.model.BladeFile; import com.qianwen.core.secure.utils.AuthUtil; -import com.qianwen.core.tool.utils.CollectionUtil; import com.qianwen.core.tool.utils.DateUtil; import com.qianwen.core.tool.utils.Func; import com.qianwen.core.tool.utils.StringUtil; +import com.qianwen.license.common.LicenseExtraModel; +import com.qianwen.license.common.LicenseVerify; import com.qianwen.smartman.common.cache.DictCache; import com.qianwen.smartman.common.cache.cps.WorkstationCache; import com.qianwen.smartman.common.constant.CalendarConstant; @@ -59,6 +61,7 @@ import com.qianwen.smartman.common.enums.DictEnum; import com.qianwen.smartman.common.enums.StatusType; import com.qianwen.smartman.common.enums.WcsDataTypeEnums; +import com.qianwen.smartman.common.license.LicenseWrapper; import com.qianwen.smartman.common.mqtt.MqttMessageSender; import com.qianwen.smartman.common.utils.Lambda; import com.qianwen.smartman.common.utils.MessageUtils; @@ -147,6 +150,8 @@ private MqttMessageSender mqttMsgSender; @Autowired private WorkstationDatapointsService wsDpService; + @Autowired + private LicenseWrapper licenseWrapper; private final String NAME = "榛樿宸ヤ綔鍙�"; private final Integer SORT = 1; @@ -186,6 +191,9 @@ @Override @Transactional(rollbackFor = {Exception.class}) public WorkstationVO submit(WorkstationSubmitVO workstationSubmitVO) { + + checkLicenseCount(); + checkWorkstation(workstationSubmitVO); if (Func.isEmpty(workstationSubmitVO.getId())) { checkUnableWorkstation(workstationSubmitVO); @@ -229,6 +237,18 @@ } return workstationVO; } + void checkLicenseCount() { + + + Wrapper<Workstation> wrapper = Wrappers.lambdaQuery(Workstation.class).eq(Workstation::getIsDeleted, 0); + Long count = workstationMapper.selectCount(wrapper); + + + Long machineAmount = licenseWrapper.getWorkstationAmount(); + if(count >= machineAmount) { + throw new ServiceException("宸ヤ綅鏁拌秴杩囪鍙笂闄愭暟:"+ machineAmount); + } + } /* private void submitWorkstationFtpDirectory(WorkstationSubmitVO workstationSubmitVO, Workstation workstation) { if (Func.isEmpty(workstationSubmitVO.getId()) && Func.isNotEmpty(workstationSubmitVO.getFtpDirectoryType())) { diff --git a/smart-man-boot/src/main/java/com/qianwen/smartman/modules/workinghour/controller/LicenseTestController.java b/smart-man-boot/src/main/java/com/qianwen/smartman/modules/workinghour/controller/LicenseTestController.java new file mode 100644 index 0000000..fa3ca3a --- /dev/null +++ b/smart-man-boot/src/main/java/com/qianwen/smartman/modules/workinghour/controller/LicenseTestController.java @@ -0,0 +1,40 @@ +package com.qianwen.smartman.modules.workinghour.controller; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.qianwen.core.boot.ctrl.BladeController; +import com.qianwen.core.scanner.modular.stereotype.ApiResource; +import com.qianwen.core.tenant.annotation.NonDS; +import com.qianwen.core.tool.api.R; +import com.qianwen.license.common.LicenseExtraModel; +import com.qianwen.license.common.LicenseVerify; + +import de.schlichtherle.license.LicenseContent; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; + +@Api(value = "宸ユ椂鍒嗘瀽", tags = {"宸ユ椂鍒嗘瀽"}) +@RestController +@ApiResource({"test"}) +@NonDS +@Validated +public class LicenseTestController extends BladeController { + private Logger log = LoggerFactory.getLogger(this.getClass()); + + + @GetMapping({"/licmcount"}) + @ApiOperation("娴嬭瘯鎺堟潈") + public R<Object> licmcount() { + LicenseVerify licenseVerify = new LicenseVerify(); + LicenseContent model = licenseVerify.getContent(); + return R.data(model.getExtra()); + } + + + +} diff --git a/smart-man-boot/src/main/java/com/qianwen/smartman/modules/workinghour/controller/PartWorkHourController.java b/smart-man-boot/src/main/java/com/qianwen/smartman/modules/workinghour/controller/PartWorkHourController.java index 55c57f3..5fc83be 100644 --- a/smart-man-boot/src/main/java/com/qianwen/smartman/modules/workinghour/controller/PartWorkHourController.java +++ b/smart-man-boot/src/main/java/com/qianwen/smartman/modules/workinghour/controller/PartWorkHourController.java @@ -68,7 +68,7 @@ try { return R.data(partWorkingHourExportService.export(id)); } catch (Exception e) { - log.error("瀵煎嚭鍏紡鏁版嵁閿欒",e); + log.error("瀵煎嚭鍏椂鏁版嵁閿欒",e); return R.fail(e.getMessage()); } diff --git a/smart-man-boot/src/main/resources/application.yml b/smart-man-boot/src/main/resources/application.yml index 300e259..3f14e1c 100644 --- a/smart-man-boot/src/main/resources/application.yml +++ b/smart-man-boot/src/main/resources/application.yml @@ -17,6 +17,12 @@ name: smartman-api banner: location: classpath:smartbanner.txt + resources: + static-locations: static + thymeleaf: + cache: false + mvc: + static-path-pattern: classpath:/static/** redis: ##redis 鍗曟満鐜閰嶇疆 host: ${redis.host} @@ -275,6 +281,7 @@ - /blade-cps/group/groupWorkstation/jimu - /blade-fms/order/process/issued - /blade-cps/test/** + - /test/* #鎺堟潈璁よ瘉閰嶇疆 auth: - method: ALL @@ -349,36 +356,12 @@ - FlowMapper - SuperNewCollectMapper - -# rocketmq 閰嶇疆椤癸紝瀵瑰簲 RocketMQProperties 閰嶇疆绫� -rocketmq: - name-server: ${rocketmq-name-server} # RocketMQ Namesrv - # Producer 閰嶇疆椤� - producer: - group: masterlink-default-producer-group # 榛樿鐢熶骇鑰呭垎缁勶紝鏈�濂戒娇鐢ㄥ悇鑷槑纭垎缁� - send-message-timeout: 3000 # 鍙戦�佹秷鎭秴鏃舵椂闂达紝鍗曚綅锛氭绉掋�傞粯璁や负 3000 銆� - compress-message-body-threshold: 4096 # 娑堟伅鍘嬬缉闃�鍊硷紝褰撴秷鎭綋鐨勫ぇ灏忚秴杩囪闃�鍊煎悗锛岃繘琛屾秷鎭帇缂┿�傞粯璁や负 4 * 1024B - max-message-size: 4194304 # 娑堟伅浣撶殑鏈�澶у厑璁稿ぇ灏忋�傘�傞粯璁や负 4 * 1024 * 1024B - retry-times-when-send-failed: 2 # 鍚屾鍙戦�佹秷鎭椂锛屽け璐ラ噸璇曟鏁般�傞粯璁や负 2 娆°�� - retry-times-when-send-async-failed: 2 # 寮傛鍙戦�佹秷鎭椂锛屽け璐ラ噸璇曟鏁般�傞粯璁や负 2 娆°�� - retry-next-server: false # 鍙戦�佹秷鎭粰 Broker 鏃讹紝濡傛灉鍙戦�佸け璐ワ紝鏄惁閲嶈瘯鍙﹀涓�鍙� Broker 銆傞粯璁や负 false - enable-msg-trace: true # 鏄惁寮�鍚秷鎭建杩瑰姛鑳姐�傞粯璁や负 true 寮�鍚�� - customized-trace-topic: BLADE_RMQ_SYS_TRACE_TOPIC # 鑷畾涔夋秷鎭建杩圭殑 Topic 銆傞粯璁や负 RMQ_SYS_TRACE_TOPIC 銆� - message: - posting: - workstation-aggregate: - topic: workstation-aggregate-topic - consumer: workstation-aggregate-comsumer ---- - -spring: - resources: - static-locations: static - thymeleaf: - cache: false - mvc: - static-path-pattern: classpath:/static/** - +license: + subject: user #涓讳綋 - 娉ㄦ剰涓讳綋瑕佷笌鐢熸垚璇佷功鐨勪富浣撲竴鑷翠竴鑷达紝涓嶇劧楠岃瘉閫氳繃涓嶄簡 + publicAlias: publiccert #鍏挜鍒О + storePass: a1b2c3 #璁块棶鍏挜搴撶殑瀵嗙爜 + licensePath: D:\lictest\ctest.lic #license浣嶇疆 + publicKeysStorePath: D:\lictest\publicCerts.store #鍏挜浣嶇疆 forest: bean-id: config0 # 鍦╯pring涓婁笅鏂囦腑bean鐨刬d, 榛樿鍊间负forestConfiguration backend: okhttp3 # 鍚庣HTTP API锛� okhttp3 銆愭敮鎸乣okhttp3`/`httpclient`銆� -- Gitblit v1.9.3