您的位置:首页 > 产品设计 > UI/UE

软键盘实例 request参数修改

2016-07-12 16:33 591 查看

软键盘实例 request参数修改



这里的键盘 里面的内容是乱序的,每次请求都会不一样的顺序。



键盘内容是一个DIV的背景图,方框是DIV边框。控制按钮直接在前台控制就行。

请求流程如下

JSP请求软键盘图片—>action 绘制图片并记录 每个值对应的位置

JSP登录(密码位置)—->Filter把密码转换成真实密码值—>登录校验Filter

代码如下

<%@ page isELIgnored="false" %>
<%@ page language="java" contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@ taglib prefix="f" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="renderer" content="webkit">

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-store, must-revalidate">
<meta http-equiv="expires" content="0">
<title>XXXXXXXX</title>
<link rel="shortcut icon" href="r/img/icon/favicon.ico"  type="image/x-icon" />
<link href="r/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css" media="screen, projection">

body{
background-color: #7090A8;
font-family: "Microsoft YaHei", SimHei, SimSun;
padding-top: 90px;
}
.div-signin {
width: 300px;
text-align: center;
margin: 0 auto;
}
.form-signin-title {
color: #fff;
}
.panel-default {
border-radius: 4px;
behavior: url(r/jquery/ie-css3.htc);
}
.panel-default > .panel-heading{
background-color: #fff;
border-bottom: none;
}
.btn {
background-image: none;
border: medium none;
border-radius: 3px;
box-shadow: none;
font-size: 14px;
font-weight: 600;
margin-bottom: 0;
outline: medium none !important;
padding: 8px 16px;
text-shadow: none;
}
.input.btn {
outline: medium none;
}
.btn-lg {
padding: 16px 28px;
}
.btn-sm {
font-size: 12px;
padding: 4px 8px;
}
.btn-sm i {
font-size: 14px;
}
.btn-xs {
font-size: 12px;
padding: 1px 5px;
}
.btn-primary {
background: none repeat scroll 0 0 #7191a8;
color: white !important;
}
.btn-primary:hover, .btn-primary:focus {
background: none repeat scroll 0 0 #61859e;
text-decoration: none;
}
.btn-primary:active, .btn-primary.active {
background: none repeat scroll 0 0 #61859e;
outline: 0 none;
}
.auth-company {
color: #fff;
}
.auth-company a{
color: #fff;
text-decoration: none;
}
.soft_key{
-moz-user-select: none;
border-radius: 3px;
display: block;
height: 29px;
opacity: 0.5;
position: absolute;
width: 29px;
border:solid blue 1px;
}
#softkb_div {
background-image: url("getDocumentImg.action");
display: block;
height: 145px;
position: absolute;
width: 393px;
z-index: 999;
border:solid 1px rgb(6,166,213);
}
.soft_ckey {
-moz-user-select: none;
border-radius: 3px;
display: block;
height: 29px;
opacity: 0.5;
position: absolute;
border:solid blue 1px;
color:black;
}
a:hover{
text-decoration:none;
}
.soft_ckey:hover{
background-color:#0000ff;

}
.soft_key:hover{
background-color:#0000ff;
}
#shift {
height:37px;
width: 63px;
}
#skb_shift {
z-index: 9;
}
</style>
</head>

<body>
<div class="container">
<div class="div-signin">
<h2 class="form-signin-title" id="logo">xxxxxx</h2>
<br/>
<div class="panel panel-default">
<div class="panel-heading">
<strong>
<c:if test="${param.login_error==1}">
<font color="red"> <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" />.</font>
</c:if>
<c:if test="${param.login_error==2}">
<font color="red"><c:out value="验证码错误!" /></font>
</c:if>
</strong>
</div>
<div class="panel-body">
<form class="form-signin" id="loginForm" role="form" action="j_spring_security_check" method="POST">
<div class="form-group">
<input type="text" class="form-control" placeholder="账号" name="j_username" required autofocus maxlength="20" />
</div>
<div id="passwdinput" class="form-group" style="display:block;">
<input id="securityPasswd" class="form-control" type="password" maxlength="20" name="j_password"
placeholder="密码" readonly>
</div>
<input id="SoftKeyBoardPassword" name="SoftKeyBoardPassword" type="hidden" value=""/>
<input id="isUseSKB" name="isUseSKB" type="hidden" value="1" />
<div id="softkbinput" class="form-group">
<!-- <input id="passwdDisplay" class="form-control" type="text" readonly="" required="required" maxlength="10" placeholder="密码">
<input id="passwdInput" type="hidden" name="skb_password" maxlength="20" value="">   -->
<div id="softkb_div" style="display: none;" >
<a id="n00" class="soft_key" style="top: 9px; left: 10px;" sftid="s00"> </a>
<a id="n01" class="soft_key" style="top: 9px; left: 43px;" sftid="s01"> </a>
<a id="n02" class="soft_key" style="top: 9px; left: 76px;" sftid="s02"> </a>
<a id="n03" class="soft_key" style="top: 9px; left: 109px;" sftid="s03"> </a>
<a id="n04" class="soft_key" style="top: 9px; left: 142px;" sftid="s04"> </a>
<a id="n05" class="soft_key" style="top: 9px; left: 175px;" sftid="s05"> </a>
<a id="n06" class="soft_key" style="top: 9px; left: 208px;" sftid="s06"> </a>
<a id="n07" class="soft_key" style="top: 9px; left: 241px;" sftid="s07"> </a>
<a id="n08" class="soft_key" style="top: 9px; left: 274px;" sftid="s08"> </a>
<a id="n09" class="soft_key" style="top: 9px; left: 307px;" sftid="s09"> </a>
<a id="a00" class="soft_key" style="top: 42px; left: 10px;" sftid="A00"> </a>
<a id="a01" class="soft_key" style="top: 42px; left: 43px;" sftid="A01"> </a>
<a id="a02" class="soft_key" style="top: 42px; left: 76px;" sftid="A02"> </a>
<a id="a03" class="soft_key" style="top: 42px; left: 109px;" sftid="A03"> </a>
<a id="a04" class="soft_key" style="top: 42px; left: 142px;" sftid="A04"> </a>
<a id="a05" class="soft_key" style="top: 42px; left: 175px;" sftid="A05"> </a>
<a id="a06" class="soft_key" style="top: 42px; left: 208px;" sftid="A06"> </a>
<a id="a07" class="soft_key" style="top: 42px; left: 241px;" sftid="A07"> </a>
<a id="a08" class="soft_key" style="top: 42px; left: 274px;" sftid="A08"> </a>
<a id="a09" class="soft_key" style="top: 42px; left: 307px;" sftid="A09"> </a>
<a id="a10" class="soft_key" style="top: 75px; left: 29px;" sftid="A10"> </a>
<a id="a11" class="soft_key" style="top: 75px; left: 62px;" sftid="A11"> </a>
<a id="a12" class="soft_key" style="top: 75px; left: 95px;" sftid="A12"> </a>
<a id="a13" class="soft_key" style="top: 75px; left: 128px;" sftid="A13"> </a>
<a id="a14" class="soft_key" style="top: 75px; left: 161px;" sftid="A14"> </a>
<a id="a15" class="soft_key" style="top: 75px; left: 194px;" sftid="A15"> </a>
<a id="a16" class="soft_key" style="top: 75px; left: 227px;" sftid="A16"> </a>
<a id="a17" class="soft_key" style="top: 75px; left: 260px;" sftid="A17"> </a>
<a id="a18" class="soft_key" style="top: 75px; left: 293px;" sftid="A18"> </a>
<a id="a19" class="soft_key" style="top: 108px; left: 76px;" sftid="A19"> </a>
<a id="a20" class="soft_key" style="top: 108px; left: 109px;" sftid="A20"> </a>
<a id="a21" class="soft_key" style="top: 108px; left: 142px;" sftid="A21"> </a>
<a id="a22" class="soft_key" style="top: 108px; left: 175px;" sftid="A22"> </a>
<a id="a23" class="soft_key" style="top: 108px; left: 208px;" sftid="A23"> </a>
<a id="a24" class="soft_key" style="top: 108px; left: 241px;" sftid="A24"> </a>
<a id="a25" class="soft_key" style="top: 108px; left: 274px;" sftid="A25"> </a>
<a id="skb_close" class="soft_ckey" style="top: 9px; left: 340px; width: 43px;">关闭</a>
<a id="skb_backspace" class="soft_ckey" style="top: 42px; left: 340px; width: 43px;">退格 </a>
<a id="skb_shift" class="soft_ckey" style="top: 108px; left: 10px; width: 63px;">转换</a>
<a id="skb_reset" class="soft_ckey" style="top: 108px; left: 307px; width: 63px;">清空 </a>
</div>

</div>

<div class="form-group input-group">
<input class="form-control" name="j_code" id="verifyCode" maxlength="4" type="text" placeholder="验证码"/>
<span class="input-group-addon" style="padding: 0;">
<img src="Captcha.jpg" style="width: 100px; max-height: 30px;" onclick="this.src='Captcha.jpg?r='+Math.random();" >
</span>
</div>
<hr>

<div class="row">
<div class="col-md-4">
<a class="btn btn-primary" role="button" id="loginBtn" href="javascript:void(0);">登录</a>
</div>
<div class="col-md-8 text-left">
<input id="isRemember" type="checkbox" checked="checked" /> 记住用户名<br/>
<small style="color: #555;">忘记密码请联系系统管理员</small>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="text text-center auth-company">xxxxxx</div>

<script type="text/javascript" src="r/plugins/jquery/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="r/bootstrap-3.3.5-dist/js/bootstrap.min.js"></script>
<script type="text/javascript" src="r/jquery/jquery.placeholder.min.js"></script>
<script type="text/javascript" src="r/plugins/jquery/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript" src="r/plugins/jquery/ui/jquery-ui-1.10.4.custom.min.js"></script>
<script type="text/javascript" src="r/plugins/jquery/jquery.cookie.js"></script>
<script type="text/javascript">
if(self!=top){
top.location="login.jsp";
}
var USERNAME = "USERNAME";
if ( $.cookie(USERNAME) ) {
$('input[name="j_username"]').val($.cookie(USERNAME));
}
var loginFormSubmit = function() {
if ( $('#isRemember').attr('checked')) {
$.cookie(USERNAME, $('input[name="j_username"]').val(), {expires: 7})
} else {
$.removeCookie(USERNAME);
}
if(1 == $('#isUseSKB').val()) {
//alert("1");
$('#securityPasswd').val($('#SoftKeyBoardPassword').val());
}
$('#loginForm')[0].submit();
};
$('#loginForm input').bind('keydown', function(e){
if ( e.which == 13 ) {
loginFormSubmit();
}
});
$('#loginBtn').click(function(){ loginFormSubmit();});

/*var urls = {
skb:'softkb_security',
sec:'j_spring_security_check'
};*/
var shift = false;
var securityLogin = false;
var initSoftkb = function() {

$('#logo').click(function(){
securityLogin = !securityLogin;
if ( securityLogin ) {
alert('切换密码输入模式为键盘.');
$('#softkbinput').hide();
//$('#passwdinput').show();
//$('#passwdDisplay').removeAttr('required');
$('#isUseSKB').val(0);
$('#securityPasswd').removeAttr('readonly');
$('#securityPasswd').val("");
$('#SoftKeyBoardPassword').val("");
//$('#loginForm').attr('action',"");
} else {
alert('切换密码输入模式为图片软键盘.');
//$('#passwdinput').hide();
$('#softkbinput').show();
$('#isUseSKB').val(1);
$('#securityPasswd').attr('readonly','false');
$('#securityPasswd').val("");
$('#SoftKeyBoardPassword').val("");
//$('#passwdDisplay').attr('required','true');
//$('#loginForm').attr('action',"LoginSoftKeyBoardServlet");
}
});

$('#securityPasswd').click(function(){
$('#softkb_div').show();
});
$('#skb_close').click(function(){
$('#softkb_div').hide();
});
$('#skb_reset').click(function(){
$('#securityPasswd').val('');//.change();
$('#SoftKeyBoardPassword').val("");
//alert($('#securityPasswd').val() + "|" + $('#SoftKeyBoardPassword').val());
});
$('#skb_backspace').click(function(){
var val = $('#securityPasswd').val();
var skbVal = $('#SoftKeyBoardPassword').val();
if(val.length > 1 ) {
$('#securityPasswd').val(val.substr(0, val.length-1));
$('#SoftKeyBoardPassword').val(skbVal.substr(0, skbVal.length-4));
} else {
$('#securityPasswd').val('');//.change();
$('#SoftKeyBoardPassword').val("");
}
//alert($('#securityPasswd').val() + "|" + $('#SoftKeyBoardPassword').val());
/*if ( val.indexOf(',') >= 0 ) {
$('#passwdInput').val(val.substr(0, val.lastIndexOf(','))).change();
}*/
});

$('.soft_key').click(function(){
var val = shift?$(this).attr('sftId'):$(this).attr('id');
var secOrVal = $('#securityPasswd').val();
var hidOrVal = $('#SoftKeyBoardPassword').val();
//$('#passwdInput').val($('#passwdInput').val() + ',' + val ).change();
if(secOrVal != null && secOrVal != "") {
$('#securityPasswd').val(secOrVal + '*');
$('#SoftKeyBoardPassword').val(hidOrVal + ',' + val);
} else {
$('#securityPasswd').val('*');
$('#SoftKeyBoardPassword').val(val);
}
//alert($('#securityPasswd').val() + "|" + $('#SoftKeyBoardPassword').val());
});

/*$('#passwdInput').change(function() {
var val = $(this).val();
$('#passwdDisplay').val('');
if ( val ) {
var arr = val.split(',');
var dispVal = '';
$.each(arr, function(_i,_n){
if ( _n.length > 0 ) {
dispVal += '*';
}
});
$('#passwdDisplay').val(dispVal);
}
});*/
$('#skb_shift').click(function(){
shift = !shift;
if ( shift ) {
$('#softkb_div').css({'backgroundPosition':'0 -145px'});//右下正 左上负
} else {
$('#softkb_div').css({'backgroundPosition':'0 0'});
}
});

$('#passwdInput').val('').change();
};

$(function(){
initSoftkb();

$('input[name="j_username"]').placeholder();
$('input[name="j_password"]').placeholder();
$('input[name="j_code"]').placeholder();
});
</script>
</body>
</html>


键盘绘制代码如下:

package com.jiuyv.yl.console.image.action;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.jiuyv.common.web.action.DefaultPageSupportAction;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class JcOffDocumentAction extends DefaultPageSupportAction  {

//  private ByteArrayInputStream inputStream;
//  public static Map NUMMAP = new HashMap();
//  public static Map CHARMAP = new HashMap();
//  public static Map<String, Character> ALPMAP = new HashMap<String, Character>();
//  public ByteArrayInputStream getInputStream() {
//      return inputStream;
//  }
//
//
//  public void setInputStream(ByteArrayInputStream inputStream) {
//      this.inputStream = inputStream;
//  }

private Map softKeyBoardMap = null;

public String getDocumentImg() throws Exception {
HttpServletResponse response = null;
ServletOutputStream out = null;
BufferedImage image = this.draw();
try {
response = ServletActionContext.getResponse();
response.setContentType("multipart/form-data");
out = response.getOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return null;
}

public BufferedImage draw(){

HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
softKeyBoardMap = new HashMap();
session.setAttribute("softKeyBoardMap", softKeyBoardMap);
/**
* BufferedImage(int width, int height, int imageType)
* 构造一个类型为预定义图像类型之一的 BufferedImage。
*/
BufferedImage bufferedImage = new BufferedImage(393, 290, BufferedImage.TYPE_3BYTE_BGR);
/**
* createGraphics() 创建一个 Graphics2D,可以将它绘制到此 BufferedImage 中。
*/
Graphics2D graphics2d = bufferedImage.createGraphics();
Graphics2D graphicsChar = bufferedImage.createGraphics();
/**
* setBackground(Color color) 设置 Graphics2D 上下文的背景色。
*/
graphics2d.setBackground(Color.WHITE);
/**
* public abstract void clearRect(int x,int y,int width,int height)
* 通过使用当前绘图表面的背景色进行填充来清除指定的矩形。此操作不使用当前绘图模式。
* 从 Java 1.1 开始,离屏图像的背景色可能与系统相关。
* 应用程序应该使用 setColor,然后使用 fillRect,以确保离屏图像被清除为指定颜色。
* 参数:
* x - 要清除矩形的 x 坐标。
* y - 要清除矩形的 y 坐标。
* width - 要清除矩形的宽度。
* height - 要清除矩形的高度。
* */
graphics2d.clearRect(0, 0, 500, 300);
/**
* public abstract void setColor(Color c)
* 将此图形上下文的当前颜色设置为指定颜色。
* 使用此图形上下文的所有后续图形操作均使用这个指定的颜色。
* 参数:
* c - 新的呈现颜色。
* */
//graphics2d.setColor(Color.RED);
Font font = new Font("Romantic", Font.PLAIN, 15);
Font charFont = new Font("Romantic", Font.PLAIN, 8);
/**
* 将此图形上下文的字体设置为指定字体。
* 使用此图形上下文的所有后续文本操作均使用此字体。忽略 null 参数
*/
graphics2d.setFont(font);
graphicsChar.setFont(charFont);
/**
* public abstract void fillRect(int x,int y,int width,int height)
* 填充指定的矩形。该矩形左边缘和右边缘分别位于 x 和 x + width - 1。
* 上边缘和下边缘分别位于 y 和 y + height - 1。
* 得到的矩形覆盖 width 像素宽乘以 height 像素高的区域。
* 使用图形上下文的当前颜色填充该矩形。
* */
int flag = 33;
//graphics2d.fillRect(0, 0, 500, 30);
graphics2d.setColor(Color.black);
graphicsChar.setColor(Color.black);
//      x = 28;

//      for(int i=0; i<10; i++) {
//          graphics2d.drawString(String.valueOf(i), x, 33);
//          x += 33;
//      }
String[] number = {"1","2","3","4","5","6","7","8","9","0"};
String[] charVal = {"!","@","#","$","%","^","&","*","(",")"};
List<String> numList = new ArrayList<String>(Arrays.asList(number));
List<String> charList = new ArrayList<String>(Arrays.asList(charVal));
Random random = new Random();
int x = 18;
int y = 33;
int upy = y + 143;
for(int i=number.length; i>0; i--) {
int position = random.nextInt(i);
String num = numList.remove(position);
String chr = charList.remove(position);
softKeyBoardMap.put("n" + String.format("%02d", Long.valueOf(10 - i)), num);//key位置 value 值
softKeyBoardMap.put("s" + String.format("%02d", Long.valueOf(10 - i)), chr);//key位置 value 值
graphics2d.drawString(num, x, y);
graphicsChar.drawString(chr, x+10, y-13);
graphics2d.drawString(chr, x, upy);
graphicsChar.drawString(num, x+10, upy-13);
x += 33;
}
int alp = 97;//a
int alpX = 18;
int alpY = 62;
for(int i=26; i>0; i--) {
int alpNum = random.nextInt(i) + alp;
char alpChar = (char)this.existAutoAdd(alpNum);
softKeyBoardMap.put("a" + String.format("%02d", Long.valueOf(26-i)), alpChar);//key位置 value 值
softKeyBoardMap.put("A" + String.format("%02d", Long.valueOf(26-i)), String.valueOf((char)(alpChar-32)));//key位置 value 值
graphics2d.drawString(String.valueOf(alpChar), alpX, alpY);
graphics2d.drawString(String.valueOf((char)(alpChar-32)), alpX, alpY + 143);
alpX += 33;
if(i==17) {
alpX = 40;
alpY = 96;
} else if(i == 8) {
alpX = 83;
alpY = 127;
}
}
//      graphics2d.setFont(font);
//      graphics2d.drawString("!", x, y);
//      graphics2d.drawString("@", x+=33, y);
//      graphics2d.drawString("#", x+=33, y);
//      graphics2d.drawString("$", x+=33, y);
//      graphics2d.drawString("%", x+=33, y);
//      graphics2d.drawString("^", x+=33, y);
//      graphics2d.drawString("&", x+=33, y);
//      graphics2d.drawString("*", x+=33, y);
//      graphics2d.drawString("(", x+=33, y);
//      graphics2d.drawString(")", x+=33, y);
/**
* public abstract void fillOval(int x,int y,int width,int height)
* 使用当前颜色填充外接指定矩形框的椭圆。
* 参数:
* x - 要填充椭圆的左上角的 x 坐标。
* y - 要填充椭圆的左上角的 y 坐标。
* width - 要填充椭圆的宽度。
* height - 要填充椭圆的高度。
* */
//      graphics2d.fillOval(20, 40, 20, 20);
//      graphics2d.setColor(Color.ORANGE);
//      graphics2d.fillOval(40, 60, 20, 20);
//      graphics2d.setColor(Color.YELLOW);
//      graphics2d.fillOval(60, 80, 20, 20);
//      graphics2d.setColor(Color.GREEN);
//      graphics2d.fillOval(80, 100, 20, 20);
//      graphics2d.setColor(Color.CYAN);
//      graphics2d.fillOval(100, 120, 20, 20);
//      graphics2d.setColor(Color.BLUE);
//      graphics2d.fillOval(120, 140, 20, 20);
//      graphics2d.setColor(Color.MAGENTA);
//      graphics2d.fillOval(140, 160, 20, 20);
//
//      graphics2d.setColor(Color.MAGENTA);
//      graphics2d.fillOval(340, 160, 20, 20);
//      graphics2d.setColor(Color.BLUE);
//      graphics2d.fillOval(360, 140, 20, 20);
//      graphics2d.setColor(Color.CYAN);
//      graphics2d.fillOval(380, 120, 20, 20);
//      graphics2d.setColor(Color.GREEN);
//      graphics2d.fillOval(400, 100, 20, 20);
//      graphics2d.setColor(Color.YELLOW);
//      graphics2d.fillOval(420, 80, 20, 20);
//      graphics2d.setColor(Color.ORANGE);
//      graphics2d.fillOval(440, 60, 20, 20);
//      graphics2d.setColor(Color.RED);
//      graphics2d.fillOval(460, 40, 20, 20);

/**
* public abstract void drawLine(int x1,int y1,int x2,int y2)
* 在此图形上下文的坐标系中,使用当前颜色在点 (x1, y1) 和 (x2, y2) 之间画一条线。
* 参数:
* x1 - 第一个点的 x 坐标。
* y1 - 第一个点的 y 坐标。
* x2 - 第二个点的 x 坐标。
* y2 - 第二个点的 y 坐标。
* */
//      graphics2d.setColor(Color.RED);
//      for(int m=0;m<5;m++){
//          graphics2d.drawLine(0, 190+m*20, 500, 190+m*20);
//      }
//      graphics2d.setColor(Color.RED);
//      for(int n=0;n<25;n++){
//          graphics2d.drawLine(0+n*20, 190, 0+n*20, 270);
//      }

/**
* public abstract void dispose()释放此图形的上下文以及它使用的所有系统资源。
* 调用 dispose 之后,就不能再使用 Graphics 对象。
* Java 程序运行时,可以在一个短时间帧内创建大量的 Graphics 对象。
* 尽管垃圾回收器的终止进程也能够进行相同的系统资源释放,
* 但是最好通过调用此方法来手工释放相关资源,而不是依靠终止进程,
* 因为终止进程需要很长时间才能结束运行。
* 作为组件 paint 和 update 方法的参数提供的 Graphics 对象,
* 在这些方法返回时将被系统自动释放。
* 为了提高效率,仅当所使用的 Graphics 对象
* 是由组件或另一个 Graphics 对象直接创建时,
* 程序员才应在使用完此 Graphics 后调用 dispose。
* */
graphics2d.dispose();
/**
* public void flush()
* 刷新此 Image 对象正在使用的所有可重构的资源。
* 这包括为呈现到屏幕而缓存的所有像素数据,
* 以及用来存储图像数据或像素的所有系统资源(如果可以重新创建它们)。
* 图像被重置为与初始创建时类似的状态,因此如果再次呈现图像,
* 则必须重新创建图像数据或再次从源中获取这些数据。
* 此方法如何影响特定类型的 Image 对象的示例如下:
* BufferedImage 对象不改动存储其像素的主要 Raster,但是刷新有关像素的缓存信息,
* 比如上传到用于加速位图的显示硬件的副本。
* Image 对象由带有 width 和 height 参数的 Component 方法创建,
* 该方法不改动像素的主要缓冲区,但是释放所有缓存的信息,
* 这与对 BufferedImage 对象的操作非常类似。
* VolatileImage 对象释放其所有像素资源,
* 这些资源包括通常存储在缺乏资源的显示硬件上的主要副本。
* 这些对象稍后可以使用其 validate 方法还原。
* 卸载从文件、URL 中加载或由 ImageProducer 生成的 Toolkit
* 和 Component 类创建的 Image 对象,并释放所有本地资源。
* 稍后可在呈现这些对象时根据需要从初始源重新加载它们,就像第一次创建它们时那样。
* */
bufferedImage.flush();
return bufferedImage;
}

public int existAutoAdd(int value) {
if(!softKeyBoardMap.containsValue((char)(value))) {
return value;
} else {
return existAutoAdd(++value);
}
}

public static String getDecoderPassword(String password,HttpServletRequest request) {
// String password = request.getParameter("j_password");
String regex = ",";
String changePassword = "";
if (null != password && password.length() > 0) {
// JcOffDocumentAction
String[] pw = password.split(regex);
Map skbMap = (Map)request.getSession().getAttribute("softKeyBoardMap");
if (null != pw || pw.length > 0) {

for (int i = 0; i < pw.length; i++) {
String pwStr = pw[i];
changePassword += skbMap.get(pwStr);
}
}
}
return changePassword;
}
}


struts2 配置如下

<!-- 软键盘 -->
<action name="getDocumentImg" class="JcOffDocumentAction" method="getDocumentImg">
</action>


这里软键盘主做好了。在页面上可以看到键盘样式。

在点击登录的时候,前台密码输入框j_password装的不是用户输入的数据,而是位置代码。传到后台后需要解析成原密码。而解析上面代码放到了SESSION中。

以下代码是登录处理代码。

package com.jiuyv.yl.console.security.util;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.jiuyv.yl.console.image.action.JcOffDocumentAction;

public class ParamFilter implements Filter {

@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub

}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
Map<String, String[]> m = new HashMap<String, String[]>(
request.getParameterMap());
String[] pwdArray = m.get("j_password");
String[] isUseSKB = m.get("isUseSKB");
if (null !=isUseSKB && isUseSKB.length>0 && "1".equals(isUseSKB[0])) {
ParameterRequestWrapper wrapRequest = new ParameterRequestWrapper(
(HttpServletRequest) request, m);
if (null != pwdArray) {
String pwdStr = JcOffDocumentAction.getDecoderPassword(pwdArray[0], wrapRequest);
String[] pwd = { pwdStr };
m.put("j_password", pwd);
}

chain.doFilter(wrapRequest, response);
} else {
chain.doFilter(request, response);
}
}

@Override
public void destroy() {
// TODO Auto-generated method stub

}
}


这块代码是一个Filter,拦截登录请求,因为我后台登录校验用的是springScurity。所以不能在那块处理。而用Filter截下来的请求,request参数是不能修改的(代码被LOCK,一改就报错)。所以我这里自己建一个request ParameterRequestWrapper 。

代码如下:

package com.jiuyv.yl.console.security.util;

import java.util.Enumeration;
import java.util.Map;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class ParameterRequestWrapper extends HttpServletRequestWrapper {

private Map<String, String[]> params;

public ParameterRequestWrapper(HttpServletRequest request,
Map<String, String[]> newParams) {
super(request);

this.params = newParams;

// RequestDispatcher.forward parameter
renewParameterMap(request);
}

@Override
public String getParameter(String name) {
String result = "";

Object v = params.get(name);
if (v == null) {
result = null;
} else if (v instanceof String[]) {
String[] strArr = (String[]) v;
if (strArr.length > 0) {
result =  strArr[0];
} else {
result = null;
}
} else if (v instanceof String) {
result = (String) v;
} else {
result =  v.toString();
}

return result;
}

@Override
public Map<String, String[]> getParameterMap() {
return params;
}

@Override
public Enumeration<String> getParameterNames() {
return new Vector<String>(params.keySet()).elements();
}

@Override
public String[] getParameterValues(String name) {
String[] result = null;

Object v = params.get(name);
if (v == null) {
result =  null;
} else if (v instanceof String[]) {
result =  (String[]) v;
} else if (v instanceof String) {
result =  new String[] { (String) v };
} else {
result =  new String[] { v.toString() };
}

return result;
}

private void renewParameterMap(HttpServletRequest req) {

String queryString = req.getQueryString();

if (queryString != null && queryString.trim().length() > 0) {
String[] params = queryString.split("&");

for (int i = 0; i < params.length; i++) {
int splitIndex = params[i].indexOf("=");
if (splitIndex == -1) {
continue;
}

String key = params[i].substring(0, splitIndex);

if (!this.params.containsKey(key)) {
if (splitIndex < params[i].length()) {
String value = params[i].substring(splitIndex + 1);
this.params.put(key, new String[] { value });
}
}
}
}
}

}


web.xml 配置如下:

<filter>
<filter-name>ParameterFilter</filter-name>
<filter-class>com.jiuyv.yl.console.security.util.ParamFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ParameterFilter</filter-name>
<url-pattern>/j_spring_security_check</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>


这个Filter把request把密码转换成原码后,新建了自己的request,发到后端,后端流程就完全不需要改变了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息