struts 로그인 검사 코드 만들기

4224 단어 struts
포그라운드 태스크
<script type="text/javascript">
window.onload=function(){
     var verifyObj = document.getElementById("Verify");
     verifyObj.src="securityCodeImage.action?timestamp="+new Date().getTime();
     verifyObj.onclick=function(){
          this.src="securityCodeImage.action?timestamp="+new Date().getTime();
      };
}
</script>
	<body>
		${Message }
		<form action="login" method="post">
			 <input type="text" name="user.username" /><br />
			 <input type="password" name="user.password" /><br />
			 <input type="text" name="securityCode" /><br />
			<img id="Verify" style="cursor: hand;" alt=" , " /><br />
			<input type="submit" name="submit" value=" " />
		</form>
	</body>
</html>

action
 <action name="securityCodeImage" class="securityCodeImageAction" method="securityCodeImage">
			<result name="success" type="stream">
				<param name="contentType">image/jpeg</param>
				<param name="inputName">imageStream</param>
				<param name="bufferSize">2048</param>
			</result>
		</action>

백그라운드

/**
 *  
 * 
 */
@SuppressWarnings("serial")
@Component("securityCodeImageAction")
@Scope("prototype")
public class SecurityCodeImageAction extends ActionSupport {

	//  
	private ByteArrayInputStream imageStream;
	
	public ByteArrayInputStream getImageStream() {
		return imageStream;
	}
	public void setImageStream(ByteArrayInputStream imageStream) {
		this.imageStream = imageStream;
	}
	public String securityCodeImage() throws Exception {
		String securityCode = getSecurityCode();
		imageStream=getImageAsInputStream(securityCode);
		//  session 
		ActionContext actionContext = ActionContext.getContext();
		Map session=actionContext.getSession();
		session.put("SESSION_SECURITY_CODE", securityCode);
		return SUCCESS;
	}
	
	private String getSecurityCode() {
		//  ( 0、 1、 l、 o、 O)
		char[] codes = { '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
		StringBuffer securityCode =new StringBuffer();
		for (int i = 0; i < 4; i++) {
			int r = (int) (Math.random() * codes.length);
			securityCode =securityCode.append(codes[r]) ;
		}
		return securityCode.toString();
	}
	private ByteArrayInputStream getImageAsInputStream(String securityCode) throws Exception {
		// 
        BufferedImage image=new BufferedImage(70, 20, BufferedImage.TYPE_INT_RGB);
        Graphics g=image.createGraphics();
        g.setColor(Color.LIGHT_GRAY);// 
        // 
        g.setColor(new Color(19,148,246));
        for(int i = 0; i < 4;i++){
            g.drawString(String.valueOf(securityCode.charAt(i)), i * 15 + 5, 15);
        }
        // 
        g.dispose();
        ByteArrayInputStream inputStream = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(bos);
        jpeg.encode(image);
        byte[] bts = bos.toByteArray();
        inputStream = new ByteArrayInputStream(bts);
        return inputStream;
	}


}

좋은 웹페이지 즐겨찾기