13 changed files with 575 additions and 3 deletions
			
			
		@ -0,0 +1,41 @@ | 
				
			|||||
 | 
					<?xml version="1.0" encoding="UTF-8"?> | 
				
			||||
 | 
					<project xmlns="http://maven.apache.org/POM/4.0.0" | 
				
			||||
 | 
					         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | 
				
			||||
 | 
					         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | 
				
			||||
 | 
					    <parent> | 
				
			||||
 | 
					        <artifactId>epdc-commons</artifactId> | 
				
			||||
 | 
					        <groupId>com.esua.epdc</groupId> | 
				
			||||
 | 
					        <version>1.0.0</version> | 
				
			||||
 | 
					    </parent> | 
				
			||||
 | 
					    <modelVersion>4.0.0</modelVersion> | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    <artifactId>epdc-commons-tools-phone</artifactId> | 
				
			||||
 | 
					    <packaging>jar</packaging> | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    <dependencies> | 
				
			||||
 | 
					        <dependency> | 
				
			||||
 | 
					            <groupId>com.googlecode.libphonenumber</groupId> | 
				
			||||
 | 
					            <artifactId>libphonenumber</artifactId> | 
				
			||||
 | 
					            <version>8.8.8</version> | 
				
			||||
 | 
					        </dependency> | 
				
			||||
 | 
					        <dependency> | 
				
			||||
 | 
					            <groupId>com.googlecode.libphonenumber</groupId> | 
				
			||||
 | 
					            <artifactId>carrier</artifactId> | 
				
			||||
 | 
					            <version>1.75</version> | 
				
			||||
 | 
					        </dependency> | 
				
			||||
 | 
					        <dependency> | 
				
			||||
 | 
					            <groupId>com.googlecode.libphonenumber</groupId> | 
				
			||||
 | 
					            <artifactId>geocoder</artifactId> | 
				
			||||
 | 
					            <version>2.85</version> | 
				
			||||
 | 
					        </dependency> | 
				
			||||
 | 
					        <dependency> | 
				
			||||
 | 
					            <groupId>com.googlecode.libphonenumber</groupId> | 
				
			||||
 | 
					            <artifactId>prefixmapper</artifactId> | 
				
			||||
 | 
					            <version>2.85</version> | 
				
			||||
 | 
					        </dependency> | 
				
			||||
 | 
					    </dependencies> | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    <build> | 
				
			||||
 | 
					        <finalName>${project.artifactId}</finalName> | 
				
			||||
 | 
					    </build> | 
				
			||||
 | 
					</project> | 
				
			||||
@ -0,0 +1,79 @@ | 
				
			|||||
 | 
					package com.elink.esua.epdc.commons.tools.utils; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					/** | 
				
			||||
 | 
					 * 电话DTO | 
				
			||||
 | 
					 * | 
				
			||||
 | 
					 * @author rongchao | 
				
			||||
 | 
					 * @Date 18-12-11 | 
				
			||||
 | 
					 */ | 
				
			||||
 | 
					public class PhoneDto { | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 省份名称 | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    private String provinceName; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 城市名称 | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    private String cityName; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 运营商:移动/电信/联通 | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    private String carrier; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 省份名称 | 
				
			||||
 | 
					     * | 
				
			||||
 | 
					     * @return 获取provinceName属性值 | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    public String getProvinceName() { | 
				
			||||
 | 
					        return provinceName; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 省份名称 | 
				
			||||
 | 
					     * | 
				
			||||
 | 
					     * @param provinceName 设置 provinceName 属性值为参数值 provinceName | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    public void setProvinceName(String provinceName) { | 
				
			||||
 | 
					        this.provinceName = provinceName; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 城市名称 | 
				
			||||
 | 
					     * | 
				
			||||
 | 
					     * @return 获取cityName属性值 | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    public String getCityName() { | 
				
			||||
 | 
					        return cityName; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 城市名称 | 
				
			||||
 | 
					     * | 
				
			||||
 | 
					     * @param cityName 设置 cityName 属性值为参数值 cityName | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    public void setCityName(String cityName) { | 
				
			||||
 | 
					        this.cityName = cityName; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 运营商:移动/电信/联通 | 
				
			||||
 | 
					     * | 
				
			||||
 | 
					     * @return 获取carrier属性值 | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    public String getCarrier() { | 
				
			||||
 | 
					        return carrier; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 运营商:移动/电信/联通 | 
				
			||||
 | 
					     * | 
				
			||||
 | 
					     * @param carrier 设置 carrier 属性值为参数值 carrier | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    public void setCarrier(String carrier) { | 
				
			||||
 | 
					        this.carrier = carrier; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					} | 
				
			||||
@ -0,0 +1,175 @@ | 
				
			|||||
 | 
					package com.elink.esua.epdc.commons.tools.utils; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					/** | 
				
			||||
 | 
					 * @author rongchao | 
				
			||||
 | 
					 * @Date 18-12-11 | 
				
			||||
 | 
					 */ | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					import com.google.i18n.phonenumbers.PhoneNumberToCarrierMapper; | 
				
			||||
 | 
					import com.google.i18n.phonenumbers.PhoneNumberUtil; | 
				
			||||
 | 
					import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber; | 
				
			||||
 | 
					import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					import java.util.Locale; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					/** | 
				
			||||
 | 
					 * 手机号归属地查询 | 
				
			||||
 | 
					 * jar依赖:com.googlecode.libphonenumber(Libphonenumber、Geocoder、Prefixmapper | 
				
			||||
 | 
					 * 、Carrier) pom依赖:http://mvnrepository.com/search?q=libphonenumber
 | 
				
			||||
 | 
					 * 项目地址:https://github.com/googlei18n/libphonenumber
 | 
				
			||||
 | 
					 * | 
				
			||||
 | 
					 * @author rongchao | 
				
			||||
 | 
					 * @Date 18-12-11 | 
				
			||||
 | 
					 */ | 
				
			||||
 | 
					public class PhoneUtil { | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 直辖市 | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    private final static String[] MUNICIPALITY = {"北京市", "天津市", "上海市", "重庆市"}; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 自治区 | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    private final static String[] AUTONOMOUS_REGION = {"新疆", "内蒙古", "西藏", "宁夏", "广西"}; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    private static PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil | 
				
			||||
 | 
					            .getInstance(); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 提供与电话号码相关的运营商信息 | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    private static PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper | 
				
			||||
 | 
					            .getInstance(); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 提供与电话号码有关的地理信息 | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    private static PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder | 
				
			||||
 | 
					            .getInstance(); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 中国大陆区区号 | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    private final static int COUNTRY_CODE = 86; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 根据手机号 判断手机号是否有效 | 
				
			||||
 | 
					     * | 
				
			||||
 | 
					     * @param phoneNumber 手机号码 | 
				
			||||
 | 
					     * @return true-有效 false-无效 | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    public static boolean checkPhoneNumber(String phoneNumber) { | 
				
			||||
 | 
					        long phone = Long.parseLong(phoneNumber); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					        PhoneNumber pn = new PhoneNumber(); | 
				
			||||
 | 
					        pn.setCountryCode(COUNTRY_CODE); | 
				
			||||
 | 
					        pn.setNationalNumber(phone); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					        return phoneNumberUtil.isValidNumber(pn); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 根据手机号 判断手机运营商 | 
				
			||||
 | 
					     * | 
				
			||||
 | 
					     * @param phoneNumber 手机号码 | 
				
			||||
 | 
					     * @return 如:广东省广州市移动 | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    public static String getCarrier(String phoneNumber) { | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					        long phone = Long.parseLong(phoneNumber); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					        PhoneNumber pn = new PhoneNumber(); | 
				
			||||
 | 
					        pn.setCountryCode(COUNTRY_CODE); | 
				
			||||
 | 
					        pn.setNationalNumber(phone); | 
				
			||||
 | 
					        // 返回结果只有英文,自己转成成中文
 | 
				
			||||
 | 
					        String carrierEn = carrierMapper.getNameForNumber(pn, Locale.ENGLISH); | 
				
			||||
 | 
					        String carrierZh = ""; | 
				
			||||
 | 
					        switch (carrierEn) { | 
				
			||||
 | 
					            case "China Mobile": | 
				
			||||
 | 
					                carrierZh += "移动"; | 
				
			||||
 | 
					                break; | 
				
			||||
 | 
					            case "China Unicom": | 
				
			||||
 | 
					                carrierZh += "联通"; | 
				
			||||
 | 
					                break; | 
				
			||||
 | 
					            case "China Telecom": | 
				
			||||
 | 
					                carrierZh += "电信"; | 
				
			||||
 | 
					                break; | 
				
			||||
 | 
					            default: | 
				
			||||
 | 
					                break; | 
				
			||||
 | 
					        } | 
				
			||||
 | 
					        return carrierZh; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 根据手机号 获取手机归属地 | 
				
			||||
 | 
					     * | 
				
			||||
 | 
					     * @param phoneNumber 手机号码 | 
				
			||||
 | 
					     * @return 如:广东省广州市 | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    public static String getGeo(String phoneNumber) { | 
				
			||||
 | 
					        long phone = Long.parseLong(phoneNumber); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					        PhoneNumber pn = new PhoneNumber(); | 
				
			||||
 | 
					        pn.setCountryCode(COUNTRY_CODE); | 
				
			||||
 | 
					        pn.setNationalNumber(phone); | 
				
			||||
 | 
					        return geocoder.getDescriptionForNumber(pn, Locale.CHINESE); | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 根据手机号 获取手机信息模型 | 
				
			||||
 | 
					     * | 
				
			||||
 | 
					     * <pre> | 
				
			||||
 | 
					     * 若返回值为null,则说明该号码无效 | 
				
			||||
 | 
					     * </pre> | 
				
			||||
 | 
					     * | 
				
			||||
 | 
					     * @param phoneNumber 手机号码 | 
				
			||||
 | 
					     * @return 手机信息模型PhoneModel | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    public static PhoneDto getPhoneDto(String phoneNumber) { | 
				
			||||
 | 
					        if (checkPhoneNumber(phoneNumber)) { | 
				
			||||
 | 
					            String geo = getGeo(phoneNumber); | 
				
			||||
 | 
					            PhoneDto phoneDto = new PhoneDto(); | 
				
			||||
 | 
					            String carrier = getCarrier(phoneNumber); | 
				
			||||
 | 
					            phoneDto.setCarrier(carrier); | 
				
			||||
 | 
					            // 直辖市
 | 
				
			||||
 | 
					            for (String val : MUNICIPALITY) { | 
				
			||||
 | 
					                if (geo.equals(val)) { | 
				
			||||
 | 
					                    phoneDto.setProvinceName(val.replace("市", "")); | 
				
			||||
 | 
					                    phoneDto.setCityName(val); | 
				
			||||
 | 
					                    return phoneDto; | 
				
			||||
 | 
					                } | 
				
			||||
 | 
					            } | 
				
			||||
 | 
					            // 自治区
 | 
				
			||||
 | 
					            for (String val : AUTONOMOUS_REGION) { | 
				
			||||
 | 
					                if (geo.startsWith(val)) { | 
				
			||||
 | 
					                    phoneDto.setProvinceName(val); | 
				
			||||
 | 
					                    phoneDto.setCityName(geo.replace(val, "")); | 
				
			||||
 | 
					                    return phoneDto; | 
				
			||||
 | 
					                } | 
				
			||||
 | 
					            } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					            // 其它
 | 
				
			||||
 | 
					            String[] splitArr = geo.split("省"); | 
				
			||||
 | 
					            if (splitArr != null && splitArr.length == 2) { | 
				
			||||
 | 
					                phoneDto.setProvinceName(splitArr[0]); | 
				
			||||
 | 
					                phoneDto.setCityName(splitArr[1]); | 
				
			||||
 | 
					                return phoneDto; | 
				
			||||
 | 
					            } | 
				
			||||
 | 
					        } | 
				
			||||
 | 
					        return null; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    public static void main(String[] args) { | 
				
			||||
 | 
					        PhoneDto phoneDto = PhoneUtil.getPhoneDto("13701001254"); | 
				
			||||
 | 
					        if (phoneDto != null) { | 
				
			||||
 | 
					            System.out.println(phoneDto.getProvinceName()); | 
				
			||||
 | 
					            System.out.println(phoneDto.getCityName()); | 
				
			||||
 | 
					            System.out.println(phoneDto.getCarrier()); | 
				
			||||
 | 
					        } else { | 
				
			||||
 | 
					            System.err.println("该号码无效"); | 
				
			||||
 | 
					        } | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					} | 
				
			||||
@ -0,0 +1,40 @@ | 
				
			|||||
 | 
					package com.elink.esua.epdc.commons.tools.utils; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					import java.util.Random; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					/** | 
				
			||||
 | 
					 * @author rongchao | 
				
			||||
 | 
					 * @Date 18-12-17 | 
				
			||||
 | 
					 */ | 
				
			||||
 | 
					public class RandomUtil { | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 根据需要生成指定长度的纯数字随机数,这个随机数的每一位都是从(0-9)这个产生的一位 | 
				
			||||
 | 
					     * | 
				
			||||
 | 
					     * @param codeLen | 
				
			||||
 | 
					     * @return | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    public static String getNewRandomCode(int codeLen) { | 
				
			||||
 | 
					        Random randomCode = new Random(); | 
				
			||||
 | 
					        String strCode = ""; | 
				
			||||
 | 
					        while (codeLen > 0) { | 
				
			||||
 | 
					            int charCode = randomCode.nextInt(9); | 
				
			||||
 | 
					            strCode += charCode; | 
				
			||||
 | 
					            codeLen--; | 
				
			||||
 | 
					        } | 
				
			||||
 | 
					        return strCode; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    public static int getRandomNum(int min, int max) { | 
				
			||||
 | 
					        Random random = new Random(); | 
				
			||||
 | 
					        return random.nextInt(max - min + 1) + min; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    public static void main(String[] args) { | 
				
			||||
 | 
					        for (int i = 0; i < 10000; i++) { | 
				
			||||
 | 
					            int s = RandomUtil.getRandomNum(1, 10); | 
				
			||||
 | 
					            if (s < 1 || s > 10) { | 
				
			||||
 | 
					                System.out.println(s); | 
				
			||||
 | 
					            } | 
				
			||||
 | 
					        } | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					} | 
				
			||||
@ -0,0 +1,45 @@ | 
				
			|||||
 | 
					package com.elink.esua.epdc.config; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					import com.elink.esua.epdc.config.property.WxMpProperties; | 
				
			||||
 | 
					import me.chanjar.weixin.mp.api.WxMpConfigStorage; | 
				
			||||
 | 
					import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage; | 
				
			||||
 | 
					import me.chanjar.weixin.mp.api.WxMpService; | 
				
			||||
 | 
					import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; | 
				
			||||
 | 
					import org.springframework.beans.factory.annotation.Autowired; | 
				
			||||
 | 
					import org.springframework.boot.context.properties.EnableConfigurationProperties; | 
				
			||||
 | 
					import org.springframework.context.annotation.Bean; | 
				
			||||
 | 
					import org.springframework.context.annotation.Configuration; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					/** | 
				
			||||
 | 
					 * 微信公众号配置 | 
				
			||||
 | 
					 * | 
				
			||||
 | 
					 * @author yujintao | 
				
			||||
 | 
					 * @date 2019/6/17 17:38 | 
				
			||||
 | 
					 */ | 
				
			||||
 | 
					@Configuration | 
				
			||||
 | 
					@EnableConfigurationProperties(WxMpProperties.class) | 
				
			||||
 | 
					public class WxMpConfig { | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    private WxMpProperties properties; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    @Autowired | 
				
			||||
 | 
					    public WxMpConfig(WxMpProperties properties) { | 
				
			||||
 | 
					        this.properties = properties; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    @Bean | 
				
			||||
 | 
					    public WxMpService wxMpService() { | 
				
			||||
 | 
					        WxMpService wxMpService = new WxMpServiceImpl(); | 
				
			||||
 | 
					        wxMpService.setWxMpConfigStorage(wxMpConfigStorage()); | 
				
			||||
 | 
					        return wxMpService; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    @Bean | 
				
			||||
 | 
					    public WxMpConfigStorage wxMpConfigStorage() { | 
				
			||||
 | 
					        WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage(); | 
				
			||||
 | 
					        wxMpConfigStorage.setAppId(properties.getAppid()); | 
				
			||||
 | 
					        wxMpConfigStorage.setSecret(properties.getSecret()); | 
				
			||||
 | 
					        return wxMpConfigStorage; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					} | 
				
			||||
@ -0,0 +1,26 @@ | 
				
			|||||
 | 
					package com.elink.esua.epdc.config.property; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					import lombok.Data; | 
				
			||||
 | 
					import org.springframework.boot.context.properties.ConfigurationProperties; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					/** | 
				
			||||
 | 
					 * 微信公众号框架配置类 | 
				
			||||
 | 
					 * | 
				
			||||
 | 
					 * @author | 
				
			||||
 | 
					 * @Date | 
				
			||||
 | 
					 */ | 
				
			||||
 | 
					@Data | 
				
			||||
 | 
					@ConfigurationProperties(prefix = "wx.mp") | 
				
			||||
 | 
					public class WxMpProperties { | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 设置微信公众号的appid | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    private String appid; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 设置微信公众号的Secret | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    private String secret; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					} | 
				
			||||
@ -0,0 +1,43 @@ | 
				
			|||||
 | 
					/** | 
				
			||||
 | 
					 * Copyright (c) 2018 人人开源 All rights reserved. | 
				
			||||
 | 
					 * | 
				
			||||
 | 
					 * https://www.renren.io
 | 
				
			||||
 | 
					 * | 
				
			||||
 | 
					 * 版权所有,侵权必究! | 
				
			||||
 | 
					 */ | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					package com.elink.esua.epdc.jwt; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					import org.springframework.boot.context.properties.ConfigurationProperties; | 
				
			||||
 | 
					import org.springframework.context.annotation.Configuration; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					/** | 
				
			||||
 | 
					 * Jwt | 
				
			||||
 | 
					 * | 
				
			||||
 | 
					 * @author Mark sunlightcs@gmail.com | 
				
			||||
 | 
					 * @since 1.0.0 | 
				
			||||
 | 
					 */ | 
				
			||||
 | 
					@Configuration | 
				
			||||
 | 
					@ConfigurationProperties(prefix = "jwt") | 
				
			||||
 | 
					public class JwtProperties { | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    private String secret; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    private int expire; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    public String getSecret() { | 
				
			||||
 | 
					        return secret; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    public void setSecret(String secret) { | 
				
			||||
 | 
					        this.secret = secret; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    public int getExpire() { | 
				
			||||
 | 
					        return expire; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    public void setExpire(int expire) { | 
				
			||||
 | 
					        this.expire = expire; | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					} | 
				
			||||
@ -0,0 +1,80 @@ | 
				
			|||||
 | 
					/** | 
				
			||||
 | 
					 * Copyright (c) 2018 人人开源 All rights reserved. | 
				
			||||
 | 
					 * <p> | 
				
			||||
 | 
					 * https://www.renren.io
 | 
				
			||||
 | 
					 * <p> | 
				
			||||
 | 
					 * 版权所有,侵权必究! | 
				
			||||
 | 
					 */ | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					package com.elink.esua.epdc.jwt; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					import io.jsonwebtoken.Claims; | 
				
			||||
 | 
					import io.jsonwebtoken.Jwts; | 
				
			||||
 | 
					import io.jsonwebtoken.SignatureAlgorithm; | 
				
			||||
 | 
					import org.joda.time.DateTime; | 
				
			||||
 | 
					import org.slf4j.Logger; | 
				
			||||
 | 
					import org.slf4j.LoggerFactory; | 
				
			||||
 | 
					import org.springframework.beans.factory.annotation.Autowired; | 
				
			||||
 | 
					import org.springframework.stereotype.Component; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					import java.util.Date; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					/** | 
				
			||||
 | 
					 * Jwt工具类 | 
				
			||||
 | 
					 * | 
				
			||||
 | 
					 * @author Mark sunlightcs@gmail.com | 
				
			||||
 | 
					 * @since 1.0.0 | 
				
			||||
 | 
					 */ | 
				
			||||
 | 
					@Component | 
				
			||||
 | 
					public class JwtUtils { | 
				
			||||
 | 
					    private static final Logger logger = LoggerFactory.getLogger(JwtUtils.class); | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    @Autowired | 
				
			||||
 | 
					    private JwtProperties jwtProperties; | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 生成jwt token | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    public String generateToken(Long userId) { | 
				
			||||
 | 
					        return Jwts.builder() | 
				
			||||
 | 
					                .setHeaderParam("typ", "JWT") | 
				
			||||
 | 
					                .setSubject(userId + "") | 
				
			||||
 | 
					                .setIssuedAt(new Date()) | 
				
			||||
 | 
					                .setExpiration(DateTime.now().plusSeconds(jwtProperties.getExpire()).toDate()) | 
				
			||||
 | 
					                .signWith(SignatureAlgorithm.HS512, jwtProperties.getSecret()) | 
				
			||||
 | 
					                .compact(); | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * 生成jwt token | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    public String generateToken(String userId) { | 
				
			||||
 | 
					        return Jwts.builder() | 
				
			||||
 | 
					                .setHeaderParam("typ", "JWT") | 
				
			||||
 | 
					                .setSubject(userId) | 
				
			||||
 | 
					                .setIssuedAt(new Date()) | 
				
			||||
 | 
					                .setExpiration(DateTime.now().plusSeconds(jwtProperties.getExpire()).toDate()) | 
				
			||||
 | 
					                .signWith(SignatureAlgorithm.HS512, jwtProperties.getSecret()) | 
				
			||||
 | 
					                .compact(); | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    public Claims getClaimByToken(String token) { | 
				
			||||
 | 
					        try { | 
				
			||||
 | 
					            return Jwts.parser() | 
				
			||||
 | 
					                    .setSigningKey(jwtProperties.getSecret()) | 
				
			||||
 | 
					                    .parseClaimsJws(token) | 
				
			||||
 | 
					                    .getBody(); | 
				
			||||
 | 
					        } catch (Exception e) { | 
				
			||||
 | 
					            logger.debug("validate is token error, token = " + token, e); | 
				
			||||
 | 
					            return null; | 
				
			||||
 | 
					        } | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					
 | 
				
			||||
 | 
					    /** | 
				
			||||
 | 
					     * token是否过期 | 
				
			||||
 | 
					     * @return true:过期 | 
				
			||||
 | 
					     */ | 
				
			||||
 | 
					    public boolean isTokenExpired(Date expiration) { | 
				
			||||
 | 
					        return expiration.before(new Date()); | 
				
			||||
 | 
					    } | 
				
			||||
 | 
					} | 
				
			||||
					Loading…
					
					
				
		Reference in new issue