Artikel ini telah membagikan kode spesifik kode verifikasi login Javaweb untuk referensi Anda. Konten spesifiknya adalah sebagai berikut
menggunakan:
Pengontrol: Hasilkan kode verifikasi
@RequestMapping ("/user/check.jpg") public void createCode (permintaan httpservletRequest, httpservletResponse response) melempar ioException {// beri tahu browser untuk tidak cache response.setheader ("Expires", "-1"); response.setheader ("cache-control", "no-cache"); response.setheader ("pragma", "-1"); Captchautil util = captchautil.instance (); // Masukkan kode verifikasi ke dalam sesi untuk memverifikasi kode string = util.getString (); request.getSession (). setAttribute ("code", code); // output halaman web gambaroo.write (util.getImage (), "jpg", response.getoutputStream ()); } JSP: Tampilkan kode verifikasi
<img id = "img" src = "<%= Basepath%> user/check.jpg" ontClick = "refresh ()">
function refresh () {var url = $ ("#BasePath"). Val () + "user/check.jpg? Number =" + math.random (); $ ("#img"). attr ("src", url); } memeriksa:
Dapatkan apakah kode dalam sesi konsisten dengan kode yang dikirim kembali di latar depan
/ ** * Verifikasi kode verifikasi * * @param sesi * @param kode */ private void checkCode (sesi httpsession, kode string) {string codesession = (string) session.getAttribute ("code"); if (stringutils.isempty (codesession)) {log.error ("Tidak ada informasi kode verifikasi yang dihasilkan"); Lempar IllegalStateException baru ("ERR-01000"); } if (stringutils.isempty (kode)) {log.error ("Tidak ada informasi kode verifikasi yang diisi"); Lempar BusinessException baru ("ERR-06018"); } if (codesession.equalSignorecase (kode)) {// kode verifikasi dilewati} else {log.error ("kesalahan kode verifikasi"); Lempar BusinessException baru ("ERR-06019"); }} Peralatan:
impor java.awt.color; impor java.awt.font; impor java.awt.graphics; impor java.awt.image.bufferedimage; impor java.util.random; / ** * Alat pembuatan kode verifikasi * @author hxl * */ kelas publik captchautil {private buferedImage Image; // Image Private String stred; // kode verifikasi Kode char privat [] = "abcdefghijklmnopqrstuVwxyzabcdefgHiJKlMnoRrSrStuvwxyzabcdefgHiJKlMnoR -tuVwxyzabcdefgHiJKlMnoR -tuVwxyzabcdefgHiJKlMnArDSUVWXYZ12 Public Static Final String session_code_name = "code"; private captchautil () {init (); // properti inisialisasi}/** Dapatkan instance randomNumutil*/ instance captchautil statis publik () {return new captchautil (); } /** Dapatkan gambar kode verifikasi* / public BufferedImage getImage () {return this.image; } /** Dapatkan kode verifikasi untuk gambar* / string publik getString () {return this.str; } private void init () {// Buat gambar int gambar = 85, tinggi = 20; BufferedImage Image = BufferedImage baru (lebar, tinggi, bufferedImage.type_int_rgb); // Dapatkan grafik konteks grafis g = image.getGraphics (); // menghasilkan acak acak = acak baru (); // atur warna latar belakang g.setColor (getRandColor (200, 250)); g.fillrect (0, 0, lebar, tinggi); // atur font g.setfont (font baru ("Times New Roman", font.plain, 18)); // menghasilkan 155 baris interferensi secara acak, membuat kode otentikasi pada gambar yang lebih kecil kemungkinannya terdeteksi oleh program lain g.setColor (getRandColor (160, 200)); untuk (int i = 0; i <155; i ++) {int x = random.nextInt (lebar); int y = random.nextInt (tinggi); int xl = random.nextInt (12); int yl = random.nextInt (12); G.Drawline (x, y, x + xl, y + yl); } // Ambil kode otentikasi yang dihasilkan secara acak (nomor 4 digit) string srand = ""; untuk (int i = 0; i <4; i ++) {string rand = string.valueof (kode [random.nextInt (code.length)]); srand += rand; // Tampilkan kode otentikasi dalam gambar G.SetColor (warna baru (20 + acak.nextInt (110), 20 + acak.nextint (110))); // Warna fungsi panggilannya sama, mungkin karena benihnya terlalu dekat, sehingga Anda hanya dapat secara langsung menghasilkan G.Drawstring (Rand, 13 * i + 6, 16); } // kode verifikasi penugasan this.str = srand; // gambar mulai berlaku g.dispose (); // bytearrayInputStream input = null; // byteArrayOutputStream output = new bytearrayoutputStream (); // coba {// ImageOutputStream ImageOut = Imageo.CreateImageOutputStream (output); // Imageo.write (Image, "JPEG", ImageOut); // ImageOut.close (); // input = bytearrayInputStream baru (output.tobyteArray ()); //} catch (Exception e) {// System.out.println ("Terjadi kesalahan ketika gambar kode verifikasi dihasilkan:" + e.tostring ()); //} // this.image = input this.image = image;/* Tetapkan gambar*/}/** Dapatkan warna acak untuk rentang yang diberikan*/ warna pribadi getRandColor (int fc, int bc) {acak acak = acak baru (); 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); mengembalikan warna baru (r, g, b); }} Akhirnya, mari kita jelaskan: Interceptor login harus melepaskan jalur untuk menghasilkan kode verifikasi! Login Interceptor harus melepaskan jalur untuk menghasilkan kode verifikasi! Lai Login Interceptor harus melepaskan jalur untuk menghasilkan kode verifikasi! Lai Lai Katakan hal -hal penting tiga kali ~~
Tampilan terakhir:
Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.