この記事では、参照用のJavawebログイン検証コードの特定のコードを共有しています。特定のコンテンツは次のとおりです
使用:
コントローラー:検証コードを生成します
@RequestMapping( "/user/check.jpg")public void createcode(httpservletrequest request、httpservletResponse応答)ioException {// response.setheader( "expires"、 "-1"); Response.setheader( "Cache-Control"、 "no-cache"); Response.setheader( "Pragma"、 "-1"); captchautil util = captchautil.instance(); //検証コードをセッションに入力して、文字列code = util.getString()を確認します。 request.getSession()。setAttribute( "code"、code); // WebページImageio.write(util.getimage()、 "jpg"、response.getOutputStream())を出力します。 } JSP:検証コードを表示します
<img id = "img" src = "<%= basepath%> user/check.jpg" onclick = "refresh()">
function refresh(){var url = $( "#basepath")。val() + "user/check.jpg?number =" + math.random(); $( "#img")。attr( "src"、url); }確認する:
セッションのコードが前景に送られたコードと一致しているかどうかを取得します
/ ** *検証コード検証 * * @paramセッション * @param code */ private void checkcode(httpsession session、string code){string codessession =(string)session.getattribute( "code"); if(stringutils.isempty(codessession)){log.Error( "検証コード情報は生成されなかった");新しいIllegalStateException( "err-01000"); } if(stringutils.isempty(code)){log.Error( "検証コード情報は入力されていません");新しいBusinessException( "err-06018"); } if(codessession.equalsignorecase(code)){//検証コードが渡されます} else {log.error( "検証コードエラー");新しいBusinessException( "err-06019"); }}ツール:
java.awt.colorをインポートします。 java.awt.fontをインポートします。 java.awt.graphicsをインポートします。 java.awt.image.bufferedimageをインポートします。 java.util.randomをインポートします。 / ** *検証コード生成ツール * @author hxl * */ public class captchautil {private bufferedimage image; // image private string str; //検証コードprivate static char code [] = = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz123456789" .tochararray(); public static final string session_code_name = "code"; private captchautil(){init(); // initializationプロパティ}/** get randomnumutil instance*/ public static captchautil instance(){return new captchautil(); } /**検証コード写真を取得* / public bufferedimage getImage(){return this.image; } /**画像の検証コードを取得* / public String getString(){return this.str; } private void init(){// image int width = 85、height = 20; bufferedimage image = new BufferedImage(幅、高さ、bufferedimage.type_int_rgb); //グラフィカルコンテキストグラフィックスG = image.getGraphics()を取得します。 //ランダム= new Random()を生成します。 //背景色G.SetColor(GetRandColor(200、250)); G.FillRect(0、0、幅、高さ); // font g.setfont(new Font( "Times New Roman"、font.plain、18))を設定します。 // 155の干渉線をランダムに生成し、画像内の認証コードを他のプログラムで検出する可能性が低い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); } //ランダムに生成された認証コード(4桁の番号)文字列srand = ""; for(int i = 0; i <4; i ++){string rand = string.valueof(code [random.nextint(code.length)]); srand += rand; //画像g.setcolor(new Color(20 + random.nextint(110)、20 + random.nextint(110)))に認証コードを表示します。 //コール関数の色は同じです。たぶん、種子が近すぎるため、G.DrawString(RAND、13 * I + 6、16)のみを直接生成できます。 } //割り当て検証コードthis.str = srand; //画像は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( "検証コード画像が生成されたときにエラーが発生しました:" + e.toString()); //} // this.image = input this.image = image;/* assile emage*/}/**特定の範囲のランダム色を取得*/ 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);新しい色(r、g、b)を返します。 }}最後に、説明しましょう。ログインインターセプターは、検証コードを生成するためのパスをリリースする必要があります。ログインインターセプターは、検証コードを生成するためのパスをリリースする必要があります! !ログインインターセプターは、検証コードを生成するためのパスをリリースする必要があります! ! !重要なことを3回言う~~
最後のディスプレイ:
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。