ssh generates a random number captcha operation step
- 2020-06-01 10:47:01
- OfStack
1. login.jsp page program
Add the following sentence to the form:
2. RandomNumUtil.java class file for generating captcha
3. RandomAction.java: action program that generates captcha
4. LoginAction.java verification code action
5. Configure the struts.xml file
6. Picture demonstration of generated verification code (implemented six-digit verification code)
Description:
If you want to modify the number of captcha generated, you need to modify the following:
Point 1:
int width=85, height=20; int width=85, height=20;
Point 2: for (int i=0; i < 6;i++) for (int i=0;i < 6;i++)
Change the number 6 to the number you want to generate
<script type="text/javascript">
function changeValidateCode(obj) {
// Gets the current time as a parameter
var timenow = new Date().getTime();
// Each request requires 1 Two different arguments, otherwise the same captcha might be returned
// This has to do with the browser's caching mechanism. You can also set the page to not be cached so that you don't have to use this parameter.
obj.src="rand.action?d="+timenow;
}
</script>
Add the following sentence to the form:
<s:text name="random"></s:text> : <s:textfield name="rand" size="5"></s:textfield><img src="rand.action" onclick="changeValidateCode(this)" title=" Click the image to refresh the captcha "/>
2. RandomNumUtil.java class file for generating captcha
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
public class RandomNumUtil {
private ByteArrayInputStream image;// image
private String str;// Verification code
private RandomNumUtil(){
init();// Initialization property
}
/*
* achieve RandomNumUtil The instance
*/
public static RandomNumUtil Instance(){
return new RandomNumUtil();
}
/*
* Get the captcha image
*/
public ByteArrayInputStream getImage(){
return this.image;
}
/*
* Get the captcha of the image
*/
public String getString(){
return this.str;
}
private void init() {
// Create an image in memory
int width=85, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// Get graphic context
Graphics g = image.getGraphics();
// Generate random class
Random random = new Random();
// Setting the background color
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
// Set the font
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
// Randomly generated 155 Interference line, so that the authentication code in the image is not easily detected by other programs
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}
// Take the randomly generated authentication code (6 A digital )
String sRand="";
for (int i=0;i<6;i++){
String rand=String.valueOf(random.nextInt(10));
sRand+=rand;
// Displays the authentication code in the image
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
// Call the function out of the same color, perhaps because the seed is too close, so can only be generated directly
g.drawString(rand,13*i+6,16);
}
// Assign captcha
this.str=sRand;
// The image effect
g.dispose();
ByteArrayInputStream input=null;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try{
ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
ImageIO.write(image, "JPEG", imageOut);
imageOut.close();
input = new ByteArrayInputStream(output.toByteArray());
}catch(Exception e){
System.out.println(" Error in captcha image: "+e.toString());
}
this.image=input;/* Assignment image */
}
/*
* Get a random color for a given range
*/
private Color getRandColor(int fc,int bc){
Random random = new Random();
if(fc>255) fc=255;
if(bc>255) bc=255;
int r=fc+random.nextInt(bc-fc);
int g=fc+random.nextInt(bc-fc);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
}
3. RandomAction.java: action program that generates captcha
import java.io.ByteArrayInputStream;
import com.mxl.util.RandomNumUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class RandomAction extends ActionSupport{
private ByteArrayInputStream inputStream;
public String execute() throws Exception{
RandomNumUtil rdnu=RandomNumUtil.Instance();
this.setInputStream(rdnu.getImage());// Gets a picture with a random string
ActionContext.getContext().getSession().put("random", rdnu.getString());// Get a random string and put it in HttpSession
return SUCCESS;
}
public void setInputStream(ByteArrayInputStream inputStream) {
this.inputStream = inputStream;
}
public ByteArrayInputStream getInputStream() {
return inputStream;
}
}
4. LoginAction.java verification code action
private String rand; // In the form rand
public String getRand() {
return rand;
}
public void setRand(String rand) {
this.rand = rand;
}
// from session Remove the RandomAction.java The captcha generated in random
String arandom=(String)(ActionContext.getContext().getSession().get("random"));
// So here's how session Save the captcha string in compare to the captcha string entered by the customer
if(arandom.equals(this.getRand())) {
ActionContext.getContext().getSession().put("user", this.getUsername());
return SUCCESS;
}
else {
return ERROR;
}
5. Configure the struts.xml file
<!-- Random Verification code -->
<action name="rand" class="com.mxl.rand.RandomAction">
<result type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">inputStream</param>
</result>
</action>
6. Picture demonstration of generated verification code (implemented six-digit verification code)
Description:
If you want to modify the number of captcha generated, you need to modify the following:
Point 1:
int width=85, height=20; int width=85, height=20;
Point 2: for (int i=0; i < 6;i++) for (int i=0;i < 6;i++)
Change the number 6 to the number you want to generate