baseController中的一些封装
来源:互联网 发布:深圳中科软件 编辑:程序博客网 时间:2024/06/05 08:41
protected static final Logger logger = Logger.getLogger(Constants.LOG_INFO); protected HttpServletRequest request; protected HttpServletResponse response; protected HttpSession session; public int defEnd = 0; public int defSTART = 29; @ModelAttribute public void setReqAndResp(HttpServletRequest request, HttpServletResponse response){ this.request = request; this.response = response; this.session = request.getSession(); } @InitBinder protected void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Date.class, new CustomDateEditor( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true)); binder.registerCustomEditor(Date.class, new CustomDateEditor( new SimpleDateFormat("yyyy-MM-dd"), true)); } /** * 输出信息到页面 * @param msg:要输出的信息(可以是js脚本等) */ public void printOutMsg(String msg){ try { this.response.setCharacterEncoding("UTF-8"); this.response.setContentType("text/html;charset=utf-8"); PrintWriter out = null; out = this.response.getWriter(); out.print(msg); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 输出信息到页面 * @param response * @param msg */ public void printOutMsg(HttpServletResponse response, String msg){ try { response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = null; out = response.getWriter(); out.print(msg); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 获取请求完整路径 * @param request * @return */ public String getUrl(HttpServletRequest request){ String url = request.getRequestURI(); String params = ""; if(request.getQueryString()!=null){ params = request.getQueryString().toString(); } if(!"".equals(params)){ url = url+"?"+params; } return url; } /** * 获取日期 * @param day 天 */ public String getDate(int day){ StringBuffer s = new StringBuffer(); Calendar c = Calendar.getInstance(); int currentDay = c.get(Calendar.DATE); if(day < 0){ c.add(Calendar.YEAR, -1); c.set(Calendar.DATE, currentDay); }else if(day == 29){ c.add(Calendar.MONTH, -1); c.set(Calendar.DATE, currentDay); }else{ c.add(Calendar.DATE, -day); } s.append(c.get(Calendar.YEAR)+"-"); s.append((c.get(Calendar.MONTH)+1) < 10 ? ("0"+(c.get(Calendar.MONTH)+1)) : (c.get(Calendar.MONTH)+1)); s.append("-"); s.append(c.get(Calendar.DATE) < 10 ? ("0"+c.get(Calendar.DATE)) : c.get(Calendar.DATE)); return s.toString(); } /** * 转换统计的map * @param statMap 统计的map * @param constMap 常量的map * @return */ public Map<String, Long> getFmtMap(Map<String, Long> statMap, Map<Integer, String> constMap){ Map<String, Long> dataMap = null; if(statMap != null){ dataMap = new LinkedHashMap<String, Long>(); for(Entry<String, Long> entry : statMap.entrySet()){ dataMap.put(constMap.get(Integer.valueOf(entry.getKey()))+"&"+Integer.valueOf(entry.getKey()), entry.getValue()); } } return dataMap; } /** * 获取登录用户信息 * @return */ public SessionUser getSessionUser(){ Object session = request.getSession().getAttribute(Constants.KEY_SESSION_USER); return session == null ? null : (SessionUser) session; } /** * 获取token * @param type(1:登录;2:注册;3:找回密码) * @return */ protected RSAToken getRSAToken(int type){ // 生成一个token String token = UUID.randomUUID().toString(); if(type == 1){ session.setAttribute(Constants.KEY_SESSION_TOKEN_LOGIN, token); }else if(type == 2){ session.setAttribute(Constants.KEY_SESSION_TOKEN_REGISTER, token); }else if(type == 3){ session.setAttribute(Constants.KEY_SESSION_TOKEN_FIND_PWD, token); } // 生成公钥信息 RSAPublicKey publicKey = RSAUtils.getDefaultPublicKey(); String modulus = new String(Hex.encodeHex(publicKey.getModulus().toByteArray())); String exponent = new String(Hex.encodeHex(publicKey.getPublicExponent().toByteArray())); RSAToken rsaToken = new RSAToken(); rsaToken.setToken(token); rsaToken.setModulus(modulus); rsaToken.setExponent(exponent); return rsaToken; } /** *获取后台登陆用户信息 * */ public SessionUser getSessionAdminUser(){ AiUserInfo session = (AiUserInfo) request.getSession().getAttribute(Constants.KEY_SESSION_ADMIN_USER); SessionUser adminUser = new SessionUser(); adminUser.setUserId(session.getId()); adminUser.setUsername(session.getUserName()); return adminUser == null ? null : adminUser; } /** * 获取用户加密信息 * @return */ public String getDesUserInfo(){ SessionUser sessionUser = getSessionUser(); String encryptText = ""; if(sessionUser != null){ String plainText = sessionUser.getUsername()+"&"+sessionUser.getUserId(); String secretKey = "3dbfac1abd334784860723ef2022a92f"; try { encryptText = URLEncoder.encode(Des3.DESEncode(plainText, secretKey), "UTF-8"); } catch (Exception e) { e.printStackTrace(); } } return encryptText; } /** * 设置用户session * @param user */ public void setSessionUser(AiUserInfo user){ if(user != null){ SessionUser sessionUser = new SessionUser(); sessionUser.setUserId(user.getId()); if(user.getUserName() != null && !"".equals(user.getUserName())){ sessionUser.setUsername(user.getUserName()); } else { if(user.getPhone() != null && !"".equals(user.getPhone())){ sessionUser.setUsername(user.getPhone()); }else { sessionUser.setUsername(user.getNickName()); } } sessionUser.setPhone(user.getPhone()); sessionUser.setUserType(user.getUserType()==null?0:user.getUserType()); sessionUser.setVipStatus(user.getVipLevel()==null?0:user.getVipLevel()); sessionUser.setLogoPath(user.getLogoPath()); session.setAttribute(Constants.KEY_SESSION_USER, sessionUser); } } public static void main(String[] args) throws Exception, Exception { String plainText = "zhouqi"+"&"+"8"; String secretKey = "3dbfac1abd334784860723ef2022a92f"; String dd = Des3.DESEncode(plainText, secretKey); System.out.println(dd); String encryptText = URLEncoder.encode(Des3.DESEncode(plainText, secretKey), "UTF-8"); System.out.println(encryptText); }
阅读全文
0 0
- baseController中的一些封装
- BaseController的封装
- BaseController
- 【iOS】BaseController的用处
- BaseController的写法
- SSM 中 BaseController 使用 session
- 封装一些组合控件
- 一些框架封装
- 一些异常封装
- OkHttp的一些封装
- OkHttp3Utils的一些封装
- 一些常用的函数封装
- 封装了一些辅助类
- EasyUI的一些封装方法
- py windows一些封装函数
- android 封装一些高效方法
- js的一些函数封装
- string封装一些小函数
- Java泛型
- rabbitmq学习(java客户端)
- Error:Could not determine the class-path for interface com.android.builder.model.AndroidProject.
- xp下能用ffmeeg库的工具
- Mysql几种索引类型的区别及适用情况
- baseController中的一些封装
- 全面理解Unity加载和内存管理
- ActiveMQ发送queue消息
- spring-servlet.xml 和 application.xml 配置含义及bean被加载两次的问题
- java基础2 面向对象三等基本特征,五大基本特性
- 用了一年wkwebview中遇到的坑
- JIT准时生产制造管理
- 0基础lua学习(十八)C调用Lua----02Lua堆栈
- Android-----WebView使用