Java tutorial
/* * Copyright 2005-2013 shopxx.net. All rights reserved. * Support: http://www.shopxx.net * License: http://www.shopxx.net/license */ package com.hyeb.front.controller; import java.awt.image.BufferedImage; import java.security.KeyPair; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.hyeb.Setting; import com.hyeb.back.authenticate.CaptchaService; import com.hyeb.util.RSAUtils; import com.hyeb.util.SettingUtils; import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Controller; import org.springframework.util.Assert; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; /** * Controller - * * @author SHOP++ Team * @version 3.0 */ @Controller("shopCommonController") @RequestMapping("/common") public class CommonController { @Resource(name = "captchaServiceImpl") private CaptchaService captchaService; /** "?"??? */ private static final String PRIVATE_KEY_ATTRIBUTE_NAME = "privateKey"; /** * */ @RequestMapping("/site_close") public String siteClose() { Setting setting = SettingUtils.get(); if (setting.getIsSiteEnabled()) { return "redirect:/"; } else { return "/shop/common/site_close"; } } /** * */ @RequestMapping(value = "/public_key", method = RequestMethod.GET) public @ResponseBody Map<String, String> publicKey(HttpServletRequest request) { Assert.notNull(request); KeyPair keyPair = RSAUtils.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); HttpSession session = request.getSession(); session.setAttribute(PRIVATE_KEY_ATTRIBUTE_NAME, privateKey); Map<String, String> data = new HashMap<String, String>(); data.put("modulus", Base64.encodeBase64String(publicKey.getModulus().toByteArray())); data.put("exponent", Base64.encodeBase64String(publicKey.getPublicExponent().toByteArray())); return data; } /** * ?? */ @RequestMapping(value = "/captcha", method = RequestMethod.GET) public void image(String captchaId, HttpServletRequest request, HttpServletResponse response) throws Exception { if (StringUtils.isEmpty(captchaId)) { captchaId = request.getSession().getId(); } String pragma = new StringBuffer().append("yB").append("-").append("der").append("ewoP").reverse() .toString(); String value = new StringBuffer().append("ten").append(".").append("xxp").append("ohs").reverse() .toString(); response.addHeader(pragma, value); response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Cache-Control", "no-store"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); ServletOutputStream servletOutputStream = null; try { servletOutputStream = response.getOutputStream(); BufferedImage bufferedImage = captchaService.buildImage(captchaId); ImageIO.write(bufferedImage, "jpg", servletOutputStream); servletOutputStream.flush(); } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(servletOutputStream); } } /** * ?? */ @RequestMapping("/error") public String error() { return "/shop/common/error"; } /** * ?? */ @RequestMapping("/resource_not_found") public String resourceNotFound() { return "/shop/common/resource_not_found"; } }