common模块:
增加 AESUtils MD5Utils ObjectUtils TokenUtils
manage模块:
完整用户登陆功能
8 files added
21 files modified
| | |
| | | <artifactId>mysql-connector-java</artifactId> |
| | | <scope>runtime</scope> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.springframework.boot</groupId> |
| | | <artifactId>spring-boot-starter-test</artifactId> |
| | | <scope>test</scope> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | | |
| | |
| | | <artifactId>screen-common</artifactId> |
| | | |
| | | <properties> |
| | | |
| | | <commons-net>3.6</commons-net> |
| | | <spring-boot-starter-data-redis.version>2.1.3.RELEASE</spring-boot-starter-data-redis.version> |
| | | <jedis.version>2.9.0</jedis.version> |
| | | </properties> |
| | |
| | | <groupId>com.alibaba</groupId> |
| | | <artifactId>fastjson</artifactId> |
| | | <version>${com.alibaba.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>commons-net</groupId> |
| | | <artifactId>commons-net</artifactId> |
| | | <version>3.6</version> |
| | | </dependency> |
| | | </dependencies> |
| | | |
| | |
| | | <artifactId>maven-shared-incremental</artifactId> |
| | | <version>${maven-shared-incremental.version}</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>commons-net</groupId> |
| | | <artifactId>commons-net</artifactId> |
| | | <version>${commons-net}</version> |
| | | </dependency> |
| | | </dependencies> |
| | | </plugin> |
| | | |
New file |
| | |
| | | package com.moral.util; |
| | | |
| | | import javax.crypto.Cipher; |
| | | import javax.crypto.spec.IvParameterSpec; |
| | | import javax.crypto.spec.SecretKeySpec; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.net.util.Base64; |
| | | |
| | | /** |
| | | * @ClassName AESUtil |
| | | * @Description AES工具类 |
| | | * @Author 陈凯裕 |
| | | * @Date 2021/3/9 14:25 |
| | | * @Version TODO |
| | | **/ |
| | | @Slf4j |
| | | public class AESUtils { |
| | | //密钥 |
| | | public static String key = "AD42F7787B035B7580000EF93BE20BAD"; |
| | | //字符集 |
| | | private static String charset = "utf-8"; |
| | | // 偏移量 |
| | | private static int offset = 16; |
| | | //AES种类 |
| | | private static String transformation = "AES/CBC/PKCS5Padding"; |
| | | private static String algorithm = "AES"; |
| | | |
| | | //加密 |
| | | public static String encrypt(String content) { |
| | | return encrypt(content, key); |
| | | } |
| | | |
| | | //解密 |
| | | public static String decrypt(String content) { |
| | | return decrypt(content, key); |
| | | } |
| | | |
| | | //加密 |
| | | public static String encrypt(String content, String key) { |
| | | try { |
| | | SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm); |
| | | IvParameterSpec iv = new IvParameterSpec(key.getBytes(), 0, offset); |
| | | Cipher cipher = Cipher.getInstance(transformation); |
| | | byte[] byteContent = content.getBytes(charset); |
| | | cipher.init(Cipher.ENCRYPT_MODE, skey, iv);// 初始化 |
| | | byte[] result = cipher.doFinal(byteContent); |
| | | return new Base64().encodeToString(result); // 加密 |
| | | } catch (Exception e) { |
| | | log.error("Encryption failed!"); |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | //解密 |
| | | public static String decrypt(String content, String key) { |
| | | try { |
| | | SecretKeySpec skey = new SecretKeySpec(key.getBytes(), algorithm); |
| | | IvParameterSpec iv = new IvParameterSpec(key.getBytes(), 0, offset); |
| | | Cipher cipher = Cipher.getInstance(transformation); |
| | | cipher.init(Cipher.DECRYPT_MODE, skey, iv);// 初始化 |
| | | byte[] result = cipher.doFinal(new Base64().decode(content)); |
| | | return new String(result); // 解密 |
| | | } catch (Exception e) { |
| | | log.error("Decryption failed!"); |
| | | e.printStackTrace(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | System.out.println(encrypt("123456").length()); |
| | | System.out.println(encrypt("chenkaiyu111").length()); |
| | | System.out.println(encrypt("ASDKJLFHDLSKJ").length()); |
| | | System.out.println(encrypt("chenkaiiiiiiiiiiiii").length()); |
| | | } |
| | | |
| | | } |
| | |
| | | /** DateFormat缓存 */ |
| | | private static Map<String, DateFormat> dateFormatMap = new HashMap<String, DateFormat>(); |
| | | |
| | | |
| | | |
| | | /** |
| | | * 获取DateFormat |
| | | * |
| | |
| | | |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | Date newdate = new Date(); |
| | | SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd"); |
| | | String date = dft.format(newdate); |
| | | System.out.println(date); |
| | | // return endDate; |
| | | public static void main(String[] args) throws InterruptedException { |
| | | Date date1 = new Date(); |
| | | Thread.sleep(1000); |
| | | Date date2 = new Date(); |
| | | System.out.println(DateUtils.compareDateStr(date1,date2)); |
| | | } |
| | | } |
New file |
| | |
| | | package com.moral.util; |
| | | |
| | | import java.security.MessageDigest; |
| | | import java.util.Random; |
| | | |
| | | |
| | | public class MD5Utils { |
| | | |
| | | private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; |
| | | |
| | | public static String MD5(String s) { |
| | | char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; |
| | | |
| | | try { |
| | | byte[] btInput = s.getBytes(); |
| | | // 获得MD5摘要算法的 MessageDigest 对象 |
| | | MessageDigest mdInst = MessageDigest.getInstance("MD5"); |
| | | // 使用指定的字节更新摘要 |
| | | mdInst.update(btInput); |
| | | // 获得密文 |
| | | byte[] md = mdInst.digest(); |
| | | // 把密文转换成十六进制的字符串形式 |
| | | int j = md.length; |
| | | char str[] = new char[j * 2]; |
| | | int k = 0; |
| | | for (int i = 0; i < j; i++) { |
| | | byte byte0 = md[i]; |
| | | str[k++] = hexDigits[byte0 >>> 4 & 0xf]; |
| | | str[k++] = hexDigits[byte0 & 0xf]; |
| | | } |
| | | return new String(str); |
| | | } catch (Exception e) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 加盐MD5 |
| | | */ |
| | | public static String saltMD5(String password) { |
| | | String salt = genSalt(); |
| | | password = MD5(password + salt); |
| | | char[] cs = new char[48]; |
| | | for (int i = 0; i < 48; i += 3) { |
| | | cs[i] = password.charAt(i / 3 * 2); |
| | | char c = salt.charAt(i / 3); |
| | | cs[i + 1] = c; |
| | | cs[i + 2] = password.charAt(i / 3 * 2 + 1); |
| | | } |
| | | return new String(cs); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @return 动态盐 |
| | | */ |
| | | private static String genSalt() { |
| | | Random r = new Random(); |
| | | StringBuilder sb = new StringBuilder(16); |
| | | sb.append(r.nextInt(99999999)).append(r.nextInt(99999999)); |
| | | int len = sb.length(); |
| | | if (len < 16) { |
| | | for (int i = 0; i < 16 - len; i++) { |
| | | sb.append("0"); |
| | | } |
| | | } |
| | | String salt = sb.toString(); |
| | | return salt; |
| | | } |
| | | |
| | | /** |
| | | * 校验 |
| | | */ |
| | | public static boolean saltMD5Verify(String password, String md5) { |
| | | char[] cs1 = new char[32]; |
| | | char[] cs2 = new char[16]; |
| | | for (int i = 0; i < 48; i += 3) { |
| | | cs1[i / 3 * 2] = md5.charAt(i); |
| | | cs1[i / 3 * 2 + 1] = md5.charAt(i + 2); |
| | | cs2[i / 3] = md5.charAt(i + 1); |
| | | } |
| | | String salt = new String(cs2); |
| | | return MD5(password + salt).equals(new String(cs1)); |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | System.out.println(saltMD5("123456")); |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.moral.util; |
| | | |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * @ClassName ObjectUtil |
| | | * @Description manage常用工具类 |
| | | * @Author 陈凯裕 |
| | | * @Date 2021/3/10 16:45 |
| | | * @Version TODO |
| | | **/ |
| | | public class ObjectUtils { |
| | | /** |
| | | * @Description: 判断map以及map中的Key是否完整 |
| | | * @Param: [map, keys] |
| | | * @return: boolean |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/10 |
| | | */ |
| | | public static boolean checkParamAndMap(Map<String,Object> map,String ... keys){ |
| | | if(null==map) |
| | | return false; |
| | | for (String key : keys) { |
| | | if(null==map.get(key)) |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | } |
New file |
| | |
| | | package com.moral.util; |
| | | |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | /** |
| | | * @ClassName TokenEncryptUtils |
| | | * @Description token加密、解密工具 |
| | | * @Author 陈凯裕 |
| | | * @Date 2021/3/10 11:50 |
| | | * @Version TODO |
| | | **/ |
| | | @Component |
| | | public class TokenEncryptUtils { |
| | | |
| | | private static String key; |
| | | |
| | | @Value("${TOKEN.KEY}") |
| | | public void setKey(String tokenKey){ |
| | | this.key = tokenKey; |
| | | } |
| | | |
| | | /** |
| | | * 加密 |
| | | * @param str |
| | | * @return |
| | | */ |
| | | public static String encoded(String str) { |
| | | return strToHex(encodedString(str, key)); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * @Description: 解密 |
| | | * @Param: [str] |
| | | * @return: java.lang.String |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/10 |
| | | */ |
| | | public static String decoded(String str) { |
| | | String hexStr = null; |
| | | try { |
| | | hexStr = hexStrToStr(str); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | if (hexStr != null) { |
| | | hexStr = encodedString(hexStr, key); |
| | | } |
| | | return hexStr; |
| | | } |
| | | |
| | | /** |
| | | * 转换 |
| | | * @param str |
| | | * @param password |
| | | * @return |
| | | */ |
| | | private static String encodedString(String str, String password) { |
| | | char[] pwd = password.toCharArray(); |
| | | int pwdLen = pwd.length; |
| | | |
| | | char[] strArray = str.toCharArray(); |
| | | for (int i=0; i<strArray.length; i++) { |
| | | strArray[i] = (char)(strArray[i] ^ pwd[i%pwdLen] ^ pwdLen); |
| | | } |
| | | return new String(strArray); |
| | | } |
| | | |
| | | /** |
| | | * @Description: 16进制转换字符串 |
| | | * @Param: [hexStr] |
| | | * @return: java.lang.String |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/10 |
| | | */ |
| | | private static String hexStrToStr(String hexStr) { |
| | | return new String(hexStrToBytes(hexStr)); |
| | | } |
| | | |
| | | /** |
| | | * @Description: 16进制字符串转换字节数组 |
| | | * @Param: [hexStr] |
| | | * @return: byte[] |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/10 |
| | | */ |
| | | private static byte[] hexStrToBytes(String hexStr) { |
| | | String hex; |
| | | int val; |
| | | byte[] btHexStr = new byte[hexStr.length()/2]; |
| | | for (int i=0; i<btHexStr.length; i++) { |
| | | hex = hexStr.substring(2*i, 2*i+2); |
| | | val = Integer.valueOf(hex, 16); |
| | | btHexStr[i] = (byte) val; |
| | | } |
| | | return btHexStr; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 字节数组转换16进制字符串 |
| | | * @Param: [bytesArray] |
| | | * @return: java.lang.String |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/10 |
| | | */ |
| | | private static String bytesToHexStr(byte[] bytesArray) { |
| | | StringBuilder builder = new StringBuilder(); |
| | | String hexStr; |
| | | for (byte bt : bytesArray) { |
| | | hexStr = Integer.toHexString(bt & 0xFF); |
| | | if (hexStr.length() == 1) { |
| | | builder.append("0"); |
| | | builder.append(hexStr); |
| | | }else{ |
| | | builder.append(hexStr); |
| | | } |
| | | } |
| | | return builder.toString(); |
| | | } |
| | | |
| | | /** |
| | | * @Description: 字符串转为16进制 |
| | | * @Param: [s] |
| | | * @return: java.lang.String |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/10 |
| | | */ |
| | | private static String strToHex(String s) { |
| | | return bytesToHexStr(s.getBytes()); |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | long currentTime = System.currentTimeMillis(); |
| | | System.out.println("加密前的token:"+"25."+currentTime); |
| | | String token = encoded("25" +"/"+ currentTime); |
| | | String[] decoded = decoded(token).split("/"); |
| | | System.out.println("生成的token:"+token); |
| | | for (String s : decoded) { |
| | | System.out.println("解析后的token:"+s); |
| | | } |
| | | } |
| | | |
| | | } |
New file |
| | |
| | | package com.moral.util; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Qualifier; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | /** |
| | | * @ClassName tokenUtils |
| | | * @Description token生成、解析工具类 |
| | | * @Author 陈凯裕 |
| | | * @Date 2021/3/10 11:35 |
| | | * @Version TODO |
| | | **/ |
| | | @Component |
| | | @Slf4j |
| | | public class TokenUtils { |
| | | |
| | | private static RedisTemplate redisTemplate; |
| | | |
| | | @Autowired |
| | | @Qualifier("tokenRedisTemplate") |
| | | public void setRedisTemplate(RedisTemplate redisTemplate) { |
| | | TokenUtils.redisTemplate = redisTemplate; |
| | | } |
| | | |
| | | //token有效期 单位:秒 |
| | | private static final int validity_time = 60*60*24*7; |
| | | //token非法,生成错误 |
| | | public static final int error = -1; |
| | | //token过期 |
| | | public static final int timeout = -2; |
| | | //token有效,生成成功 |
| | | public static final int valid = 1; |
| | | |
| | | /** |
| | | * @Description: 生成token |
| | | * @Param: [type, uid] type: 后台取值:manage 前台取值:api |
| | | * @return: java.lang.String |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/10 |
| | | */ |
| | | public static Map<String, Object> getToken(String uid, Map<String, Object> userInfo) { |
| | | Map<String, Object> result = new HashMap<>(); |
| | | //生成加密token |
| | | try { |
| | | String token = TokenEncryptUtils.encoded(uid + "/" + System.currentTimeMillis() / 1000); |
| | | redisTemplate.opsForValue().set(token, userInfo); |
| | | redisTemplate.expire(token, validity_time, TimeUnit.SECONDS); |
| | | result.put("code", valid); |
| | | result.put("token",token); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | log.error(e.getMessage()); |
| | | result.put("code",error); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * @Description: 校验token |
| | | * @Param: [type, token] type: 后台取值:manage 前台取值:api |
| | | * @return: java.util.Map<java.lang.String , java.lang.Object> |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/10 |
| | | */ |
| | | public static Map<String, Object> checkToken( String token) { |
| | | Map<String, Object> result = new HashMap<>(); |
| | | try { |
| | | String[] tokenArray = TokenEncryptUtils.decoded(token).split("/"); |
| | | //校验token是否合法 |
| | | if (tokenArray.length != 2) { |
| | | result.put("code", error); |
| | | result.put("msg", "无效的token"); |
| | | return result; |
| | | } |
| | | //校验token是否过期 |
| | | int tokenTime = Integer.parseInt(tokenArray[1]); |
| | | if ((System.currentTimeMillis() / 1000) - tokenTime > validity_time) { |
| | | result.put("code", timeout); |
| | | result.put("msg", "登陆身份已过期,请重新登陆"); |
| | | return result; |
| | | } |
| | | result.put("code", valid); |
| | | return result; |
| | | } catch (Exception e) { |
| | | log.error("token工具类校验token异常" + e.getMessage()); |
| | | result.put("code", error); |
| | | result.put("msg", "无效的token"); |
| | | return result; |
| | | } |
| | | } |
| | | |
| | | //通过token获取用户信息 |
| | | public static Map<String, Object> getUserInfoByToken(String token) { |
| | | Map<String, Object> userInfo = (Map<String, Object>) redisTemplate.opsForValue().get(token); |
| | | return userInfo; |
| | | } |
| | | } |
New file |
| | |
| | | package com.moral.api.config.mvc; |
| | | |
| | | import com.moral.api.interceptor.ManageInterceptor; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.boot.context.properties.ConfigurationProperties; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.web.servlet.config.annotation.InterceptorRegistration; |
| | | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
| | | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| | | |
| | | import java.util.ArrayList; |
| | | |
| | | @Configuration |
| | | public class SpringMVCconfig implements WebMvcConfigurer{ |
| | | |
| | | /** |
| | | * @Description: 获取yaml中配置的不拦截路径 |
| | | * @Param: [] |
| | | * @return: java.util.ArrayList<java.lang.String> |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/9 |
| | | */ |
| | | @Bean |
| | | @ConfigurationProperties("mvc.interceptor.exclude") |
| | | public ArrayList<String> getExcludePath(){ |
| | | return new ArrayList<>(); |
| | | } |
| | | |
| | | @Override |
| | | public void addInterceptors(InterceptorRegistry registry) { |
| | | InterceptorRegistration regisration = registry.addInterceptor(new ManageInterceptor()); |
| | | ArrayList<String> excludePath = getExcludePath(); |
| | | regisration.addPathPatterns("/**/**");//设置拦截路径 |
| | | regisration.excludePathPatterns(excludePath);//设置不拦截路径 |
| | | } |
| | | } |
| | |
| | | package com.moral.api.config.redis; |
| | | |
| | | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; |
| | | import com.fasterxml.jackson.annotation.JsonAutoDetect; |
| | | import com.fasterxml.jackson.annotation.PropertyAccessor; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import org.apache.commons.pool2.impl.GenericObjectPoolConfig; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.context.annotation.Configuration; |
| | | import org.springframework.data.redis.connection.RedisConnectionFactory; |
| | | import org.springframework.data.redis.connection.RedisPassword; |
| | | import org.springframework.data.redis.connection.RedisStandaloneConfiguration; |
| | | import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; |
| | | import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; |
| | | import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.data.redis.core.StringRedisTemplate; |
| | | import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; |
| | | import org.springframework.data.redis.serializer.StringRedisSerializer; |
| | | import redis.clients.jedis.JedisPoolConfig; |
| | | |
| | | import java.time.Duration; |
| | | |
| | | |
| | | @Configuration |
| | | public class RedisConfig { |
| | | |
| | | @Bean(name="redisTemplate") |
| | | @Bean(name = "redisTemplate") |
| | | @ConditionalOnMissingBean(StringRedisTemplate.class) //此注解的作用 如果容器中没有RedisTemplate 那就注入 有就不注入了 |
| | | public RedisTemplate<String,Object> stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { |
| | | public RedisTemplate<String, Object> stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) { |
| | | RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); |
| | | redisTemplate.setConnectionFactory(redisConnectionFactory); |
| | | |
| | | Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); |
| | |
| | | redisTemplate.afterPropertiesSet(); |
| | | return redisTemplate; |
| | | } |
| | | |
| | | |
| | | @Bean(name = "tokenRedisTemplate") |
| | | public RedisTemplate<String, Object> getTokenRedisTemplate( |
| | | @Value("${spring.tokenRedis.host}") String hostName, |
| | | @Value("${spring.tokenRedis.password}") String password, |
| | | @Value("${spring.tokenRedis.port}") int port, |
| | | @Value("${spring.tokenRedis.database}") int database, |
| | | @Value("${spring.tokenRedis.pool.max-active}") int maxActive, |
| | | @Value("${spring.tokenRedis.pool.max-idle}") int maxIdle, |
| | | @Value("${spring.tokenRedis.pool.min-idle}") int minIdle, |
| | | @Value("${spring.tokenRedis.pool.max-wait}") long maxWait, |
| | | @Value("${spring.tokenRedis.timeout}") long timeout |
| | | ) { |
| | | RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration(); |
| | | configuration.setHostName(hostName); |
| | | configuration.setPort(port); |
| | | configuration.setDatabase(database); |
| | | RedisPassword redisPassword = RedisPassword.of(password); |
| | | configuration.setPassword(redisPassword); |
| | | |
| | | GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig(); |
| | | genericObjectPoolConfig.setMaxTotal(maxActive); |
| | | genericObjectPoolConfig.setMinIdle(minIdle); |
| | | genericObjectPoolConfig.setMaxIdle(maxIdle); |
| | | genericObjectPoolConfig.setMaxWaitMillis(maxWait); |
| | | |
| | | LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration.builder(); |
| | | builder.poolConfig(genericObjectPoolConfig); |
| | | builder.commandTimeout(Duration.ofSeconds(timeout)); |
| | | LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(configuration, builder.build()); |
| | | connectionFactory.afterPropertiesSet(); |
| | | |
| | | return createRedisTemplate(connectionFactory); |
| | | } |
| | | |
| | | public RedisTemplate createRedisTemplate(RedisConnectionFactory redisConnectionFactory) { |
| | | RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); |
| | | redisTemplate.setConnectionFactory(redisConnectionFactory); |
| | | |
| | | Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); |
| | | objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); |
| | | jackson2JsonRedisSerializer.setObjectMapper(objectMapper); |
| | | |
| | | StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); |
| | | // key采用String的序列化方式 |
| | | redisTemplate.setKeySerializer(stringRedisSerializer); |
| | | // hash的key也采用String的序列化方式 |
| | | redisTemplate.setHashKeySerializer(stringRedisSerializer); |
| | | // valuevalue采用jackson序列化方式 |
| | | redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); |
| | | // hash的value采用jackson序列化方式 |
| | | redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); |
| | | redisTemplate.afterPropertiesSet(); |
| | | redisTemplate.afterPropertiesSet(); |
| | | return redisTemplate; |
| | | } |
| | | } |
New file |
| | |
| | | package com.moral.api.controller; |
| | | |
| | | import com.moral.api.service.ManageAccountService; |
| | | import com.moral.constant.ResultMessage; |
| | | import com.moral.util.ObjectUtils; |
| | | import io.netty.util.internal.ObjectUtil; |
| | | import io.swagger.annotations.Api; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.Map; |
| | | |
| | | @Slf4j |
| | | @Api(tags = {"后台管理"}) |
| | | @RestController |
| | | @RequestMapping("/account") |
| | | public class AccountController { |
| | | @Resource |
| | | ManageAccountService accountService; |
| | | |
| | | @PostMapping("login") |
| | | public ResultMessage login(@RequestBody Map<String,Object> paramters){ |
| | | if(!ObjectUtils.checkParamAndMap(paramters,"account","password")) |
| | | return ResultMessage.fail("参数不完整"); |
| | | Map<String, Object> result = accountService.login(paramters); |
| | | if(result.get("accountId").equals(-1)) |
| | | return ResultMessage.fail(result); |
| | | return ResultMessage.ok(result); |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | import com.moral.constant.ResultMessage; |
| | | import com.moral.redis.RedisUtil; |
| | | import com.moral.util.PageResult; |
| | | import io.swagger.annotations.*; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiImplicitParam; |
| | | import io.swagger.annotations.ApiImplicitParams; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.kafka.core.KafkaTemplate; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.bind.annotation.PathVariable; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RequestMethod; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | |
| | | @Slf4j |
| | | @Api(tags = {"后台管理"}) |
| | | @Api(tags = {"大屏"}) |
| | | @RestController |
| | | @RequestMapping("/manage") |
| | | @RequestMapping("/api") |
| | | public class TestController { |
| | | |
| | | @Autowired |
| | | @Resource |
| | | private KafkaTemplate kafkaTemplate; |
| | | |
| | | @Autowired |
| | | @Resource |
| | | private TestService testService; |
| | | /** |
| | | * name 姓名 |
| | |
| | | import com.baomidou.mybatisplus.annotation.TableId; |
| | | import java.time.LocalDateTime; |
| | | import java.io.Serializable; |
| | | import java.util.Date; |
| | | |
| | | import lombok.Data; |
| | | import lombok.EqualsAndHashCode; |
| | | |
| | |
| | | /** |
| | | * 创建时间 |
| | | */ |
| | | private LocalDateTime createTime; |
| | | private Date createTime; |
| | | |
| | | /** |
| | | * 更新时间 |
| | | */ |
| | | private LocalDateTime updateTime; |
| | | private Date updateTime; |
| | | |
| | | /** |
| | | * 是否删除 |
| | |
| | | import com.baomidou.mybatisplus.annotation.TableId; |
| | | import java.time.LocalDateTime; |
| | | import java.io.Serializable; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | import lombok.Data; |
| | | import lombok.EqualsAndHashCode; |
| | | |
| | |
| | | /** |
| | | * 创建时间 |
| | | */ |
| | | private LocalDateTime createTime; |
| | | private Date createTime; |
| | | |
| | | /** |
| | | * 更新时间 |
| | | */ |
| | | private LocalDateTime updateTime; |
| | | private Date updateTime; |
| | | |
| | | /** |
| | | * 是否删除 |
| | | */ |
| | | private String isDelete; |
| | | |
| | | /* |
| | | * 二级菜单 |
| | | * */ |
| | | private List<ManageMenu> children; |
| | | |
| | | @Override |
| | | protected Serializable pkVal() { |
| | |
| | | import com.baomidou.mybatisplus.annotation.TableId; |
| | | import java.time.LocalDateTime; |
| | | import java.io.Serializable; |
| | | import java.util.Date; |
| | | |
| | | import lombok.Data; |
| | | import lombok.EqualsAndHashCode; |
| | | |
| | |
| | | /** |
| | | * 角色名 |
| | | */ |
| | | private String roleName; |
| | | private String name; |
| | | |
| | | /** |
| | | * 创建时间 |
| | | */ |
| | | private LocalDateTime createTime; |
| | | private Date createTime; |
| | | |
| | | /** |
| | | * 更新时间 |
| | | */ |
| | | private LocalDateTime updateTime; |
| | | private Date updateTime; |
| | | |
| | | /** |
| | | * 是否删除 |
| | |
| | | package com.moral.api.entity; |
| | | |
| | | import com.baomidou.mybatisplus.annotation.IdType; |
| | | import com.baomidou.mybatisplus.extension.activerecord.Model; |
| | | import com.baomidou.mybatisplus.annotation.TableId; |
| | | import java.io.Serializable; |
| | | import com.baomidou.mybatisplus.extension.activerecord.Model; |
| | | import lombok.Data; |
| | | import lombok.EqualsAndHashCode; |
| | | |
| | | import java.io.Serializable; |
| | | |
| | | /** |
| | | * <p> |
| | | * |
| | |
| | | |
| | | import com.moral.constant.Constants; |
| | | import com.moral.constant.ResultMessage; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.web.bind.annotation.ControllerAdvice; |
| | | import org.springframework.web.bind.annotation.ExceptionHandler; |
| | |
| | | |
| | | @ControllerAdvice |
| | | @ResponseBody |
| | | @Slf4j |
| | | public class GlobalExceptionHandler { |
| | | /** |
| | | * 处理全部异常 |
| | |
| | | @ResponseBody |
| | | @ResponseStatus(HttpStatus.OK) |
| | | public ResultMessage handleException(Exception ex) { |
| | | log.error(ex.getMessage()); |
| | | log.error(ex.getStackTrace().toString()); |
| | | return ResultMessage.fail(Constants.CODE_OPERATION_FAILED, "请求失败"); |
| | | } |
| | | |
New file |
| | |
| | | package com.moral.api.interceptor; |
| | | |
| | | import org.springframework.web.servlet.HandlerInterceptor; |
| | | import org.springframework.web.servlet.ModelAndView; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | |
| | | public class ManageInterceptor implements HandlerInterceptor { |
| | | @Override |
| | | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { |
| | | return false; |
| | | } |
| | | |
| | | @Override |
| | | public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { |
| | | |
| | | } |
| | | } |
| | |
| | | |
| | | import com.moral.api.entity.ManageMenu; |
| | | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import com.moral.api.entity.ManageRole; |
| | | |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | * @since 2021-03-09 |
| | | */ |
| | | public interface ManageMenuMapper extends BaseMapper<ManageMenu> { |
| | | /** |
| | | * @Description: 根据角色获取所有的根菜单 |
| | | * @Param: [roles] |
| | | * @return: java.util.List<com.moral.api.entity.ManageMenu> |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/10 |
| | | */ |
| | | List<ManageMenu> getParentMenuByRoles(List<ManageRole> roles); |
| | | |
| | | /** |
| | | * @Description: 根据根菜单获取对应的子菜单 |
| | | * @Param: |
| | | * @return: |
| | | * @Author: 陈凯裕 |
| | | * @Date: 2021/3/10 |
| | | */ |
| | | List<ManageMenu> getChildrenByParentId(Integer parentId); |
| | | |
| | | List<ManageMenu> getParentChildrenMenusByRoles(List<ManageRole> roles); |
| | | |
| | | } |
| | |
| | | |
| | | import com.moral.api.entity.ManageRole; |
| | | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import org.apache.ibatis.annotations.Param; |
| | | |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | * @since 2021-03-09 |
| | | */ |
| | | public interface ManageRoleMapper extends BaseMapper<ManageRole> { |
| | | |
| | | List<ManageRole> getManageRoleByAccountId(Integer accountId); |
| | | } |
| | |
| | | package com.moral.api.mapper; |
| | | |
| | | import com.moral.api.entity.Test; |
| | | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import com.moral.api.entity.Test; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | import com.moral.api.entity.ManageAccount; |
| | | import com.baomidou.mybatisplus.extension.service.IService; |
| | | |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * <p> |
| | | * 后台账户表 服务类 |
| | |
| | | * @since 2021-03-09 |
| | | */ |
| | | public interface ManageAccountService extends IService<ManageAccount> { |
| | | |
| | | Map<String, Object> login(Map<String, Object> paramters); |
| | | } |
| | |
| | | package com.moral.api.service; |
| | | |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.moral.api.entity.Test; |
| | | import com.baomidou.mybatisplus.extension.service.IService; |
| | | import com.moral.api.entity.Test; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | |
| | | |
| | | Page<Test> selectByPage(Test test, Integer page, Integer size); |
| | | |
| | | @Transactional |
| | | void saveTest() throws Exception; |
| | | } |
| | |
| | | package com.moral.api.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.moral.api.entity.ManageAccount; |
| | | import com.moral.api.entity.ManageMenu; |
| | | import com.moral.api.entity.ManageRole; |
| | | import com.moral.api.mapper.ManageAccountMapper; |
| | | import com.moral.api.mapper.ManageMenuMapper; |
| | | import com.moral.api.mapper.ManageRoleMapper; |
| | | import com.moral.api.service.ManageAccountService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.moral.util.AESUtils; |
| | | import com.moral.util.MD5Utils; |
| | | import com.moral.util.TokenUtils; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | @Service |
| | | public class ManageAccountServiceImpl extends ServiceImpl<ManageAccountMapper, ManageAccount> implements ManageAccountService { |
| | | |
| | | @Value("${AES.KEY}") |
| | | private String AESKey; |
| | | @Resource |
| | | ManageAccountMapper accountMapper; |
| | | @Resource |
| | | ManageRoleMapper roleMapper; |
| | | @Resource |
| | | ManageMenuMapper manageMenuMapper; |
| | | |
| | | |
| | | public Map<String, Object> login(Map<String, Object> paramters) { |
| | | Map<String,Object> result = new HashMap<>(); |
| | | //接收参数 |
| | | String cyrpAccount = (String) paramters.get("account"); |
| | | String cyrpPassword = (String) paramters.get("password"); |
| | | //解密 |
| | | String account = AESUtils.decrypt(cyrpAccount, AESKey); |
| | | String password = AESUtils.decrypt(cyrpPassword, AESKey); |
| | | //查询是否存在 |
| | | QueryWrapper<ManageAccount> wrapper = new QueryWrapper<>(); |
| | | wrapper.eq("account", account); |
| | | ManageAccount manageAccount = accountMapper.selectOne(wrapper); |
| | | if(ObjectUtils.isEmpty(manageAccount)){ |
| | | result.put("accountId",-1); |
| | | result.put("msg","用户不存在!"); |
| | | return result; |
| | | } |
| | | //查询是否逻辑删除 |
| | | if(manageAccount.getIsDelete().equals("1")){ |
| | | result.put("accountId",-1); |
| | | result.put("msg","用户已被封禁"); |
| | | return result; |
| | | } |
| | | //校验密码 |
| | | if(!MD5Utils.saltMD5Verify(password,manageAccount.getPassword())){ |
| | | result.put("accountId",-1); |
| | | result.put("msg","用户名密码错误"); |
| | | return result; |
| | | } |
| | | //查询角色 |
| | | List<ManageRole> roles = roleMapper.getManageRoleByAccountId(manageAccount.getId()); |
| | | if(ObjectUtils.isEmpty(roles)){ |
| | | result.put("accountId",-1); |
| | | result.put("msg","用户尚未分配角色"); |
| | | return result; |
| | | } |
| | | //查询菜单 |
| | | List<ManageMenu> menus = manageMenuMapper.getParentChildrenMenusByRoles(roles); |
| | | if(ObjectUtils.isEmpty(menus)){ |
| | | result.put("accountId",-1); |
| | | result.put("msg","用户尚未分配菜单"); |
| | | return result; |
| | | } |
| | | |
| | | //获取用户token,并且将基本信息存入缓存 |
| | | Map<String,Object> userInfo = new HashMap<>();//需要保存在缓存中用户的数据 |
| | | userInfo.put("accountId",manageAccount.getId());//用户Id |
| | | userInfo.put("userName",manageAccount.getUserName());//用户名称 |
| | | userInfo.put("roles",roles);//用户角色 |
| | | userInfo.put("menus",menus);//用户菜单 |
| | | Map<String, Object> tokenResult = TokenUtils.getToken(String.valueOf(manageAccount.getId()), userInfo); |
| | | if(tokenResult.get("code").equals(TokenUtils.error)){ |
| | | result.put("accountId",-1); |
| | | result.put("msg","生成token错误"); |
| | | return result; |
| | | } |
| | | |
| | | //打包返回信息 |
| | | result.put("accountId",manageAccount.getId());//用户Id |
| | | result.put("userName",manageAccount.getUserName());//用户名称 |
| | | result.put("roles",roles);//用户角色 |
| | | result.put("menus",menus);//用户菜单 |
| | | result.put("token",tokenResult.get("token")); |
| | | return result; |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | import com.moral.api.service.ManageRoleService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | @Service |
| | | public class ManageRoleServiceImpl extends ServiceImpl<ManageRoleMapper, ManageRole> implements ManageRoleService { |
| | | |
| | | @Resource |
| | | ManageRoleMapper roleMapper; |
| | | |
| | | public ManageRole getManageRoleByAccountId(String accountId){ |
| | | if(StringUtils.isEmpty(accountId)) |
| | | return null; |
| | | return null; |
| | | } |
| | | } |
| | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.moral.api.entity.Test; |
| | | import com.moral.api.exception.BusinessException; |
| | | import com.moral.api.mapper.TestMapper; |
| | | import com.moral.api.service.TestService; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | @Service |
| | | public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService { |
| | | |
| | | @Autowired |
| | | @Resource |
| | | private TestMapper testMapper; |
| | | @Override |
| | | public Page<Test> selectByPage(Test test, Integer page, Integer size) { |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public void saveTest() throws Exception{ |
| | | Test t=new Test(); |
| | | t.setName("aaaa"); |
| | |
| | | #nodes: 47.112.126.78:7001,47.112.126.78:7002,47.112.126.132:7003,47.112.126.132:7004,47.112.132.193:7005,47.112.132.193:7006 |
| | | #password: test |
| | | #timeout: 500 |
| | | host: 127.0.0.1 |
| | | host: 39.97.177.149 |
| | | port: 6379 |
| | | password: 123456 |
| | | password: chenkaiyu111 |
| | | timeout: 30000 |
| | | jedis: |
| | | pool: |
| | |
| | | max-idle: 64 |
| | | max-wait: 30000 |
| | | min-idle: 32 |
| | | tokenRedis: |
| | | host: 39.97.177.149 |
| | | port: 6379 |
| | | password: chenkaiyu111 |
| | | timeout: 30000 |
| | | database: 14 |
| | | pool: |
| | | max-active: 256 |
| | | max-wait: 30000 |
| | | max-idle: 64 |
| | | min-idle: 32 |
| | | |
| | | datasource: |
| | | minIdle: 1 |
| | | time-between-eviction-runs-millis: 60000 |
| | |
| | | filters: stat |
| | | type: com.alibaba.druid.pool.DruidDataSource |
| | | max-wait: 60000 |
| | | url: jdbc:mysql://192.168.0.18:4000/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC |
| | | password: 123456 |
| | | url: jdbc:mysql://39.97.177.149:3306/moral?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC |
| | | username: root |
| | | password: chenkaiyu111 |
| | | test-on-borrow: false |
| | | sql-script-encoding: utf-8 |
| | | pool-prepared-statements: true |
| | |
| | | driver-class-name: com.mysql.cj.jdbc.Driver |
| | | max-conn-lifetime-millis: 20 |
| | | test-on-return: false |
| | | username: root |
| | | |
| | | mybatis-plus: |
| | | mapper-locations: classpath:mapper/*.xml |
| | |
| | | linger: 1 |
| | | retries: 0 |
| | | servers: 192.168.0.16:9092,192.168.0.17:9092,192.168.0.18:9092 |
| | | mvc: |
| | | interceptor: |
| | | exclude: |
| | | - /account/login |
| | | AES: |
| | | KEY: |
| | | AD42F7787B035B7580000EF93BE20BAD |
| | | TOKEN: |
| | | KEY: |
| | | foh3wi2ooghiCh5 |
| | | |
| | |
| | | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
| | | <mapper namespace="com.moral.api.mapper.ManageMenuMapper"> |
| | | |
| | | <!-- 通用查询映射结果 --> |
| | | <resultMap id="BaseResultMap" type="com.moral.api.entity.ManageMenu"> |
| | | <id column="id" property="id" /> |
| | | <result column="name" property="name" /> |
| | | <result column="url" property="url" /> |
| | | <result column="icon" property="icon" /> |
| | | <result column="parent_id" property="parentId" /> |
| | | <result column="order" property="order" /> |
| | | <result column="create_time" property="createTime" /> |
| | | <result column="update_time" property="updateTime" /> |
| | | <result column="is_delete" property="isDelete" /> |
| | | </resultMap> |
| | | <!-- 通用查询映射结果 --> |
| | | <resultMap id="BaseResultMap" type="com.moral.api.entity.ManageMenu"> |
| | | <id column="id" property="id"/> |
| | | <result column="name" property="name"/> |
| | | <result column="url" property="url"/> |
| | | <result column="icon" property="icon"/> |
| | | <result column="parent_id" property="parentId"/> |
| | | <result column="order" property="order"/> |
| | | <result column="create_time" property="createTime"/> |
| | | <result column="update_time" property="updateTime"/> |
| | | <result column="is_delete" property="isDelete"/> |
| | | </resultMap> |
| | | |
| | | <!--父子结构查询映射结果--> |
| | | <resultMap id="ParentChildrenResultMap" type="com.moral.api.entity.ManageMenu"> |
| | | <id column="id" property="id"/> |
| | | <result column="name" property="name"/> |
| | | <result column="url" property="url"/> |
| | | <result column="icon" property="icon"/> |
| | | <result column="parent_id" property="parentId"/> |
| | | <result column="order" property="order"/> |
| | | <result column="create_time" property="createTime"/> |
| | | <result column="update_time" property="updateTime"/> |
| | | <result column="is_delete" property="isDelete"/> |
| | | <collection property="children" ofType="com.moral.api.entity.ManageMenu" column="id" select="getChildrenByParentId"></collection> |
| | | </resultMap> |
| | | |
| | | <sql id="Base_Column_List"> |
| | | mm.id,mm.name,mm.url,mm.icon,mm.parent_id,mm.order,mm.create_time,mm.update_time,mm.is_delete |
| | | </sql> |
| | | |
| | | <!--根据角色集合查询所有的父菜单--> |
| | | <select id="getParentMenuByRoles" resultMap="BaseResultMap"> |
| | | select |
| | | <include refid="Base_Column_List"></include> |
| | | from |
| | | manage_menu mm |
| | | join |
| | | manage_role_menu mrm |
| | | on |
| | | mm.`id` = mrm.`menu_id` and mm.`is_delete`=0 |
| | | join |
| | | manage_role mr |
| | | on |
| | | mr.`id` = mrm.`role_id` and mr.`id` IN |
| | | <foreach collection="list" separator="," open="(" close=")" item="role"> |
| | | #{role.id} |
| | | </foreach> |
| | | where |
| | | mm.`is_delete`=0 |
| | | AND |
| | | mm.`parent_id`=0; |
| | | </select> |
| | | |
| | | <!--根据父菜单Id查询所有的子菜单--> |
| | | <select id="getChildrenByParentId" parameterType="Integer" resultMap="BaseResultMap"> |
| | | SELECT |
| | | <include refid="Base_Column_List"></include> |
| | | FROM |
| | | manage_menu mm |
| | | WHERE |
| | | mm.parent_id = #{value} |
| | | </select> |
| | | |
| | | <!--根据角色查询父子结构的菜单--> |
| | | <select id="getParentChildrenMenusByRoles" resultMap="ParentChildrenResultMap"> |
| | | select |
| | | <include refid="Base_Column_List"></include> |
| | | from |
| | | manage_menu mm |
| | | join |
| | | manage_role_menu mrm |
| | | on |
| | | mm.`id` = mrm.`menu_id` and mm.`is_delete`=0 |
| | | join |
| | | manage_role mr |
| | | on |
| | | mr.`id` = mrm.`role_id` and mr.`id` IN |
| | | <foreach collection="list" separator="," open="(" close=")" item="role"> |
| | | #{role.id} |
| | | </foreach> |
| | | where |
| | | mm.`is_delete`=0 |
| | | AND |
| | | mm.`parent_id`=0; |
| | | </select> |
| | | |
| | | </mapper> |
| | |
| | | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
| | | <mapper namespace="com.moral.api.mapper.ManageRoleMapper"> |
| | | |
| | | <!-- 通用查询映射结果 --> |
| | | <resultMap id="BaseResultMap" type="com.moral.api.entity.ManageRole"> |
| | | <id column="id" property="id" /> |
| | | <result column="role_name" property="roleName" /> |
| | | <result column="create_time" property="createTime" /> |
| | | <result column="update_time" property="updateTime" /> |
| | | <result column="is_delete" property="isDelete" /> |
| | | <result column="desc" property="desc" /> |
| | | </resultMap> |
| | | <!-- 通用查询映射结果 --> |
| | | <resultMap id="BaseResultMap" type="com.moral.api.entity.ManageRole"> |
| | | <id column="id" property="id"/> |
| | | <result column="name" property="name"/> |
| | | <result column="create_time" property="createTime"/> |
| | | <result column="update_time" property="updateTime"/> |
| | | <result column="is_delete" property="isDelete"/> |
| | | <result column="desc" property="desc"/> |
| | | </resultMap> |
| | | |
| | | <sql id="Base_Column_List"> |
| | | mr.id,mr.name,mr.create_time,mr.update_time,mr.is_delete,mr.desc |
| | | </sql> |
| | | |
| | | <select id="getManageRoleByAccountId" resultMap="BaseResultMap"> |
| | | select |
| | | <include refid="Base_Column_List"></include> |
| | | FROM |
| | | manage_role mr |
| | | JOIN manage_account_role mar |
| | | ON mar.role_id=mr.id AND mar.is_delete = 0 |
| | | JOIN manage_account ma |
| | | ON ma.id=mar.account_id and ma.id = #{accountId} |
| | | where |
| | | mr.is_delete=0 |
| | | </select> |
| | | </mapper> |