From d8e2cd27eed45224faecb07ca45be69bf78611af Mon Sep 17 00:00:00 2001 From: whyclxw <810412026@qq.com> Date: 星期三, 14 五月 2025 15:18:28 +0800 Subject: [PATCH] 用户登录rsa --- src/main/java/com/whyc/util/CommonUtil.java | 615 ++++--------------------------------------------------- 1 files changed, 49 insertions(+), 566 deletions(-) diff --git a/src/main/java/com/whyc/util/CommonUtil.java b/src/main/java/com/whyc/util/CommonUtil.java index 9d82bd0..4145c80 100644 --- a/src/main/java/com/whyc/util/CommonUtil.java +++ b/src/main/java/com/whyc/util/CommonUtil.java @@ -1,594 +1,77 @@ package com.whyc.util; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonSyntaxException; import com.whyc.constant.YamlProperties; import com.whyc.pojo.db_user.User; -import org.apache.commons.codec.digest.DigestUtils; -import org.apache.shiro.SecurityUtils; +import com.whyc.service.UserLogService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.system.ApplicationHome; -import org.springframework.web.context.request.RequestContextHolder; -import org.springframework.web.context.request.ServletRequestAttributes; -import sun.misc.BASE64Decoder; -import sun.misc.BASE64Encoder; +import org.springframework.stereotype.Component; -import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; -import java.io.*; -import java.lang.reflect.Type; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Calendar; -import java.util.Date; -import java.util.Locale; +import java.io.File; +/** + * 閫氱敤宸ュ叿鍒� + */ +@Component public class CommonUtil { - public static Object objectNull = null; + private static UserLogService userLogService; - public static String time_yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss"; - public static String time_yyyyMMdd = "yyyy-MM-dd"; - public static String time_yyyyMMdd_HH_mm_ss = "yyyy-MM-dd_HH_mm_ss"; - public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - public static SimpleDateFormat sdfwithOutday = new SimpleDateFormat("yyyy_MM"); - public static SimpleDateFormat sdfwithday = new SimpleDateFormat("yyyy-MM-dd"); - public static SimpleDateFormat sdfwithtime = new SimpleDateFormat("HH:mm:ss"); - public static SimpleDateFormat sdfwithtime_yyyyMMdd_HH_mm_ss = new SimpleDateFormat(time_yyyyMMdd_HH_mm_ss); - public static SimpleDateFormat sdfwithALL = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - - public static String classesPath(){ - ApplicationHome applicationHome = new ApplicationHome(CommonUtil.class); - File jarFile = applicationHome.getDir(); - return jarFile.toString(); - } - - /** - * 鑾峰彇瀛樻斁鏂囦欢鐨勬牴璺緞 - * @return 杩斿洖瀛樻斁鏂囦欢鐨勬牴璺緞 - */ - public static String getRootFile(){ - ApplicationHome applicationHome = new ApplicationHome(CommonUtil.class); - File jarFile = applicationHome.getDir(); - String baseDirPath; - if(YamlProperties.runModel == 1) { - //寮�鍙戣矾寰� - baseDirPath = jarFile.getParentFile().toString()+File.separator+"battery_gwm_file"+File.separator; - }else { - //鎵撳寘璺緞 - baseDirPath = jarFile.toString()+File.separator+"battery_gwm_file"+File.separator; - } - return baseDirPath; - } - - /** - * 鑾峰彇椤圭洰鎵�鍦ㄦ枃浠跺す璺緞 - * @return 鑾峰彇椤圭洰鎵�鍦ㄦ枃浠跺す璺緞 - */ - public static String getProjectDir(){ - ApplicationHome applicationHome = new ApplicationHome(CommonUtil.class); - File jarFile = applicationHome.getDir(); - String baseDirPath; - if(YamlProperties.runModel == 1) { - //寮�鍙戣矾寰� - baseDirPath = jarFile.getParentFile().toString(); - }else { - //鎵撳寘璺緞 - baseDirPath = jarFile.toString(); - } - return baseDirPath; - } - - /* - * 鑾峰彇HttpServletRequest - */ - public static HttpServletRequest getRequest(){ - ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); - HttpServletRequest request = requestAttributes.getRequest(); - return request; - } - - /* - * 鑾峰彇HttpServletResponse - */ - public static HttpServletResponse getResponse(){ - ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); - HttpServletResponse response = requestAttributes.getResponse(); - return response; - } - - /* - * 鑾峰彇HttpSession - */ - public static HttpSession getSession(){ - return getRequest().getSession(); - } - - /** - * - * @return 杩斿洖application - */ - public static ServletContext getApplication(){ - return getSession().getServletContext(); - } - - public static User getUser(){ - - User principal = (User) SecurityUtils.getSubject().getPrincipal(); - if(principal == null){ - User user = new User(); - user.setName("unLogged-in user"); - user.setId(0); - return user; - }else { - return principal; - } - } - - //鑾峰彇application涓簰鏂ヤ笂浼犵殑鏍囧織浣峟lag - public static int getFlag(){ - ServletContext app=getApplication(); - int flag=(Integer) app.getAttribute("flag"); - return flag; - } - ////缁檃pplication涓簰鏂ヤ笂浼犵殑鏍囧織浣峟lag璁剧疆鍊� - public static void setFlag(int flag){ - ServletContext app=getApplication(); - app.setAttribute("flag", flag); - } - /* - * 鑾峰彇灏嗗璞¤浆鎹㈡垚json鏍煎紡 - */ - public static String tojson(Object obj){ - Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); - return gson.toJson(obj); - } - - public static String chageDateToString(Date time,String type){ - String msg = ""; - SimpleDateFormat sdf = new SimpleDateFormat(type); - msg = sdf.format(time); - return msg; - } - - - /** - * - * @param datetype 闇�瑕佽В鏋愮殑鏃ユ湡鐨勬牸寮忓锛�"yyyy-MM-dd HH:mm:ss" - * @return 寰楀埌瀵瑰簲鐨刧son瀵硅薄 - */ - public static Gson getGson(String datetype){ - return new GsonBuilder().setDateFormat(datetype).create(); - } - - /** - * 鑾峰彇榛樿鐨刧son瀵硅薄 - * @return - */ - public static Gson getGson(){ - return new Gson(); - } - - - public static <T> T getObject(String jsonstring,Type listtype){ - Gson gson=new Gson(); - T t=null; - try { - t=gson.fromJson(jsonstring, listtype); - } catch (JsonSyntaxException e) { - e.printStackTrace(); - } - return t; - } - - /** - * - * @return 鑾峰彇杈撳嚭娴� - */ - public static PrintWriter getOut(){ - PrintWriter out =null; - try { - out=getResponse().getWriter(); - } catch (IOException e) { - e.printStackTrace(); - } - return out; - } - - /** - * - * @param time java.util鍖呬腑鐨勬椂闂� - * @return java.sql鏃堕棿 - */ - public static Date getSimpDate(Date time){ - //System.out.println(time); - return new java.sql.Date(time.getTime()); - } - - //浣跨敤Base64鍔犲瘑 - public static Object EncryptionBase64(Object obj){ - String base64=null; - if(obj!=null && obj.toString().length()>0){ - base64=new BASE64Encoder().encode(obj.toString().getBytes()); - base64 = base64.replaceAll("[\\s*\t\n\r]", ""); //鏇挎崲鍔犲瘑鍚庝笉鎹㈣ - } - return base64; - } - - //浣跨敤Base64瑙e瘑 - public static Object DecryptionBase64(Object obj){ - String base64=null; - if(obj!=null && obj.toString().length()>0){ - try { - base64 = new String(new BASE64Decoder().decodeBuffer(obj.toString())); - } catch (IOException e) { - e.printStackTrace(); - } - } - return base64; - } - - //鑾峰彇鎸囧畾骞存湀鐨勫ぉ鏁� - public static int getDaysByYearMonth(int year, int month) { - - Calendar a = Calendar.getInstance(); - a.set(Calendar.YEAR, year); - a.set(Calendar.MONTH, month - 1); - a.set(Calendar.DATE, 1); - a.roll(Calendar.DATE, -1); - int maxDate = a.get(Calendar.DATE); - return maxDate; + @Autowired + public void setUserLogService(UserLogService userLogService) { + CommonUtil.userLogService = userLogService; } - //鑾峰彇鏌愬勾鏌愭湀鐨勮捣濮嬪拰缁撴潫鏃堕棿 - //闇�瑕佹敞鎰忕殑鏄細鏈堜唤鏄粠0寮�濮嬬殑锛屾瘮濡傝濡傛灉杈撳叆5鐨勮瘽锛屽疄闄呬笂鏄剧ず鐨勬槸4鏈堜唤鐨勬渶鍚庝竴澶╋紝鍗冧竾涓嶈鎼為敊浜嗗摝 - public static String getLastDayOfMonth(int year, int month) { - Calendar cal = Calendar.getInstance(); - cal.set(Calendar.YEAR, year); - cal.set(Calendar.MONTH, month); - cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DATE)); - return new SimpleDateFormat( "yyyy-MM-dd ").format(cal.getTime())+"23:59:59"; - } - - public static String getFirstDayOfMonth(int year, int month) { - Calendar cal = Calendar.getInstance(); - cal.set(Calendar.YEAR, year); - cal.set(Calendar.MONTH, month); - cal.set(Calendar.DAY_OF_MONTH,cal.getMinimum(Calendar.DATE)); - return new SimpleDateFormat( "yyyy-MM-dd ").format(cal.getTime())+"00:00:00"; - } - //鑾峰彇褰撳墠鏃堕棿鐨勫勾浠� - public static int getNowYear(){ - Calendar ca = Calendar.getInstance();//寰楀埌涓�涓狢alendar鐨勫疄渚� - ca.setTime(new Date()); //璁剧疆鏃堕棿涓哄綋鍓嶆椂闂� - int year = ca.get(Calendar.YEAR); - //System.out.println(month); - return year; - } - //鑾峰彇褰撳墠鏃堕棿鐨勬湀浠� - public static int getNowMonth(){ - Calendar ca = Calendar.getInstance();//寰楀埌涓�涓狢alendar鐨勫疄渚� - ca.setTime(new Date()); //璁剧疆鏃堕棿涓哄綋鍓嶆椂闂� - int month = ca.get(Calendar.MONTH) + 1; - //System.out.println(month); - return month; - } - //鑾峰彇褰撳墠鏃堕棿鐨勬棩鏈� - public static int getNowday(){ - Calendar ca = Calendar.getInstance();//寰楀埌涓�涓狢alendar鐨勫疄渚� - ca.setTime(new Date()); //璁剧疆鏃堕棿涓哄綋鍓嶆椂闂� - int day = ca.get(Calendar.DAY_OF_MONTH); - //System.out.println(month); - return day; - } - //褰撳墠鏃堕棿鍔犱笂x灏忔椂 - public static String getDateAdd(int minute){ - Date date = new Date(); - Calendar c = Calendar.getInstance(); - c.setTime(date); - c.add(Calendar.MINUTE, minute); - return new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss").format(c.getTime()); + /**鑾峰彇褰撳墠Session涓殑灞炴�ser*/ + public static User getUser(HttpServletRequest request) { + return (User) request.getSession().getAttribute("user"); } - //浣跨敤MD5鍔犲瘑 - public static Object EncryptionMD5(Object obj){ - String md5=null; - if(obj!=null && obj.toString().length()>0){ - md5= DigestUtils.md5Hex(obj.toString()); - } - return md5; - } - - //鑾峰彇鏃堕棿鐨勫勾浠� - public static int getDateYear(Date date){ - Calendar ca = Calendar.getInstance();//寰楀埌涓�涓狢alendar鐨勫疄渚� - ca.setTime(date); //璁剧疆鏃堕棿涓哄綋鍓嶆椂闂� - int year = ca.get(Calendar.YEAR); - //System.out.println(month); - return year; + public static String classesPath(){ + ApplicationHome applicationHome = new ApplicationHome(CommonUtil.class); + File jarFile = applicationHome.getDir(); + return jarFile.toString(); } - //鑾峰彇鏃堕棿鐨勬湀浠� - public static int getDateMonth(Date date){ - Calendar ca = Calendar.getInstance();//寰楀埌涓�涓狢alendar鐨勫疄渚� - ca.setTime(date); //璁剧疆鏃堕棿涓哄綋鍓嶆椂闂� - int month = ca.get(Calendar.MONTH) + 1; - //System.out.println(month); - return month; - } - //鑾峰彇鏃堕棿鐨勬棩鏈� - public static int getDateday(Date date){ - Calendar ca = Calendar.getInstance();//寰楀埌涓�涓狢alendar鐨勫疄渚� - ca.setTime(date); //璁剧疆鏃堕棿涓哄綋鍓嶆椂闂� - int day = ca.get(Calendar.DAY_OF_MONTH); - //System.out.println(month); - return day; - } - //褰撳墠鏃堕棿鍔犱笂x灏忔椂 - public static Date getDateAdd(Date date,int minute){ - Calendar c = Calendar.getInstance(); - c.setTime(date); - c.add(Calendar.MINUTE, minute); - return c.getTime(); - } - //缁欏畾鏃堕棿鍒ゆ柇涓庡綋鍓嶆椂闂寸浉姣旀槸鍚︿负鑰佺數姹�(numYear涓烘椂闂存湡闄愬嚑骞�) - public static int judgeBatt(Date inUseDate,Date date,int numYear){ - int judge=0; - inUseDate.setYear(CommonUtil.getDateYear(inUseDate)+numYear-1900); - //System.out.println("inUseDate: "+inUseDate); - if(inUseDate.after(date)){ - judge=1; - }else{ - judge=0; - } - return judge; - } - //鑾峰彇鏌愬勾鏌愭湀鏌愭棩鍐呯殑鏃堕棿<date涓烘墽琛屾椂闂寸殑鏃跺垎绉掕缃负鎻愰啋鏃堕棿娈电殑鏃跺垎绉�> - public static Date getWorkDay(Date date,Date noticeTime) { - Calendar cal = Calendar.getInstance(); - cal.setTime(date); //璁剧疆鏃堕棿涓烘墽琛屾棩鏈� - int year = cal.get(Calendar.YEAR);//骞� - int month = cal.get(Calendar.MONTH);//鏈� - int day = cal.get(Calendar.DAY_OF_MONTH);//鏃� - cal.setTime(noticeTime);//璁剧疆鏃堕棿涓烘彁閱掓椂闂存 - /*int hour=cal.get(Calendar.HOUR_OF_DAY);//鏃� - int minute=cal.get(Calendar.MINUTE);//鍒� - int second=cal.get(Calendar.SECOND);//绉�*/ - cal.set(Calendar.YEAR, year); - cal.set(Calendar.MONTH, month); - cal.set(Calendar.DAY_OF_MONTH,day); - return cal.getTime(); - } - //鑾峰彇鏌愬勾鏌愭湀鏌愭棩璧峰鏃堕棿/缁撴潫鏃堕棿 - public static String getDayTime(int year, int month,int day,int type) { - Calendar cal = Calendar.getInstance(); - cal.set(Calendar.YEAR, year); - cal.set(Calendar.MONTH, month); - cal.set(Calendar.DAY_OF_MONTH,day); - if(type==1){ - return new SimpleDateFormat( "yyyy-MM-dd ").format(cal.getTime())+"00:00:00"; - - } else if(type==2){ - return new SimpleDateFormat( "yyyy-MM-dd ").format(cal.getTime())+"09:00:00"; - }else if(type==3){ - return new SimpleDateFormat( "yyyy-MM-dd ").format(cal.getTime())+"17:00:00"; - }else if(type==4){ - return new SimpleDateFormat( "yyyy-MM-dd ").format(cal.getTime())+"23:59:59"; + + public static String getRootFile(){ + ApplicationHome applicationHome = new ApplicationHome(CommonUtil.class); + File jarFile = applicationHome.getDir(); + String baseDirPath; + if(YamlProperties.runModel == 1) { + //寮�鍙戣矾寰� + baseDirPath = jarFile.getParentFile().toString()+File.separator+"fg_file"+File.separator; + }else { + //鎵撳寘璺緞 + baseDirPath = jarFile.toString()+File.separator+"fg_file"+File.separator; } - return new SimpleDateFormat( "yyyy-MM-dd ").format(cal.getTime()); - } + return baseDirPath; + } - //璁$畻鐢垫睜浣跨敤浜嗗灏戜釜鏈� - public static int battInuseMonth(Date inUseDate){ - int useDay=0; - Date nowd=new Date();//褰撳墠鏃堕棿 - int inuseY= CommonUtil.getDateYear(inUseDate); - int inuseM= CommonUtil.getDateMonth(inUseDate); - int nowY= CommonUtil.getDateYear(nowd); - int nowM= CommonUtil.getDateMonth(nowd); - if(inuseY==nowY){ - useDay=nowM-inuseM; - }else{ - useDay=(nowY-inuseY)*12+nowM-inuseM; - } - return useDay; - - } - //璁$畻鏃ユ湡涔嬮棿鐩稿樊鐨勫ぉ鏁� - public static int daysBetween(Date smdate,Date bdate) - { - SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); - - try { - smdate=sdf.parse(sdf.format(smdate)); - bdate=sdf.parse(sdf.format(bdate)); - } catch (ParseException e) { - e.printStackTrace(); - } - - Calendar cal = Calendar.getInstance(); - cal.setTime(smdate); - long time1 = cal.getTimeInMillis(); - cal.setTime(bdate); - long time2 = cal.getTimeInMillis(); - long between_days=(time2-time1)/(1000*3600*24); - - return Math.abs(Integer.parseInt(String.valueOf(between_days))); - } - //璁$畻鏃ユ湡涔嬮棿鐩稿樊鐨勫皬鏃� - public static int HoursBetween(Date smdate,Date bdate) - { - SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - try { - smdate=sdf.parse(sdf.format(smdate)); - bdate=sdf.parse(sdf.format(bdate)); - } catch (ParseException e) { - e.printStackTrace(); - } - Calendar cal = Calendar.getInstance(); - cal.setTime(smdate); - long time1 = cal.getTimeInMillis(); - cal.setTime(bdate); - long time2 = cal.getTimeInMillis(); - long between_hours=(time2-time1)/(1000*3600); - //System.out.println("between_hours:"+between_hours); - return (int) Math.abs(between_hours); - } - - //姣旇緝涓や釜鏃堕棿鐨勫ぇ灏� - public static int compareDate(Date d1,Date d2){ - if (d1.getTime() > d2.getTime()) { - return 1; - } else if (d1.getTime() < d2.getTime()) { - return -1; - } else {//鐩哥瓑 - return 0; - } - } /** - * @Description: 鏍规嵁鍥剧墖鍦板潃杞崲涓篵ase64缂栫爜瀛楃涓� - * @Author: - * @CreateTime: - * @return + * 鑾峰彇椤圭洰鎵�鍦ㄦ枃浠跺す璺緞 + * @return 鑾峰彇椤圭洰鎵�鍦ㄦ枃浠跺す璺緞 */ - public static String getImageStr(String imgFile) { - InputStream inputStream = null; - byte[] data = null; - try { - inputStream = new FileInputStream(imgFile); - data = new byte[inputStream.available()]; - inputStream.read(data); - inputStream.close(); - } catch (IOException e) { - e.printStackTrace(); + public static String getProjectDir(){ + ApplicationHome applicationHome = new ApplicationHome(CommonUtil.class); + File jarFile = applicationHome.getDir(); + String baseDirPath; + if (YamlProperties.runModel == 1) { + //寮�鍙戣矾寰� + baseDirPath = jarFile.getParentFile().toString(); + } else { + //鎵撳寘璺緞 + baseDirPath = jarFile.toString(); } - // 鍔犲瘑 - BASE64Encoder encoder = new BASE64Encoder(); - return encoder.encode(data); + return baseDirPath; } - - + /** - * @Description: 灏哹ase64缂栫爜瀛楃涓茶浆鎹负鍥剧墖 - * @Author: - * @CreateTime: - * @param imgStr base64缂栫爜瀛楃涓� - * @param path 鍥剧墖璺緞-鍏蜂綋鍒版枃浠� - * @return - */ - public static boolean generateImage(String imgStr, String path) { - if (imgStr == null) { - return false; - } - BASE64Decoder decoder = new BASE64Decoder(); - try { - // 瑙e瘑 - byte[] b = decoder.decodeBuffer(imgStr); - // 澶勭悊鏁版嵁 - for (int i = 0; i < b.length; ++i) { - if (b[i] < 0) { - b[i] += 256; - } - } - OutputStream out = new FileOutputStream(path); - out.write(b); - out.flush(); - out.close(); - return true; - } catch (Exception e) { - return false; - } - } - - /** - * 灏嗚緭鍏ユ祦涓殑鏁版嵁璇诲埌瀛楄妭鏁扮粍涓� - * @param is - * @return + * 鎵嬪姩璁板綍鐗瑰畾鏃ュ織 */ - public static byte[] IStoByteArr(InputStream is){ - byte[] b = null; - try { - b = new byte[is.available()]; - is.read(b); - } catch (IOException e) { - e.printStackTrace(); - } finally{ - if(is != null){ - try { - is.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - return b; + public static void record(int category, int type, String message,String messageDetail) { + userLogService.add(category, type, message, messageDetail); } - /** - * 鍒涘缓鎸囧畾鐨勬枃浠舵枃浠讹紝鑻ヤ笉瀛樺湪鍒欏厛鍒涘缓鎸囧畾鐨勬枃浠跺す鍐嶅垱寤烘寚瀹氱殑鏂囦欢 - * @param filePath - */ - public static void createFilefolderIFNotExist(String filePath){ - File f = new File(filePath); - if(!f.exists()){ - if(!f.getParentFile().exists()){ - f.getParentFile().mkdirs(); - } - } - } - /** - * 绉掕浆鎹㈠皬鏃�-鍒�-绉抋nalytics/util/DateUtil.java - * - * @param seconds 绉掍负鍗曚綅 姣斿..600绉� - * @return 姣斿...2灏忔椂3鍒嗛挓52绉� - */ - public static String secToTime(int seconds) { - int hour = seconds / 3600; - int minute = (seconds - hour * 3600) / 60; - int second = (seconds - hour * 3600 - minute * 60); - - StringBuffer sb = new StringBuffer(); - if (hour > 0) { - if(hour<10){ - sb.append("0"+hour + ":"); - }else{ - sb.append(hour + ":"); - } - }else{ - sb.append("00"+ ":"); - } - if (minute > 0) { - if(minute<10){ - sb.append("0"+minute + ":"); - }else{ - sb.append(minute + ":"); - } - }else{ - sb.append("00" + ":"); - } - if (second > 0) { - if(second<10){ - sb.append("0"+second); - }else{ - sb.append(second); - } - }else{ - sb.append("00"); - } - return sb.toString(); - } - - //鑾峰彇褰撳墠绯荤粺鐨勮瑷�鐜 - public static String getLang() { - Locale locale = Locale.getDefault();//瀵筁ocale绫诲疄渚嬪寲瀹氫箟 - String lang = locale.getLanguage(); - String str = (String) CommonUtil.getSession().getAttribute("lang"); - if (str != null) { - lang = str; - } - return lang; - } } -- Gitblit v1.9.1