8个文件已修改
6 文件已重命名
42个文件已添加
5个文件已删除
| | |
| | | <packaging>pom</packaging> |
| | | <modules> |
| | | <module>servicebase</module> |
| | | <!-- <module>springsecurity</module>--> |
| | | <module>springsecurity</module> |
| | | </modules> |
| | | <properties> |
| | | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
| | |
| | | <dependency> |
| | | <groupId>com.baomidou</groupId> |
| | | <artifactId>mybatis-plus-boot-starter</artifactId> |
| | | <version>3.2.0</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>com.baomidou</groupId> |
| | | <artifactId>dynamic-datasource-spring-boot-starter</artifactId> |
| | | <version>3.2.0</version> |
| | | </dependency> |
| | | |
| | |
| | | <groupId>org.apache.velocity</groupId> |
| | | <artifactId>velocity-engine-core</artifactId> |
| | | <version>2.0</version> |
| | | <exclusions> |
| | | <exclusion> |
| | | <groupId>com.alibaba</groupId> |
| | | <artifactId>fastjson</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.alibaba</groupId> |
| | |
| | | <version>1.1.8</version> |
| | | </dependency> |
| | | |
| | | <!-- 数据库文档--> |
| | | <dependency> |
| | | <groupId>com.alibaba</groupId> |
| | | <artifactId>fastjson</artifactId> |
| | | <version>1.2.60</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.apache.poi</groupId> |
| | | <artifactId>poi</artifactId> |
| | | <version>3.17</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.apache.poi</groupId> |
| | | <artifactId>poi-ooxml</artifactId> |
| | | <version>3.17</version> |
| | | </dependency> |
| | | |
| | | <!-- 数据库设计文档生成工具--> |
| | | <dependency> |
| | | <groupId>cn.smallbun.screw</groupId> |
| | | <artifactId>screw-core</artifactId> |
| | | <version>1.0.5</version> |
| | | <exclusions> |
| | | <exclusion> |
| | | <groupId>com.alibaba</groupId> |
| | | <artifactId>fastjson</artifactId> |
| | | </exclusion> |
| | | </exclusions> |
| | | </dependency> |
| | | <!-- 2.0~2.2 Knife4j 2.0.0 ~ 2.0.6--> |
| | | <!-- <dependency>--> |
| | |
| | | <version>2.7.0</version> |
| | | </dependency> |
| | | |
| | | |
| | | <!--jwt 项目生成token--> |
| | | <dependency> |
| | | <groupId>com.auth0</groupId> |
| | | <artifactId>java-jwt</artifactId> |
| | | <version>3.10.3</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.assertj</groupId> |
| | | <artifactId>assertj-core</artifactId> |
| | | </dependency> |
| | | <!--hutool java验证方法类--> |
| | | <dependency> |
| | | <groupId>cn.hutool</groupId> |
| | |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-data-redis</artifactId> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.springframework.session</groupId> |
| | | <artifactId>spring-session-data-redis</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-security</artifactId> |
| | | <groupId>com.alibaba</groupId> |
| | | <artifactId>fastjson</artifactId> |
| | | <version>1.2.33</version> |
| | | </dependency> |
| | | |
| | | </dependencies> |
| | | |
| | | |
| | |
| | | |
| | | import com.mes.common.interceptor.JwtInterceptor; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.web.servlet.config.annotation.CorsRegistry; |
| | | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
| | | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | @Configuration |
| | | |
| | | public class InterceptorConfig implements WebMvcConfigurer { |
| | | @Resource |
| | | private JwtInterceptor jwtInterceptor; |
| | | |
| | | @Override |
| | | public void addInterceptors(InterceptorRegistry registry) { |
| | | registry.addInterceptor(jwtInterceptor) |
| | |
| | | .excludePathPatterns("/user/login", |
| | | "/user/register", |
| | | "/swagger-resources/**" |
| | | ,"/webjars/**" |
| | | ,"/v2/**" |
| | | ,"/swagger-ui.html/**" |
| | | ,"/swagger-ui.html#/**"); |
| | | , "/webjars/**" |
| | | , "/v2/**" |
| | | , "/swagger-ui.html/**" |
| | | , "/swagger-ui.html#/**"); |
| | | } |
| | | |
| | | @Override |
| | | public void addCorsMappings(CorsRegistry registry) { |
| | | // 设置允许跨域的路径 |
| | | registry.addMapping("/**") |
| | | // 设置允许跨域请求的域名 |
| | | // .allowedOriginPatterns("*") |
| | | // 是否允许cookie |
| | | .allowCredentials(true) |
| | | // 设置允许的请求方式 |
| | | .allowedMethods("GET", "POST", "DELETE", "PUT") |
| | | // 设置允许的header属性 |
| | | .allowedHeaders("*") |
| | | // 跨域允许时间 |
| | | .maxAge(3600); |
| | | } |
| | | |
| | | } |
File was renamed from hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/common/PLCAutoMes.java |
| | |
| | | package com.mes.common; |
| | | package com.mes.device; |
| | | |
| | | import com.mes.device.PlcParameterObject; |
| | | import com.mes.tools.InitUtil; |
| | | import org.springframework.context.annotation.Configuration; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | import static com.mes.tools.InitUtil.readAndUpdateWordValues; |
| | | |
| | | public class PLCAutoMes extends Thread { |
| | | |
| | |
| | | // System.out.println(jsonFilePath); |
| | | |
| | | // readAndUpdateWordValues(PlcReadObject); |
| | | readAndUpdateWordValues(PlcMesObject); |
| | | InitUtil.readAndUpdateWordValues(PlcMesObject); |
| | | // readAndUpdateWordValues(PlcframeObject); |
| | | // readAndUpdateWordValues(PlcframeObject); |
| | | |
File was renamed from hangzhoumesParent/moduleService/UnLoadGlassModule/src/main/java/com/mes/tools/InitUtil.java |
| | |
| | | package com.mes.tools;
|
| | |
|
| | | import cn.hutool.json.JSONArray;
|
| | | import cn.hutool.json.JSONObject;
|
| | | import com.mes.device.PlcBitInfo;
|
| | | import com.mes.device.PlcBitObject;
|
| | | import com.mes.device.PlcParameterInfo;
|
| | | import com.mes.device.PlcParameterObject;
|
| | |
|
| | | import java.io.BufferedReader;
|
| | | import java.io.FileReader;
|
| | | import java.io.IOException;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Arrays;
|
| | | import java.util.List;
|
| | |
|
| | | public class InitUtil {
|
| | | //初始化word
|
| | | public static PlcParameterObject initword(String jsonFilePath) {
|
| | | try {
|
| | | FileReader fileReader = new FileReader(jsonFilePath);
|
| | | BufferedReader bufferedReader = new BufferedReader(fileReader);
|
| | |
|
| | | StringBuilder content = new StringBuilder();
|
| | | String line;
|
| | |
|
| | | while ((line = bufferedReader.readLine()) != null) {
|
| | | content.append(line);
|
| | | }
|
| | |
|
| | | bufferedReader.close();
|
| | | fileReader.close();
|
| | |
|
| | | JSONObject jsonfileobj = new JSONObject(content.toString());
|
| | | JSONArray jsonArray = jsonfileobj.getJSONArray("parameteInfor");
|
| | | PlcParameterObject plcParameterObject = new PlcParameterObject();
|
| | | plcParameterObject.setPlcAddressBegin(jsonfileobj.getStr("plcAddressBegin"));//设置起始位地址
|
| | | plcParameterObject.setPlcAddressLength(Integer.valueOf(jsonfileobj.getStr("plcAddressLenght")));//设置地址长度
|
| | |
|
| | | for (int i = 0; i < jsonArray.size(); i++) {
|
| | | JSONObject parameterObj = jsonArray.getJSONObject(i);
|
| | | PlcParameterInfo plcParameterInfo = new PlcParameterInfo(jsonfileobj.getStr("plcAddressBegin")); //参数实例
|
| | | String codeid = parameterObj.getStr("codeId");
|
| | | plcParameterInfo.setCodeId(codeid);
|
| | | plcParameterInfo.setAddressIndex(Integer.valueOf(parameterObj.getStr("addressIndex")));
|
| | | //plcParameterInfo.setRatio(Integer.valueOf(parameterObj.getStr("ratio")));
|
| | | plcParameterInfo.setAddressLength(Integer.valueOf(parameterObj.getStr("addressLenght")));
|
| | | plcParameterInfo.setUnit(parameterObj.getStr("unit"));
|
| | | plcParameterObject.addPlcParameter(plcParameterInfo);
|
| | | }
|
| | | // byte[] getplcvlues = {0x01, 0x02, 0x03, 0x04,0x01, 0x02, 0x03, 0x04,0x01, 0x02, 0x03, 0x04,0x01, 0x02,0x01, 0x02, 0x03, 0x04,0x01, 0x02, 0x03, 0x04,0x01, 0x02, 0x03, 0x04,0x01, 0x02,0x01, 0x02, 0x03, 0x04,0x01, 0x02, 0x03, 0x04,0x01, 0x02, 0x03, 0x04,0x01, 0x02};
|
| | | // byte[] getplcvlues = S7control.getinstance().ReadByte(plcParameterObject.getPlcAddressBegin(), plcParameterObject.getPlcAddressLength());
|
| | | // plcParameterObject.setPlcParameterList(getplcvlues);
|
| | |
|
| | | return plcParameterObject;
|
| | | } catch (IOException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | //初始化bit
|
| | | public static PlcBitObject initbit(String jsonFilePath) {
|
| | | PlcBitObject plcBitObject = new PlcBitObject();
|
| | |
|
| | | try {
|
| | | FileReader fileReader = new FileReader(jsonFilePath);
|
| | | BufferedReader bufferedReader = new BufferedReader(fileReader);
|
| | |
|
| | | StringBuilder content = new StringBuilder();
|
| | | String line;
|
| | |
|
| | | while ((line = bufferedReader.readLine()) != null) {
|
| | | content.append(line);
|
| | | }
|
| | |
|
| | | bufferedReader.close();
|
| | | fileReader.close();
|
| | |
|
| | | JSONObject jsonfileobj = new JSONObject(content.toString());
|
| | | JSONArray jsonArray = jsonfileobj.getJSONArray("parameteInfor");
|
| | | plcBitObject.setPlcAddressBegin(jsonfileobj.getStr("plcAddressBegin"));//设置起始位地址
|
| | | plcBitObject.setPlcAddressLength(Integer.valueOf(jsonfileobj.getStr("plcAddressLenght")));//设置地址长度
|
| | |
|
| | | for (int i = 0; i < jsonArray.size(); i++) {
|
| | | JSONObject parameterObj = jsonArray.getJSONObject(i);
|
| | |
|
| | | PlcBitInfo plcBitInfo = new PlcBitInfo(jsonfileobj.getStr("plcAddressBegin")); //参数实例
|
| | | String codeid = parameterObj.getStr("codeId");
|
| | | plcBitInfo.setCodeId(codeid);
|
| | | plcBitInfo.setAddressIndex(Integer.valueOf(parameterObj.getStr("addressIndex")));
|
| | |
|
| | | plcBitObject.addPlcBit(plcBitInfo);
|
| | | }
|
| | | System.out.println("");
|
| | | } catch (IOException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | //Boolean[] values1 = { false, true, true, true, false, false, true, false,
|
| | | // false, true ,true };
|
| | | // List<Boolean> getplcvlues = new ArrayList<>(Arrays.asList(values1));
|
| | |
|
| | | // List<Boolean> getplcvlues = S7control.getinstance().ReadBits(plcBitObject.getPlcAddressBegin(), plcBitObject.getPlcAddressLength());
|
| | | // plcBitObject.setPlcBitList(getplcvlues);
|
| | | return plcBitObject;
|
| | | }
|
| | |
|
| | | //
|
| | | public static void readAndUpdateBitValues(PlcBitObject plcBitObject) {
|
| | |
|
| | | Boolean[] values1 = { false, true, true, true, false, false, true, false,
|
| | | false, true ,true };
|
| | | List<Boolean> getplcvlues = new ArrayList<>(Arrays.asList(values1));
|
| | | //List<Boolean> getplcvlues = S7control.getinstance().ReadBits(plcBitObject.getPlcAddressBegin(), plcBitObject.getPlcAddressLength());
|
| | | plcBitObject.setPlcBitList(getplcvlues);
|
| | | }
|
| | |
|
| | |
|
| | | public static void readAndUpdateWordValues(PlcParameterObject plcParameterObject) {
|
| | |
|
| | | byte[] getplcvlues = {0x01, 0x02, 0x03, 0x04,0x01, 0x02, 0x03, 0x04,0x01, 0x02, 0x03,0x03};
|
| | | // byte[] getplcvlues = MockS7PLC.getInstance().readByte(plcParameterObject.getPlcAddressBegin(), plcParameterObject.getPlcAddressLength());
|
| | | plcParameterObject.setPlcParameterList(getplcvlues);
|
| | | }
|
| | |
|
| | |
|
| | |
|
| | | }
|
| | | package com.mes.tools; |
| | | |
| | | import cn.hutool.json.JSONArray; |
| | | import cn.hutool.json.JSONObject; |
| | | import com.mes.device.PlcBitInfo; |
| | | import com.mes.device.PlcBitObject; |
| | | import com.mes.device.PlcParameterInfo; |
| | | import com.mes.device.PlcParameterObject; |
| | | |
| | | import java.io.BufferedReader; |
| | | import java.io.FileReader; |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | | public class InitUtil { |
| | | //初始化word |
| | | public static PlcParameterObject initword(String jsonFilePath) { |
| | | try { |
| | | FileReader fileReader = new FileReader(jsonFilePath); |
| | | BufferedReader bufferedReader = new BufferedReader(fileReader); |
| | | |
| | | StringBuilder content = new StringBuilder(); |
| | | String line; |
| | | |
| | | while ((line = bufferedReader.readLine()) != null) { |
| | | content.append(line); |
| | | } |
| | | |
| | | bufferedReader.close(); |
| | | fileReader.close(); |
| | | |
| | | JSONObject jsonfileobj = new JSONObject(content.toString()); |
| | | JSONArray jsonArray = jsonfileobj.getJSONArray("parameteInfor"); |
| | | PlcParameterObject plcParameterObject = new PlcParameterObject(); |
| | | plcParameterObject.setPlcAddressBegin(jsonfileobj.getStr("plcAddressBegin"));//设置起始位地址 |
| | | plcParameterObject.setPlcAddressLength(Integer.valueOf(jsonfileobj.getStr("plcAddressLenght")));//设置地址长度 |
| | | |
| | | for (int i = 0; i < jsonArray.size(); i++) { |
| | | JSONObject parameterObj = jsonArray.getJSONObject(i); |
| | | PlcParameterInfo plcParameterInfo = new PlcParameterInfo(jsonfileobj.getStr("plcAddressBegin")); //参数实例 |
| | | String codeid = parameterObj.getStr("codeId"); |
| | | plcParameterInfo.setCodeId(codeid); |
| | | plcParameterInfo.setAddressIndex(Integer.valueOf(parameterObj.getStr("addressIndex"))); |
| | | //plcParameterInfo.setRatio(Integer.valueOf(parameterObj.getStr("ratio"))); |
| | | plcParameterInfo.setAddressLength(Integer.valueOf(parameterObj.getStr("addressLenght"))); |
| | | plcParameterInfo.setUnit(parameterObj.getStr("unit")); |
| | | plcParameterObject.addPlcParameter(plcParameterInfo); |
| | | } |
| | | // byte[] getplcvlues = {0x01, 0x02, 0x03, 0x04,0x01, 0x02, 0x03, 0x04,0x01, 0x02, 0x03, 0x04,0x01, 0x02,0x01, 0x02, 0x03, 0x04,0x01, 0x02, 0x03, 0x04,0x01, 0x02, 0x03, 0x04,0x01, 0x02,0x01, 0x02, 0x03, 0x04,0x01, 0x02, 0x03, 0x04,0x01, 0x02, 0x03, 0x04,0x01, 0x02}; |
| | | // byte[] getplcvlues = S7control.getinstance().ReadByte(plcParameterObject.getPlcAddressBegin(), plcParameterObject.getPlcAddressLength()); |
| | | // plcParameterObject.setPlcParameterList(getplcvlues); |
| | | |
| | | return plcParameterObject; |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | //初始化bit |
| | | public static PlcBitObject initbit(String jsonFilePath) { |
| | | PlcBitObject plcBitObject = new PlcBitObject(); |
| | | |
| | | try { |
| | | FileReader fileReader = new FileReader(jsonFilePath); |
| | | BufferedReader bufferedReader = new BufferedReader(fileReader); |
| | | |
| | | StringBuilder content = new StringBuilder(); |
| | | String line; |
| | | |
| | | while ((line = bufferedReader.readLine()) != null) { |
| | | content.append(line); |
| | | } |
| | | |
| | | bufferedReader.close(); |
| | | fileReader.close(); |
| | | |
| | | JSONObject jsonfileobj = new JSONObject(content.toString()); |
| | | JSONArray jsonArray = jsonfileobj.getJSONArray("parameteInfor"); |
| | | plcBitObject.setPlcAddressBegin(jsonfileobj.getStr("plcAddressBegin"));//设置起始位地址 |
| | | plcBitObject.setPlcAddressLength(Integer.valueOf(jsonfileobj.getStr("plcAddressLenght")));//设置地址长度 |
| | | |
| | | for (int i = 0; i < jsonArray.size(); i++) { |
| | | JSONObject parameterObj = jsonArray.getJSONObject(i); |
| | | |
| | | PlcBitInfo plcBitInfo = new PlcBitInfo(jsonfileobj.getStr("plcAddressBegin")); //参数实例 |
| | | String codeid = parameterObj.getStr("codeId"); |
| | | plcBitInfo.setCodeId(codeid); |
| | | plcBitInfo.setAddressIndex(Integer.valueOf(parameterObj.getStr("addressIndex"))); |
| | | |
| | | plcBitObject.addPlcBit(plcBitInfo); |
| | | } |
| | | System.out.println(""); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | //Boolean[] values1 = { false, true, true, true, false, false, true, false, |
| | | // false, true ,true }; |
| | | // List<Boolean> getplcvlues = new ArrayList<>(Arrays.asList(values1)); |
| | | |
| | | // List<Boolean> getplcvlues = S7control.getinstance().ReadBits(plcBitObject.getPlcAddressBegin(), plcBitObject.getPlcAddressLength()); |
| | | // plcBitObject.setPlcBitList(getplcvlues); |
| | | return plcBitObject; |
| | | } |
| | | |
| | | // |
| | | public static void readAndUpdateBitValues(PlcBitObject plcBitObject) { |
| | | |
| | | Boolean[] values1 = { false, true, true, true, false, false, true, false, |
| | | false, true ,true }; |
| | | List<Boolean> getplcvlues = new ArrayList<>(Arrays.asList(values1)); |
| | | //List<Boolean> getplcvlues = S7control.getinstance().ReadBits(plcBitObject.getPlcAddressBegin(), plcBitObject.getPlcAddressLength()); |
| | | plcBitObject.setPlcBitList(getplcvlues); |
| | | } |
| | | |
| | | |
| | | public static void readAndUpdateWordValues(PlcParameterObject plcParameterObject) { |
| | | |
| | | byte[] getplcvlues = {0x01, 0x02, 0x03, 0x04,0x01, 0x02, 0x03, 0x04,0x01, 0x02, 0x03,0x03}; |
| | | // byte[] getplcvlues = MockS7PLC.getInstance().readByte(plcParameterObject.getPlcAddressBegin(), plcParameterObject.getPlcAddressLength()); |
| | | plcParameterObject.setPlcParameterList(getplcvlues); |
| | | } |
| | | |
| | | |
| | | |
| | | } |
| | |
| | | <version>1.0-SNAPSHOT</version> |
| | | </dependency> |
| | | <!-- Spring Security依赖 --> |
| | | <!-- <dependency>--> |
| | | <!-- <groupId>org.springframework.boot</groupId>--> |
| | | <!-- <artifactId>spring-boot-starter-security</artifactId>--> |
| | | <!-- </dependency>--> |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-security</artifactId> |
| | | </dependency> |
| | | |
| | | <!-- <dependency>--> |
| | | <!-- <groupId>io.jsonwebtoken</groupId>--> |
| | | <!-- <artifactId>jjwt</artifactId>--> |
| | | <!-- </dependency>--> |
| | | <dependency> |
| | | <groupId>io.jsonwebtoken</groupId> |
| | | <artifactId>jjwt</artifactId> |
| | | <version>0.9.0</version> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | </project> |
New file |
| | |
| | | package com.mes.common.config; |
| | | |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.web.cors.CorsConfiguration; |
| | | import org.springframework.web.cors.reactive.CorsWebFilter; |
| | | import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource; |
| | | import org.springframework.web.servlet.config.annotation.CorsRegistry; |
| | | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| | | |
| | | /** |
| | | * @Author : zhoush |
| | | * @Date: 2024/3/25 14:13 |
| | | * @Description: |
| | | */ |
| | | @Configuration |
| | | public class MyCorsConfig { |
| | | |
| | | @Bean |
| | | public WebMvcConfigurer corsConfigurer() { |
| | | return new WebMvcConfigurer() { |
| | | @Override |
| | | public void addCorsMappings(CorsRegistry registry) { |
| | | registry.addMapping("/**"); |
| | | } |
| | | }; |
| | | } |
| | | |
| | | @Bean |
| | | public CorsWebFilter corsWebFilter() { |
| | | UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); |
| | | |
| | | CorsConfiguration corsConfiguration = new CorsConfiguration(); |
| | | |
| | | //1、配置跨域 |
| | | corsConfiguration.addAllowedHeader("*"); |
| | | corsConfiguration.addAllowedMethod("*"); |
| | | corsConfiguration.addAllowedOrigin("*"); |
| | | corsConfiguration.setAllowCredentials(true); |
| | | |
| | | source.registerCorsConfiguration("/**", corsConfiguration); |
| | | return new CorsWebFilter(source); |
| | | } |
| | | |
| | | |
| | | } |
New file |
| | |
| | | package com.mes.common.config; |
| | | |
| | | import com.mes.common.utils.FastJsonRedisSerializer; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.data.redis.connection.RedisConnectionFactory; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.data.redis.serializer.StringRedisSerializer; |
| | | |
| | | /** |
| | | * @Author : zhoush |
| | | * @Date: 2024/4/9 19:13 |
| | | * @Description: |
| | | */ |
| | | @Configuration |
| | | public class RedisConfig { |
| | | |
| | | @Bean |
| | | @SuppressWarnings(value = {"unchecked", "rawtypes"}) |
| | | public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) { |
| | | RedisTemplate<Object, Object> template = new RedisTemplate<>(); |
| | | template.setConnectionFactory(connectionFactory); |
| | | |
| | | FastJsonRedisSerializer serializer = new FastJsonRedisSerializer(Object.class); |
| | | |
| | | // 使用StringRedisSerializer来序列化和反序列化redis的key值 |
| | | template.setKeySerializer(new StringRedisSerializer()); |
| | | template.setValueSerializer(serializer); |
| | | |
| | | // Hash的key也采用StringRedisSerializer的序列化方式 |
| | | template.setHashKeySerializer(new StringRedisSerializer()); |
| | | template.setHashValueSerializer(serializer); |
| | | |
| | | template.afterPropertiesSet(); |
| | | return template; |
| | | } |
| | | } |
New file |
| | |
| | | package com.mes.common.config; |
| | | |
| | | |
| | | import com.mes.common.filter.JwtAuthenticationTokenFilter; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.security.authentication.AuthenticationManager; |
| | | 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.WebSecurityConfigurerAdapter; |
| | | import org.springframework.security.config.http.SessionCreationPolicy; |
| | | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
| | | import org.springframework.security.crypto.password.PasswordEncoder; |
| | | |
| | | @Configuration |
| | | //@EnableWebSecurity |
| | | @EnableGlobalMethodSecurity(prePostEnabled = true) |
| | | public class TokenWebSecurityConfig extends WebSecurityConfigurerAdapter { |
| | | @Autowired |
| | | private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter; |
| | | |
| | | @Bean |
| | | public PasswordEncoder passwordEncoder() { |
| | | return new BCryptPasswordEncoder(); |
| | | } |
| | | |
| | | /** |
| | | * 配置过滤规则 |
| | | */ |
| | | @Override |
| | | protected void configure(HttpSecurity http) throws Exception { |
| | | http |
| | | //关闭csrf |
| | | .csrf().disable() |
| | | //不通过Session获取SecurityContext |
| | | .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) |
| | | .and() |
| | | .authorizeRequests() |
| | | // 对于登录接口 允许匿名访问 |
| | | .antMatchers("/userinfo/login").anonymous() |
| | | .antMatchers("/hello").permitAll() |
| | | // 除上面外的所有请求全部需要鉴权认证 |
| | | .anyRequest().permitAll(); |
| | | // http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class); |
| | | } |
| | | |
| | | @Bean |
| | | @Override |
| | | public AuthenticationManager authenticationManagerBean() throws Exception { |
| | | return super.authenticationManagerBean(); |
| | | } |
| | | } |
New file |
| | |
| | | package com.mes.common.filter; |
| | | |
| | | import com.mes.common.utils.JwtUtil; |
| | | import com.mes.common.utils.RedisUtil; |
| | | import com.mes.menu.mapper.SysMenuMapper; |
| | | import com.mes.userinfo.entity.LoginUser; |
| | | import io.jsonwebtoken.Claims; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| | | import org.springframework.security.core.GrantedAuthority; |
| | | import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| | | import org.springframework.security.core.context.SecurityContextHolder; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.StringUtils; |
| | | import org.springframework.web.filter.OncePerRequestFilter; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.FilterChain; |
| | | import javax.servlet.ServletException; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @Author : zhoush |
| | | * @Date: 2024/4/10 9:42 |
| | | * @Description: |
| | | */ |
| | | @Component |
| | | public class JwtAuthenticationTokenFilter extends OncePerRequestFilter { |
| | | // 此处解释为什么不去实现Filter接口,因为在某些情况下会过滤两次,执行两次Filter里面的方法,所以我们选择继承SpringSecurity中的OncePerRequestFilter |
| | | @Autowired |
| | | private RedisUtil redisUtil; |
| | | |
| | | @Resource |
| | | private SysMenuMapper menuMapper; |
| | | |
| | | @Override |
| | | protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { |
| | | //获取token |
| | | String token = request.getHeader("token"); |
| | | if (!StringUtils.hasText(token)) { |
| | | //放行 |
| | | filterChain.doFilter(request, response); |
| | | return; // 此处加上return好处是后面结果返回的时候就不会再走一遍此过滤器的方法了 |
| | | } |
| | | //解析token |
| | | String userid; |
| | | try { |
| | | Claims claims = JwtUtil.parseJWT(token); |
| | | userid = claims.getSubject(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw new RuntimeException("token非法"); |
| | | } |
| | | //从redis中获取用户信息 |
| | | String redisKey = "login:" + userid; |
| | | LoginUser loginUser = redisUtil.getCacheObject(redisKey); |
| | | if (Objects.isNull(loginUser)) { |
| | | throw new RuntimeException("用户未登录"); |
| | | } |
| | | //存入SecurityContextHolder,以供后面的过滤器使用 |
| | | List<String> permissionKeyList = menuMapper.selectPermsByUserId(Long.parseLong(userid)); |
| | | List<GrantedAuthority> authorities = permissionKeyList.stream(). |
| | | map(SimpleGrantedAuthority::new) |
| | | .collect(Collectors.toList()); |
| | | UsernamePasswordAuthenticationToken authenticationToken = |
| | | new UsernamePasswordAuthenticationToken(loginUser, null, authorities); |
| | | SecurityContextHolder.getContext().setAuthentication(authenticationToken); |
| | | //放行 |
| | | filterChain.doFilter(request, response); |
| | | } |
| | | } |
New file |
| | |
| | | package com.mes.common.handler; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.mes.common.utils.WebUtils; |
| | | import com.mes.utils.Result; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.security.access.AccessDeniedException; |
| | | import org.springframework.security.web.access.AccessDeniedHandler; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.servlet.ServletException; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | |
| | | /** |
| | | * @Author : zhoush |
| | | * @Date: 2024/4/12 10:38 |
| | | * @Description: |
| | | */ |
| | | @Component |
| | | public class AccessDeniedHandlerImpl implements AccessDeniedHandler { |
| | | @Override |
| | | public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException { |
| | | Result result = Result.error(HttpStatus.FORBIDDEN.value(), "权限不足"); |
| | | String json = JSON.toJSONString(result); |
| | | WebUtils.renderString(response, json); |
| | | |
| | | } |
| | | } |
New file |
| | |
| | | package com.mes.common.handler; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.mes.common.utils.WebUtils; |
| | | import com.mes.utils.Result; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.security.core.AuthenticationException; |
| | | import org.springframework.security.web.AuthenticationEntryPoint; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.servlet.ServletException; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | |
| | | /** |
| | | * @Author : zhoush |
| | | * @Date: 2024/4/12 10:38 |
| | | * @Description: |
| | | */ |
| | | @Component |
| | | public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint { |
| | | @Override |
| | | public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { |
| | | Result result = Result.error(HttpStatus.UNAUTHORIZED.value(), "认证失败请重新登录"); |
| | | String json = JSON.toJSONString(result); |
| | | WebUtils.renderString(response, json); |
| | | } |
| | | } |
New file |
| | |
| | | package com.mes.common.utils; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.parser.ParserConfig; |
| | | import com.alibaba.fastjson.serializer.SerializerFeature; |
| | | import com.fasterxml.jackson.databind.JavaType; |
| | | import com.fasterxml.jackson.databind.type.TypeFactory; |
| | | import org.springframework.data.redis.serializer.RedisSerializer; |
| | | import org.springframework.data.redis.serializer.SerializationException; |
| | | |
| | | import java.nio.charset.Charset; |
| | | |
| | | /** |
| | | * @Author : zhoush |
| | | * @Date: 2024/4/11 15:28 |
| | | * @Description: |
| | | */ |
| | | public class FastJsonRedisSerializer<T> implements RedisSerializer<T> { |
| | | |
| | | public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); |
| | | |
| | | private Class<T> clazz; |
| | | |
| | | static { |
| | | ParserConfig.getGlobalInstance().setAutoTypeSupport(true); |
| | | } |
| | | |
| | | public FastJsonRedisSerializer(Class<T> clazz) { |
| | | super(); |
| | | this.clazz = clazz; |
| | | } |
| | | |
| | | @Override |
| | | public byte[] serialize(T t) throws SerializationException { |
| | | if (t == null) { |
| | | return new byte[0]; |
| | | } |
| | | return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET); |
| | | } |
| | | |
| | | @Override |
| | | public T deserialize(byte[] bytes) throws SerializationException { |
| | | if (bytes == null || bytes.length <= 0) { |
| | | return null; |
| | | } |
| | | String str = new String(bytes, DEFAULT_CHARSET); |
| | | |
| | | return JSON.parseObject(str, clazz); |
| | | } |
| | | |
| | | |
| | | protected JavaType getJavaType(Class<?> clazz) { |
| | | return TypeFactory.defaultInstance().constructType(clazz); |
| | | } |
| | | } |
New file |
| | |
| | | package com.mes.common.utils; |
| | | |
| | | |
| | | import io.jsonwebtoken.Claims; |
| | | import io.jsonwebtoken.JwtBuilder; |
| | | import io.jsonwebtoken.Jwts; |
| | | import io.jsonwebtoken.SignatureAlgorithm; |
| | | |
| | | import javax.crypto.SecretKey; |
| | | import javax.crypto.spec.SecretKeySpec; |
| | | import java.util.Base64; |
| | | import java.util.Date; |
| | | import java.util.UUID; |
| | | |
| | | /** |
| | | * @Author : zhoush |
| | | * @Date: 2024/4/9 19:15 |
| | | * @Description: |
| | | */ |
| | | public class JwtUtil { |
| | | |
| | | //有效期为 |
| | | public static final Long JWT_TTL = 60 * 60 * 1000L;// 60 * 60 *1000 一个小时 |
| | | //设置秘钥明文 |
| | | public static final String JWT_KEY = "sangeng"; |
| | | |
| | | public static String getUUID() { |
| | | String token = UUID.randomUUID().toString().replaceAll("-", ""); |
| | | return token; |
| | | } |
| | | |
| | | /** |
| | | * 生成jtw |
| | | * |
| | | * @param subject token中要存放的数据(json格式) |
| | | * @return |
| | | */ |
| | | public static String createJWT(String subject) { |
| | | JwtBuilder builder = getJwtBuilder(subject, null, getUUID());// 设置过期时间 |
| | | return builder.compact(); |
| | | } |
| | | |
| | | /** |
| | | * 生成jtw |
| | | * |
| | | * @param subject token中要存放的数据(json格式) |
| | | * @param ttlMillis token超时时间 |
| | | * @return |
| | | */ |
| | | public static String createJWT(String subject, Long ttlMillis) { |
| | | JwtBuilder builder = getJwtBuilder(subject, ttlMillis, getUUID());// 设置过期时间 |
| | | return builder.compact(); |
| | | } |
| | | |
| | | private static JwtBuilder getJwtBuilder(String subject, Long ttlMillis, String uuid) { |
| | | SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; |
| | | SecretKey secretKey = generalKey(); |
| | | long nowMillis = System.currentTimeMillis(); |
| | | Date now = new Date(nowMillis); |
| | | if (ttlMillis == null) { |
| | | ttlMillis = JwtUtil.JWT_TTL; |
| | | } |
| | | long expMillis = nowMillis + ttlMillis; |
| | | Date expDate = new Date(expMillis); |
| | | return Jwts.builder() |
| | | .setId(uuid) //唯一的ID |
| | | .setSubject(subject) // 主题 可以是JSON数据 |
| | | .setIssuer("sg") // 签发者 |
| | | .setIssuedAt(now) // 签发时间 |
| | | .signWith(signatureAlgorithm, secretKey) //使用HS256对称加密算法签名, 第二个参数为秘钥 |
| | | .setExpiration(expDate); |
| | | } |
| | | |
| | | /** |
| | | * 创建token |
| | | * |
| | | * @param id |
| | | * @param subject |
| | | * @param ttlMillis |
| | | * @return |
| | | */ |
| | | public static String createJWT(String id, String subject, Long ttlMillis) { |
| | | JwtBuilder builder = getJwtBuilder(subject, ttlMillis, id);// 设置过期时间 |
| | | return builder.compact(); |
| | | } |
| | | |
| | | public static void main(String[] args) throws Exception { |
| | | String token = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJjYWM2ZDVhZi1mNjVlLTQ0MDAtYjcxMi0zYWEwOGIyOTIwYjQiLCJzdWIiOiJzZyIsImlzcyI6InNnIiwiaWF0IjoxNjM4MTA2NzEyLCJleHAiOjE2MzgxMTAzMTJ9.JVsSbkP94wuczb4QryQbAke3ysBDIL5ou8fWsbt_ebg"; |
| | | Claims claims = parseJWT(token); |
| | | System.out.println(claims); |
| | | } |
| | | |
| | | /** |
| | | * 生成加密后的秘钥 secretKey |
| | | * |
| | | * @return |
| | | */ |
| | | public static SecretKey generalKey() { |
| | | byte[] encodedKey = Base64.getDecoder().decode(JwtUtil.JWT_KEY); |
| | | SecretKey key = new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES"); |
| | | return key; |
| | | } |
| | | |
| | | /** |
| | | * 解析 |
| | | * |
| | | * @param jwt |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | public static Claims parseJWT(String jwt) throws Exception { |
| | | SecretKey secretKey = generalKey(); |
| | | return Jwts.parser() |
| | | .setSigningKey(secretKey) |
| | | .parseClaimsJws(jwt) |
| | | .getBody(); |
| | | } |
| | | |
| | | |
| | | } |
New file |
| | |
| | | package com.mes.common.utils; |
| | | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.BoundSetOperations; |
| | | import org.springframework.data.redis.core.HashOperations; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.data.redis.core.ValueOperations; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.*; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * spring redis 工具类 |
| | | **/ |
| | | @Component |
| | | public class RedisUtil { |
| | | @Autowired |
| | | public RedisTemplate redisTemplate; |
| | | |
| | | /** |
| | | * 缓存基本的对象,Integer、String、实体类等 |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @param value 缓存的值 |
| | | */ |
| | | public <T> void setCacheObject(final String key, final T value) { |
| | | redisTemplate.opsForValue().set(key, value); |
| | | } |
| | | |
| | | /** |
| | | * 缓存基本的对象,Integer、String、实体类等 |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @param value 缓存的值 |
| | | * @param timeout 时间 |
| | | * @param timeUnit 时间颗粒度 |
| | | */ |
| | | public <T> void setCacheObject(final String key, final T value, final Integer timeout, final TimeUnit timeUnit) { |
| | | redisTemplate.opsForValue().set(key, value, timeout, timeUnit); |
| | | } |
| | | |
| | | /** |
| | | * 设置有效时间 |
| | | * |
| | | * @param key Redis键 |
| | | * @param timeout 超时时间 |
| | | * @return true=设置成功;false=设置失败 |
| | | */ |
| | | public boolean expire(final String key, final long timeout) { |
| | | return expire(key, timeout, TimeUnit.SECONDS); |
| | | } |
| | | |
| | | /** |
| | | * 设置有效时间 |
| | | * |
| | | * @param key Redis键 |
| | | * @param timeout 超时时间 |
| | | * @param unit 时间单位 |
| | | * @return true=设置成功;false=设置失败 |
| | | */ |
| | | public boolean expire(final String key, final long timeout, final TimeUnit unit) { |
| | | return redisTemplate.expire(key, timeout, unit); |
| | | } |
| | | |
| | | /** |
| | | * 获得缓存的基本对象。 |
| | | * |
| | | * @param key 缓存键值 |
| | | * @return 缓存键值对应的数据 |
| | | */ |
| | | public <T> T getCacheObject(final String key) { |
| | | ValueOperations<String, T> operation = redisTemplate.opsForValue(); |
| | | return operation.get(key); |
| | | } |
| | | |
| | | /** |
| | | * 删除单个对象 |
| | | * |
| | | * @param key |
| | | */ |
| | | public boolean deleteObject(final String key) { |
| | | return redisTemplate.delete(key); |
| | | } |
| | | |
| | | /** |
| | | * 删除集合对象 |
| | | * |
| | | * @param collection 多个对象 |
| | | * @return |
| | | */ |
| | | public long deleteObject(final Collection collection) { |
| | | return redisTemplate.delete(collection); |
| | | } |
| | | |
| | | /** |
| | | * 缓存List数据 |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @param dataList 待缓存的List数据 |
| | | * @return 缓存的对象 |
| | | */ |
| | | public <T> long setCacheList(final String key, final List<T> dataList) { |
| | | Long count = redisTemplate.opsForList().rightPushAll(key, dataList); |
| | | return count == null ? 0 : count; |
| | | } |
| | | |
| | | /** |
| | | * 获得缓存的list对象 |
| | | * |
| | | * @param key 缓存的键值 |
| | | * @return 缓存键值对应的数据 |
| | | */ |
| | | public <T> List<T> getCacheList(final String key) { |
| | | return redisTemplate.opsForList().range(key, 0, -1); |
| | | } |
| | | |
| | | /** |
| | | * 缓存Set |
| | | * |
| | | * @param key 缓存键值 |
| | | * @param dataSet 缓存的数据 |
| | | * @return 缓存数据的对象 |
| | | */ |
| | | public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet) { |
| | | BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key); |
| | | Iterator<T> it = dataSet.iterator(); |
| | | while (it.hasNext()) { |
| | | setOperation.add(it.next()); |
| | | } |
| | | return setOperation; |
| | | } |
| | | |
| | | /** |
| | | * 获得缓存的set |
| | | * |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public <T> Set<T> getCacheSet(final String key) { |
| | | return redisTemplate.opsForSet().members(key); |
| | | } |
| | | |
| | | /** |
| | | * 缓存Map |
| | | * |
| | | * @param key |
| | | * @param dataMap |
| | | */ |
| | | public <T> void setCacheMap(final String key, final Map<String, T> dataMap) { |
| | | if (dataMap != null) { |
| | | redisTemplate.opsForHash().putAll(key, dataMap); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获得缓存的Map |
| | | * |
| | | * @param key |
| | | * @return |
| | | */ |
| | | public <T> Map<String, T> getCacheMap(final String key) { |
| | | return redisTemplate.opsForHash().entries(key); |
| | | } |
| | | |
| | | /** |
| | | * 往Hash中存入数据 |
| | | * |
| | | * @param key Redis键 |
| | | * @param hKey Hash键 |
| | | * @param value 值 |
| | | */ |
| | | public <T> void setCacheMapValue(final String key, final String hKey, final T value) { |
| | | redisTemplate.opsForHash().put(key, hKey, value); |
| | | } |
| | | |
| | | /** |
| | | * 获取Hash中的数据 |
| | | * |
| | | * @param key Redis键 |
| | | * @param hKey Hash键 |
| | | * @return Hash中的对象 |
| | | */ |
| | | public <T> T getCacheMapValue(final String key, final String hKey) { |
| | | HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash(); |
| | | return opsForHash.get(key, hKey); |
| | | } |
| | | |
| | | /** |
| | | * 删除Hash中的数据 |
| | | * |
| | | * @param key |
| | | * @param hkey |
| | | */ |
| | | public void delCacheMapValue(final String key, final String hkey) { |
| | | HashOperations hashOperations = redisTemplate.opsForHash(); |
| | | hashOperations.delete(key, hkey); |
| | | } |
| | | |
| | | /** |
| | | * 获取多个Hash中的数据 |
| | | * |
| | | * @param key Redis键 |
| | | * @param hKeys Hash键集合 |
| | | * @return Hash对象集合 |
| | | */ |
| | | public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys) { |
| | | return redisTemplate.opsForHash().multiGet(key, hKeys); |
| | | } |
| | | |
| | | /** |
| | | * 获得缓存的基本对象列表 |
| | | * |
| | | * @param pattern 字符串前缀 |
| | | * @return 对象列表 |
| | | */ |
| | | public Collection<String> keys(final String pattern) { |
| | | return redisTemplate.keys(pattern); |
| | | } |
| | | } |
New file |
| | |
| | | package com.mes.common.utils; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | |
| | | /** |
| | | * @Author : zhoush |
| | | * @Date: 2024/4/9 19:16 |
| | | * @Description: |
| | | */ |
| | | public class WebUtils { |
| | | /** |
| | | * 将字符串渲染到客户端 |
| | | * |
| | | * @param response 渲染对象 |
| | | * @param string 待渲染的字符串 |
| | | * @return null |
| | | */ |
| | | public static String renderString(HttpServletResponse response, String string) { |
| | | try { |
| | | response.setStatus(200); |
| | | response.setContentType("application/json"); |
| | | response.setCharacterEncoding("utf-8"); |
| | | response.getWriter().print(string); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | } |
New file |
| | |
| | | package com.mes.menu.controller; |
| | | |
| | | |
| | | import com.mes.menu.entity.SysMenu; |
| | | import com.mes.menu.service.SysMenuService; |
| | | import com.mes.utils.Result; |
| | | import io.swagger.annotations.ApiModel; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * <p> |
| | | * 菜单表 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | @ApiModel("获取菜单信息") |
| | | @RestController |
| | | @RequestMapping("/menu/sysMenu") |
| | | public class SysMenuController { |
| | | |
| | | @Autowired |
| | | SysMenuService sysMenuService; |
| | | |
| | | @ApiOperation("获取用户有权限的所有菜单") |
| | | @GetMapping("/list") |
| | | public Result<List<SysMenu>> getMenuTree() { |
| | | return Result.success(sysMenuService.getMenuTree()); |
| | | } |
| | | |
| | | } |
| | | |
New file |
| | |
| | | package com.mes.menu.entity; |
| | | |
| | | import com.baomidou.mybatisplus.annotation.IdType; |
| | | import com.baomidou.mybatisplus.annotation.TableField; |
| | | import com.baomidou.mybatisplus.annotation.TableId; |
| | | import lombok.Data; |
| | | import lombok.EqualsAndHashCode; |
| | | import lombok.experimental.Accessors; |
| | | |
| | | import java.io.Serializable; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * <p> |
| | | * |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-13 |
| | | */ |
| | | @Data |
| | | @EqualsAndHashCode(callSuper = false) |
| | | @Accessors(chain = true) |
| | | public class SysMenu implements Serializable { |
| | | |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** |
| | | * 自增id |
| | | */ |
| | | @TableId(value = "id", type = IdType.AUTO) |
| | | private Integer id; |
| | | |
| | | /** |
| | | * 父id |
| | | */ |
| | | private Integer parentId; |
| | | |
| | | /** |
| | | * 模块名称 |
| | | */ |
| | | private String menuName; |
| | | |
| | | /** |
| | | * 图标 |
| | | */ |
| | | private String icon; |
| | | |
| | | /** |
| | | * 地址 |
| | | */ |
| | | private String url; |
| | | |
| | | /** |
| | | * 语言类型 |
| | | */ |
| | | private String languageType; |
| | | |
| | | /** |
| | | * 状态 |
| | | */ |
| | | private Integer status; |
| | | |
| | | /** |
| | | * 排序 |
| | | */ |
| | | private String listSort; |
| | | |
| | | /** |
| | | * 创建时间 |
| | | */ |
| | | private Date createTime; |
| | | |
| | | /** |
| | | * 子菜单 |
| | | */ |
| | | @TableField(exist = false) |
| | | private List<SysMenu> children; |
| | | |
| | | |
| | | } |
New file |
| | |
| | | package com.mes.menu.mapper; |
| | | |
| | | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import com.mes.menu.entity.SysMenu; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * <p> |
| | | * 菜单表 Mapper 接口 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | public interface SysMenuMapper extends BaseMapper<SysMenu> { |
| | | |
| | | List<String> selectPermsByUserId(long parseLong); |
| | | } |
New file |
| | |
| | | package com.mes.menu.service; |
| | | |
| | | import com.baomidou.mybatisplus.extension.service.IService; |
| | | import com.mes.menu.entity.SysMenu; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * <p> |
| | | * 菜单表 服务类 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | public interface SysMenuService extends IService<SysMenu> { |
| | | |
| | | List<SysMenu> getMenuTree(); |
| | | } |
New file |
| | |
| | | package com.mes.menu.service.impl; |
| | | |
| | | import cn.hutool.json.JSONUtil; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.mes.menu.entity.SysMenu; |
| | | import com.mes.menu.mapper.SysMenuMapper; |
| | | import com.mes.menu.service.SysMenuService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <p> |
| | | * 菜单表 服务实现类 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | @Service |
| | | @Slf4j |
| | | public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService { |
| | | |
| | | @Override |
| | | public List<SysMenu> getMenuTree() { |
| | | List<SysMenu> menuList = this.baseMapper.selectList(null); |
| | | return create(menuList); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 将数据库中查询出来的list集合传入此方法即可获得排成树形结构的list集合 |
| | | * |
| | | * @param lists |
| | | * @return |
| | | */ |
| | | public List<SysMenu> create(List<SysMenu> lists) { |
| | | List<SysMenu> deptTreeList = lists.stream() |
| | | .filter(item -> item.getParentId() == 0) |
| | | .map(item -> { |
| | | item.setChildren(getChildren(item, lists)); |
| | | return item; |
| | | }).collect(Collectors.toList()); |
| | | return deptTreeList; |
| | | } |
| | | |
| | | /** |
| | | * 此方法将被递归调用 |
| | | * |
| | | * @param menu |
| | | * @param menus |
| | | * @return |
| | | */ |
| | | private List<SysMenu> getChildren(SysMenu menu, List<SysMenu> menus) { |
| | | List<SysMenu> res = menus.stream() |
| | | .filter(item -> item.getParentId().equals(menu.getId())) |
| | | .map(item -> { |
| | | item.setChildren(getChildren(item, menus)); |
| | | return item; |
| | | }).collect(Collectors.toList()); |
| | | log.info("菜单树:{}", JSONUtil.toJsonStr(res)); |
| | | return res; |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.mes.role.controller; |
| | | |
| | | |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | /** |
| | | * <p> |
| | | * 角色表 前端控制器 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | @RestController |
| | | @RequestMapping("/role/sys-role") |
| | | public class SysRoleController { |
| | | |
| | | } |
| | | |
New file |
| | |
| | | package com.mes.role.controller; |
| | | |
| | | |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | /** |
| | | * <p> |
| | | * 前端控制器 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | @RestController |
| | | @RequestMapping("/role/sys-role-menu") |
| | | public class SysRoleMenuController { |
| | | |
| | | } |
| | | |
New file |
| | |
| | | package com.mes.role.entity; |
| | | |
| | | import com.baomidou.mybatisplus.annotation.IdType; |
| | | import com.baomidou.mybatisplus.annotation.TableId; |
| | | import lombok.Data; |
| | | import lombok.EqualsAndHashCode; |
| | | |
| | | import java.io.Serializable; |
| | | |
| | | /** |
| | | * <p> |
| | | * 角色表 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | @Data |
| | | @EqualsAndHashCode(callSuper = false) |
| | | public class SysRole implements Serializable { |
| | | |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | @TableId(value = "id", type = IdType.AUTO) |
| | | private Long id; |
| | | |
| | | private String name; |
| | | |
| | | /** |
| | | * 角色权限字符串 |
| | | */ |
| | | private String roleKey; |
| | | |
| | | /** |
| | | * 角色状态(0正常 1停用) |
| | | */ |
| | | private String status; |
| | | |
| | | /** |
| | | * 删除标志 |
| | | */ |
| | | private Integer delFlag; |
| | | |
| | | /** |
| | | * 备注 |
| | | */ |
| | | private String remark; |
| | | |
| | | |
| | | } |
New file |
| | |
| | | package com.mes.role.entity; |
| | | |
| | | import com.baomidou.mybatisplus.annotation.IdType; |
| | | import com.baomidou.mybatisplus.annotation.TableId; |
| | | import lombok.Data; |
| | | import lombok.EqualsAndHashCode; |
| | | |
| | | import java.io.Serializable; |
| | | |
| | | /** |
| | | * <p> |
| | | * |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | @Data |
| | | @EqualsAndHashCode(callSuper = false) |
| | | public class SysRoleMenu implements Serializable { |
| | | |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** |
| | | * 角色ID |
| | | */ |
| | | @TableId(value = "role_id", type = IdType.AUTO) |
| | | private Long roleId; |
| | | |
| | | /** |
| | | * 菜单id |
| | | */ |
| | | private Long menuId; |
| | | |
| | | |
| | | } |
New file |
| | |
| | | package com.mes.role.mapper; |
| | | |
| | | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import com.mes.role.entity.SysRole; |
| | | |
| | | /** |
| | | * <p> |
| | | * 角色表 Mapper 接口 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | public interface SysRoleMapper extends BaseMapper<SysRole> { |
| | | |
| | | } |
New file |
| | |
| | | package com.mes.role.mapper; |
| | | |
| | | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import com.mes.role.entity.SysRoleMenu; |
| | | |
| | | /** |
| | | * <p> |
| | | * Mapper 接口 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | public interface SysRoleMenuMapper extends BaseMapper<SysRoleMenu> { |
| | | |
| | | } |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
| | | <mapper namespace="com.mes.role.mapper.SysRoleMapper"> |
| | | |
| | | </mapper> |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
| | | <mapper namespace="com.mes.role.mapper.SysRoleMenuMapper"> |
| | | |
| | | </mapper> |
New file |
| | |
| | | package com.mes.role.service; |
| | | |
| | | import com.baomidou.mybatisplus.extension.service.IService; |
| | | import com.mes.role.entity.SysRoleMenu; |
| | | |
| | | /** |
| | | * <p> |
| | | * 服务类 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | public interface SysRoleMenuService extends IService<SysRoleMenu> { |
| | | |
| | | } |
New file |
| | |
| | | package com.mes.role.service; |
| | | |
| | | import com.baomidou.mybatisplus.extension.service.IService; |
| | | import com.mes.role.entity.SysRole; |
| | | |
| | | /** |
| | | * <p> |
| | | * 角色表 服务类 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | public interface SysRoleService extends IService<SysRole> { |
| | | |
| | | } |
New file |
| | |
| | | package com.mes.role.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.mes.role.entity.SysRoleMenu; |
| | | import com.mes.role.mapper.SysRoleMenuMapper; |
| | | import com.mes.role.service.SysRoleMenuService; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | /** |
| | | * <p> |
| | | * 服务实现类 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | @Service |
| | | public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService { |
| | | |
| | | } |
New file |
| | |
| | | package com.mes.role.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.mes.role.entity.SysRole; |
| | | import com.mes.role.mapper.SysRoleMapper; |
| | | import com.mes.role.service.SysRoleService; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | /** |
| | | * <p> |
| | | * 角色表 服务实现类 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | @Service |
| | | public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService { |
| | | |
| | | } |
New file |
| | |
| | | package com.mes.userinfo.controller; |
| | | |
| | | |
| | | import com.mes.userinfo.entity.SysUser; |
| | | import com.mes.userinfo.service.SysUserService; |
| | | import com.mes.utils.Result; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | /** |
| | | * <p> |
| | | * 用户表 前端控制器 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | @RestController |
| | | @CrossOrigin |
| | | @RequestMapping("/userinfo") |
| | | public class SysUserController { |
| | | |
| | | @Autowired |
| | | private SysUserService sysUserService; |
| | | |
| | | @PostMapping("/login") |
| | | public Result login(@RequestBody SysUser user) { |
| | | return sysUserService.login(user); |
| | | } |
| | | |
| | | @GetMapping("/hello") |
| | | public Result hello() { |
| | | return Result.success("eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJlZjMyMjQ4NDcyODE0ZWFlYWRlOTBkYmZjYWFlZmNmZSIsInN1YiI6IjEiLCJpc3MiOiJzZyIsImlhdCI6MTcxMjkwMjI0NCwiZXhwIjoxNzEyOTA1ODQ0fQ.DlJkhkiwjZSqprdLzKaTB3yuyxGukerKeF0FbJK_6HY"); |
| | | } |
| | | } |
| | | |
New file |
| | |
| | | package com.mes.userinfo.controller; |
| | | |
| | | |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | /** |
| | | * <p> |
| | | * 前端控制器 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | @RestController |
| | | @RequestMapping("/userinfo/sys-user-role") |
| | | public class SysUserRoleController { |
| | | |
| | | } |
| | | |
New file |
| | |
| | | package com.mes.userinfo.entity; |
| | | |
| | | import com.alibaba.fastjson.annotation.JSONField; |
| | | import org.springframework.security.core.GrantedAuthority; |
| | | import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| | | import org.springframework.security.core.userdetails.UserDetails; |
| | | |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @Author : zhoush |
| | | * @Date: 2024/4/11 15:46 |
| | | * @Description: |
| | | */ |
| | | public class LoginUser implements UserDetails { |
| | | |
| | | |
| | | private SysUser user; |
| | | |
| | | /** |
| | | * 存储权限信息 |
| | | */ |
| | | @JSONField(serialize = false) |
| | | private List<String> permissions; |
| | | |
| | | /** |
| | | * 存储SpringSecurity调用getAuthorities()方法获取的权限信息的集合 |
| | | */ |
| | | @JSONField(serialize = false) |
| | | private List<GrantedAuthority> authorities; |
| | | |
| | | |
| | | public LoginUser() { |
| | | } |
| | | |
| | | public LoginUser(SysUser user, List<String> permissions) { |
| | | this.user = user; |
| | | this.permissions = permissions; |
| | | } |
| | | |
| | | public SysUser getUser() { |
| | | return user; |
| | | } |
| | | |
| | | public void setUser(SysUser user) { |
| | | this.user = user; |
| | | } |
| | | |
| | | @Override |
| | | public Collection<? extends GrantedAuthority> getAuthorities() { |
| | | |
| | | // 优化为只需要第一次获取的时候进行遍历,后面再用就会从authorities这个成员变量中获取了,不会再进行Stream循环遍历了 |
| | | if (authorities != null) { |
| | | return authorities; |
| | | } |
| | | //把permissions中字符串类型的权限信息转换成GrantedAuthority对象存入authorities中 |
| | | authorities = permissions.stream(). |
| | | map(SimpleGrantedAuthority::new) |
| | | .collect(Collectors.toList()); |
| | | return authorities; |
| | | } |
| | | |
| | | @Override |
| | | public String getPassword() { |
| | | return user.getPassword(); |
| | | } |
| | | |
| | | @Override |
| | | public String getUsername() { |
| | | return user.getUserName(); |
| | | } |
| | | |
| | | @Override |
| | | public boolean isAccountNonExpired() { |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public boolean isAccountNonLocked() { |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public boolean isCredentialsNonExpired() { |
| | | return true; |
| | | } |
| | | |
| | | @Override |
| | | public boolean isEnabled() { |
| | | return true; |
| | | } |
| | | } |
New file |
| | |
| | | package com.mes.userinfo.entity; |
| | | |
| | | import com.baomidou.mybatisplus.annotation.IdType; |
| | | import com.baomidou.mybatisplus.annotation.TableId; |
| | | import lombok.Data; |
| | | import lombok.EqualsAndHashCode; |
| | | |
| | | import java.io.Serializable; |
| | | |
| | | /** |
| | | * <p> |
| | | * 用户表 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | @Data |
| | | @EqualsAndHashCode(callSuper = false) |
| | | public class SysUser implements Serializable { |
| | | |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** |
| | | * 主键 |
| | | */ |
| | | @TableId(value = "id", type = IdType.AUTO) |
| | | private Long id; |
| | | |
| | | /** |
| | | * 用户名 |
| | | */ |
| | | private String userName; |
| | | |
| | | /** |
| | | * 昵称 |
| | | */ |
| | | private String nickName; |
| | | |
| | | /** |
| | | * 密码 |
| | | */ |
| | | private String password; |
| | | |
| | | /** |
| | | * 账号状态(0正常 1停用) |
| | | */ |
| | | private String status; |
| | | |
| | | /** |
| | | * 头像 |
| | | */ |
| | | private String avatar; |
| | | |
| | | /** |
| | | * 用户类型(0管理员,1普通用户) |
| | | */ |
| | | private String userType; |
| | | |
| | | /** |
| | | * 删除标志 |
| | | */ |
| | | private Integer delFlag; |
| | | |
| | | |
| | | } |
New file |
| | |
| | | package com.mes.userinfo.entity; |
| | | |
| | | import com.baomidou.mybatisplus.annotation.IdType; |
| | | import com.baomidou.mybatisplus.annotation.TableId; |
| | | import lombok.Data; |
| | | import lombok.EqualsAndHashCode; |
| | | |
| | | import java.io.Serializable; |
| | | |
| | | /** |
| | | * <p> |
| | | * |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | @Data |
| | | @EqualsAndHashCode(callSuper = false) |
| | | public class SysUserRole implements Serializable { |
| | | |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | /** |
| | | * 用户id |
| | | */ |
| | | @TableId(value = "user_id", type = IdType.AUTO) |
| | | private Long userId; |
| | | |
| | | /** |
| | | * 角色id |
| | | */ |
| | | private Long roleId; |
| | | |
| | | |
| | | } |
New file |
| | |
| | | package com.mes.userinfo.mapper; |
| | | |
| | | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import com.mes.userinfo.entity.SysUser; |
| | | |
| | | /** |
| | | * <p> |
| | | * 用户表 Mapper 接口 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | public interface SysUserMapper extends BaseMapper<SysUser> { |
| | | |
| | | } |
New file |
| | |
| | | package com.mes.userinfo.mapper; |
| | | |
| | | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import com.mes.userinfo.entity.SysUserRole; |
| | | |
| | | /** |
| | | * <p> |
| | | * Mapper 接口 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | public interface SysUserRoleMapper extends BaseMapper<SysUserRole> { |
| | | |
| | | } |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
| | | <mapper namespace="com.mes.userinfo.mapper.SysUserMapper"> |
| | | |
| | | </mapper> |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
| | | <mapper namespace="com.mes.userinfo.mapper.SysUserRoleMapper"> |
| | | |
| | | </mapper> |
New file |
| | |
| | | package com.mes.userinfo.service; |
| | | |
| | | import com.baomidou.mybatisplus.extension.service.IService; |
| | | import com.mes.userinfo.entity.SysUserRole; |
| | | |
| | | /** |
| | | * <p> |
| | | * 服务类 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | public interface SysUserRoleService extends IService<SysUserRole> { |
| | | |
| | | } |
New file |
| | |
| | | package com.mes.userinfo.service; |
| | | |
| | | import com.baomidou.mybatisplus.extension.service.IService; |
| | | import com.mes.userinfo.entity.SysUser; |
| | | import com.mes.utils.Result; |
| | | |
| | | /** |
| | | * <p> |
| | | * 用户表 服务类 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | public interface SysUserService extends IService<SysUser> { |
| | | |
| | | Result login(SysUser user); |
| | | |
| | | Result logout(); |
| | | } |
New file |
| | |
| | | package com.mes.userinfo.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.mes.userinfo.entity.SysUserRole; |
| | | import com.mes.userinfo.mapper.SysUserRoleMapper; |
| | | import com.mes.userinfo.service.SysUserRoleService; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | /** |
| | | * <p> |
| | | * 服务实现类 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | @Service |
| | | public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService { |
| | | |
| | | } |
New file |
| | |
| | | package com.mes.userinfo.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.mes.common.utils.JwtUtil; |
| | | import com.mes.common.utils.RedisUtil; |
| | | import com.mes.menu.mapper.SysMenuMapper; |
| | | import com.mes.userinfo.entity.LoginUser; |
| | | import com.mes.userinfo.entity.SysUser; |
| | | import com.mes.userinfo.mapper.SysUserMapper; |
| | | import com.mes.userinfo.service.SysUserService; |
| | | import com.mes.utils.Result; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.security.authentication.AuthenticationManager; |
| | | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| | | import org.springframework.security.core.Authentication; |
| | | import org.springframework.security.core.context.SecurityContextHolder; |
| | | import org.springframework.security.core.userdetails.UserDetails; |
| | | import org.springframework.security.core.userdetails.UserDetailsService; |
| | | import org.springframework.security.core.userdetails.UsernameNotFoundException; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * <p> |
| | | * 用户表 服务实现类 |
| | | * </p> |
| | | * |
| | | * @author zhoush |
| | | * @since 2024-04-11 |
| | | */ |
| | | @Service |
| | | public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService, UserDetailsService { |
| | | |
| | | @Autowired |
| | | private AuthenticationManager authenticationManager; |
| | | @Autowired |
| | | private RedisUtil redisUtil; |
| | | |
| | | @Resource |
| | | private SysMenuMapper sysMenuMapper; |
| | | |
| | | @Override |
| | | public Result login(SysUser user) { |
| | | UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user.getUserName(), user.getPassword()); |
| | | Authentication authenticate = authenticationManager.authenticate(authenticationToken); |
| | | if (Objects.isNull(authenticate)) { |
| | | throw new RuntimeException("用户名或密码错误"); |
| | | } |
| | | //使用userid生成token |
| | | LoginUser loginUser = (LoginUser) authenticate.getPrincipal(); |
| | | String userId = loginUser.getUser().getId().toString(); |
| | | String jwt = JwtUtil.createJWT(userId); |
| | | //authenticate存入redis |
| | | redisUtil.setCacheObject("login:" + userId, loginUser); |
| | | //把token响应给前端 |
| | | HashMap<String, String> map = new HashMap<>(); |
| | | map.put("token", jwt); |
| | | return Result.success(map); |
| | | } |
| | | |
| | | @Override |
| | | public Result logout() { |
| | | Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); |
| | | LoginUser loginUser = (LoginUser) authentication.getPrincipal(); |
| | | Long userid = loginUser.getUser().getId(); |
| | | redisUtil.deleteObject("login:" + userid); |
| | | return Result.success("退出成功"); |
| | | } |
| | | |
| | | @Override |
| | | public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { |
| | | LambdaQueryWrapper<SysUser> lqw = new LambdaQueryWrapper<>(); |
| | | lqw.eq(SysUser::getUserName, username); |
| | | SysUser user = this.baseMapper.selectOne(lqw); |
| | | //判断是否为空 |
| | | if (Objects.isNull(user)) { |
| | | throw new RuntimeException("用户名或密码错误"); |
| | | } |
| | | //查询权限信息 |
| | | List<String> perms = sysMenuMapper.selectPermsByUserId(user.getId()); |
| | | |
| | | return new LoginUser(user, perms); |
| | | } |
| | | } |
New file |
| | |
| | | server: |
| | | port: 8089 |
| | | spring: |
| | | datasource: |
| | | driver-class-name: com.mysql.cj.jdbc.Driver |
| | | url: jdbc:mysql://10.153.19.150:3306/hangzhoumes?serverTimezone=GMT%2b8&characterEncoding=utf-8&useSSL=false |
| | | username: root |
| | | password: beibo.123/ |
| | | cloud: |
| | | nacos: |
| | | discovery: |
| | | server-addr: 10.153.19.150:8848 |
| | | application: |
| | | name: security |
| | | redis: |
| | | database: 0 |
| | | host: 10.153.19.150 |
| | | port: 6379 |
| | | password: |
| | | session: |
| | | store-type: redis |
| | | mybatis-plus: |
| | | mapper-locations: classpath*:mapper/*.xml |
| | | configuration: |
| | | log-impl: org.apache.ibatis.logging.stdout.StdOutImpl |
New file |
| | |
| | | <?xml version="1.0" encoding="UTF-8"?> |
| | | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
| | | <mapper namespace="com.mes.menu.mapper.SysMenuMapper"> |
| | | |
| | | <select id="selectPermsByUserId" resultType="java.lang.String"> |
| | | SELECT DISTINCT m.perms |
| | | FROM sys_user_role ur |
| | | LEFT JOIN sys_role r ON ur.role_id = r.id |
| | | LEFT JOIN sys_role_menu rm ON ur.role_id = rm.role_id |
| | | LEFT JOIN sys_menu m ON m.id = rm.menu_id |
| | | WHERE user_id = #{userid} |
| | | AND r.status = 0 |
| | | AND m.status = 0 |
| | | </select> |
| | | </mapper> |
| | |
| | | package com.mes.temperingglass.service; |
| | | import com.mes.temperingglass.entity.GlassInfo; |
| | | //import com.mes.temperingglass.entity.GlassInfo; |
| | | |
| | | import com.mes.temperingglass.entity.TemperingGlassInfo; |
| | | import com.mes.tools.S7control; |
| | | import com.mes.temperingglass.mapper.TemperingMapper; |
| | | import java.util.List; |
| | | import com.mes.tools.S7control; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.List; |
| | | |
| | | @Service |
| | | public class TemperingService { |
| | | |
| | | |
| | | private final TemperingMapper temperingMapper; |
| | | private S7control s7control; |
| | | |
| | | public TemperingService(TemperingMapper temperingMapper) { |
| | | this.temperingMapper =temperingMapper; |
| | | this.temperingMapper = temperingMapper; |
| | | } |
| | | //接收id返回坐标和数据 |
| | | public GlassInfo SelectOutGlass (String glassid) { |
| | | GlassInfo GlassInfo = temperingMapper.SelectGlass(glassid); |
| | | return GlassInfo; |
| | | } |
| | | |
| | | // //接收id返回坐标和数据 |
| | | // public GlassInfo SelectOutGlass (String glassid) { |
| | | // GlassInfo GlassInfo = temperingMapper.SelectGlass(glassid); |
| | | // return GlassInfo; |
| | | // } |
| | | //发送坐标信息 |
| | | public void SendCoordinate (short glassX,short glassY) { |
| | | public void SendCoordinate(short glassX, short glassY) { |
| | | s7control.WriteWord("DB100.10", glassX); |
| | | s7control.WriteWord("DB100.10", glassY); |
| | | } |
| | | |
| | | //判断是否可以发送进炉信号 |
| | | public int SelectTempering (String glassid) { |
| | | public int SelectTempering(String glassid) { |
| | | int tempering = temperingMapper.SelectTempering(glassid); |
| | | if (tempering == 1) { |
| | | return 1; |
| | |
| | | package com.mes.config; |
| | | |
| | | import com.mes.common.PLCAutoMes; |
| | | import com.mes.common.Plchome; |
| | | |
| | | import com.mes.device.PLCAutoMes; |
| | | import org.springframework.boot.ApplicationArguments; |
| | | import org.springframework.boot.ApplicationRunner; |
| | | import org.springframework.core.annotation.Order; |
| | |
| | | import com.mes.downglassinfo.entity.DownGlassInfo; |
| | | import com.mes.downworkstation.entity.DownWorkstation; |
| | | import org.apache.ibatis.annotations.*; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.stereotype.Repository; |
| | | |
| | | import java.util.List; |
| | |
| | | @Mapper |
| | | @Repository |
| | | @InterceptorIgnore(tenantLine = "true") |
| | | @DS("hangzhoumes") // 指定使用 hangzhoumes 数据源 |
| | | @DS("salve_1") // 指定使用 hangzhoumes 数据源 |
| | | public interface DownWorkstationMapper extends BaseMapper<DownWorkstation> { |
| | | |
| | | |
| | |
| | | package com.mes.downworkstation.service;
|
| | |
|
| | | import com.mes.common.PLCAutoMes;
|
| | | import com.mes.device.PLCAutoMes;
|
| | | import com.mes.device.PlcParameterObject;
|
| | | import com.mes.downglassinfo.entity.DownGlassInfo;
|
| | | import com.mes.downglassinfo.mapper.DownGlassInfoMapper;
|
| | | import com.mes.downglassinfo.service.DownGlassInfoService;
|
| | | import com.mes.downstorage.entity.DownStorageCageDetails;
|
| | | import com.mes.downworkstation.entity.DownWorkstation;
|
| | | import com.mes.device.PlcParameterObject;
|
| | | import com.mes.downglassinfo.mapper.DownGlassInfoMapper;
|
| | | import com.mes.downworkstation.mapper.DownWorkstationMapper;
|
| | | import com.mes.tools.WebSocketServer;
|
| | | import lombok.Data;
|
| | |
| | | <version>1.0-SNAPSHOT</version> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <artifactId>servicebase</artifactId> |
| | | <groupId>com.mes</groupId> |
| | | <version>1.0-SNAPSHOT</version> |
| | | </dependency> |
| | | <dependency> |
| | | <artifactId>springsecurity</artifactId> |
| | | <groupId>com.mes</groupId> |
| | | <version>1.0-SNAPSHOT</version> |
| | | </dependency> |
| | | |
| | | <!--开发者工具--> |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |