在多人开发同一个javaweb工程时,可以把java工程从外部调用,不必都写到一个javaweb工程,
通过将二维码和加密的java程序都写成单独的java工程,最后用一个javaweb工程调用,效果图如下
子工程目录结构
javaweb工程目录(注意子工程的jar包也要拿过来)
eclipse配置步骤:
主工程右键=>java build path =>projects=>add=>选择两个子工程
java build path=>libraries=>add class folder=>选中两个子工程的bin文件夹
java build path=>order and export=>选中子工程
Deployment assembly=>project=>选中子工程
配置文件和有关验证码的那篇随笔是一样的,这里不多说
userlogin.html
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>验证码??</title> <script src="jquery/jquery-2.1.3.min.js" type="text/javascript"></script> </head> <body> <div style="text-align:center;margin-top:200px;"> <input id="password" type="password" placeholder="输入密码" style="width:200px;height:30px;"/><br> <input id="key" type="password" placeholder="输入秘钥" style="width:200px;height:30px;"/><br> <input id="key" type="button" value="确认" onclick="javascript:checkedOk()" style="width:80px;height:30px;font-family:黑体;"/> <font color="red" id="message"></font> </div> <div id="erweima" style="text-align:center;margin-left:57px;"></div> </body> <script type="text/javascript"> function checkedOk(){ var password = document.getElementById("password").value; var key = document.getElementById("key").value; $.ajax({ url:"login.do", data:{password:password,key:key}, success:function(data){ alert(1565); if(data.message == 1){ $("#erweima").html("<img src=‘erweimaimg.do‘>"); $("#message").text(""); }else{ $("#message").text("密码和秘钥不匹配"); $("#erweima").html(""); } } }); } </script> </html>
UserController.java
@Controller public class UserController { @RequestMapping(value = "/erweimaimg") public void showImgCode(HttpServletResponse resp) { try { QRCodeEncoderHandler.encoderQRCode("http://www.baidu.com", resp.getOutputStream()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @RequestMapping(value = "/login.do") @ResponseBody public Object login(HttpServletRequest request) { Map<String, String> message = new HashMap<String, String>(); String password = request.getParameter("password"); String key = request.getParameter("key"); String passwordsure; try { passwordsure = PasswordUtil.encrypt(PasswordUtil.getMD5(password), key); if("Jjpx4STXruE3jZF1gG4D/BGE6w03JQK2szWUdavdsoevuVmxoV52Lw==".equals(passwordsure)){ message.put("message", "1"); }else{ message.put("message", "0"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return message; } }
PasswordUtil.java
public class PasswordUtil { /** * @todo MD5加密 * @return String * @param str */ public static String getMD5(String str) { MessageDigest messageDigest = null; try { messageDigest = MessageDigest.getInstance("MD5"); messageDigest.reset(); messageDigest.update(str.getBytes("UTF-8")); } catch (NoSuchAlgorithmException e) { System.out.println("NoSuchAlgorithmException caught!"); System.exit(-1); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } byte[] byteArray = messageDigest.digest(); StringBuffer md5StrBuff = new StringBuffer(); for (int i = 0; i < byteArray.length; i++) { if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i])); else md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i])); } return md5StrBuff.toString(); } /** * Description 根据键值进行加密 * @param data * @param key 加密键byte数组 * @return * @throws Exception */ public static String encrypt(String data, String key) throws Exception { byte[] bt = encrypt(data.getBytes(), key.getBytes()); String strs = new BASE64Encoder().encode(bt); return strs; } /** * Description 根据键值进行解密 * @param data * @param key 加密键byte数组 * @return * @throws IOException * @throws Exception */ public static String decrypt(String data, String key) throws IOException, Exception { if (data == null) return null; BASE64Decoder decoder = new BASE64Decoder(); byte[] buf = decoder.decodeBuffer(data); byte[] bt = decrypt(buf,key.getBytes()); return new String(bt); } /** * Description 根据键值进行加密 * @param data * @param key 加密键byte数组 * @return * @throws Exception */ private static byte[] encrypt(byte[] data, byte[] key) throws Exception { // 生成一个可信任的随机数源 SecureRandom sr = new SecureRandom(); // 从原始密钥数据创建DESKeySpec对象 DESKeySpec dks = new DESKeySpec(key); // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance("DES"); // 用密钥初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); return cipher.doFinal(data); } /** * Description 根据键值进行解密 * @param data * @param key 加密键byte数组 * @return * @throws Exception */ private static byte[] decrypt(byte[] data, byte[] key) throws Exception { // 生成一个可信任的随机数源 SecureRandom sr = new SecureRandom(); // 从原始密钥数据创建DESKeySpec对象 DESKeySpec dks = new DESKeySpec(key); // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher对象实际完成解密操作 Cipher cipher = Cipher.getInstance("DES"); // 用密钥初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, securekey, sr); return cipher.doFinal(data); } }
QRCodeEncoderHandler.java //生成二维码
public class QRCodeEncoderHandler { public static void encoderQRCode(String content, OutputStream outputStream) { try { Qrcode qrcodeHandler = new Qrcode(); // 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小 qrcodeHandler.setQrcodeErrorCorrect(‘M‘); qrcodeHandler.setQrcodeEncodeMode(‘B‘); qrcodeHandler.setQrcodeVersion(5); byte[] contentBytes = content.getBytes("gb2312"); BufferedImage bufImg = new BufferedImage(250,250,BufferedImage.TYPE_INT_RGB); Graphics2D gs = bufImg.createGraphics(); gs.setBackground(Color.WHITE); gs.clearRect(0, 0, 250, 250); // 设定图像颜色> BLACK gs.setColor(Color.BLACK); // 设置偏移量 不设置可能导致解析出错 int pixoff = 2; // 输出内容> 二维码 if (contentBytes.length > 0 && contentBytes.length < 800) { boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes); for (int i = 0; i < codeOut.length; i++) { for (int j = 0; j < codeOut.length; j++) { if (codeOut[j][i]) { gs.fillRect(j * 5 + pixoff, i * 5 + pixoff, 5, 5); } } } } else { } gs.dispose(); bufImg.flush(); ImageIO.write(bufImg, "png", outputStream); } catch (Exception e) { e.printStackTrace(); } } }
时间: 2024-10-10 22:35:17