Artikel ini memperkenalkan Springboot yang dikombinasikan dengan Springsecurity untuk mengimplementasikan fungsi kode verifikasi grafis, dan membagikannya kepada Anda, sebagai berikut:
Menghasilkan kode verifikasi grafis
Proses menghasilkan kode verifikasi grafis relatif sederhana dan tidak ada hubungannya dengan Springsecurity. Jadi saya baru saja memposting kode
Menghasilkan gambar berdasarkan angka acak
/** * Hasilkan kode verifikasi grafis * @param permintaan * @return */private imagecode menghasilkan (permintaan servletwebRequest) {int width = 64; tinggi int = 32; BufferedImage Image = BufferedImage baru (lebar, tinggi, bufferedImage.type_int_rgb); Grafik g = image.getGraphics (); Acak acak = acak baru (); G.SetColor (GetRandColor (200, 250)); g.fillrect (0, 0, lebar, tinggi); g.setfont (font baru ("Times New Roman", font.italic, 20)); 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); } String srand = ""; untuk (int i = 0; i <4; i ++) {string rand = string.valueof (random.nextInt (10)); srand += rand; g.setColor (warna baru (20 + acak.nextint (110), 20 + random.nextint (110), 20 + acak.nextint (110))); G.DrawString (Rand, 13 * i + 6, 16); } g.dispose (); return new ImageCode (Image, Srand, 60);}/** * Menghasilkan garis latar belakang acak * * @param fc * @param bc * @return */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);}Simpan nomor acak di sesi && tulis gambar yang dihasilkan ke respons antarmuka
@RestControllerPublic Class ValidateCodecontroller {public static final string session_key = "session_key_image_code"; sesi private SesiStrategy strategi = httpsessiSessionstrategy baru (); @GetMapping ("/code/image") public void createCode (permintaan httpservletRequest, respons httpservletResponse) melempar ioException {imagecode imagecode = menghasilkan (servletwebRequest baru (permintaan)); sessionstrategy.setAttribute (servletWebRequest baru (permintaan), session_key, imagecode); ImageIO.Write (ImageCode.GetImage (), "JPEG", Response.GetOutputStream ()); }}Tambahkan kode verifikasi grafis ke proses otentikasi
Dalam penjelasan terperinci tentang proses otentikasi SPRINGSECURITY, kami menyebutkan bahwa SPRINGSECURURITY diverifikasi melalui rantai filter. Kami ingin memverifikasi kode verifikasi grafis, sehingga kami dapat memverifikasi sebelum proses otentikasi, yaitu, sebelum UsernamePasswordAuthenticationFilter .
Filter kode verifikasi grafis khusus
@ComponentPublic Class ValidateCodefilter meluas sekaliPLEQUESTFILTER {private SESSIONSTREGTET SESSIONSTREGTEGTEG = HTTPSESSESSESSIONSTRATEGT () baru; AuthenticationFailureHandler Private AuthenticationFailureHandler; @Override protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException { if(StringUtils.equals("/user/login", httpServletRequest.getRequestURI()) && Stringutils.equalsignorecase (httpservletrequest.getMethod (), "post")) {coba {// 1. Verifikasi verifikasi kode verifikasi (servletwebRequest baru (httpservletrequest)); } catch (validateCodeException e) {// 2. Jika verifikasi gagal, call springsecurity's verifikasi prosesor authenticationFailureHandler.onauthenticationFailure (httpservletRequest, httpservletResponse, e); kembali ; }} // 3. Jika verifikasi dilewati, rilis filterchain.dofilter (httpservletRequest, httpservletResponse); }}Proses verifikasi kode verifikasi di sini relatif sederhana. Ini terutama menentukan apakah parameter yang diteruskan konsisten dengan yang tersimpan di sesi, dan apakah kode verifikasi dalam sesi telah kedaluwarsa.
Setelah memiliki filter kode verifikasi kami sendiri, kami juga perlu mengonfigurasinya sebelum UserNamePasswordAuthenticationFilter:
@OverridEprotected void configure (httpsecurity http) melempar Exception {ValidAtecodefilter validateCodefilter = new ValidateCodefilter (); validateCodefilter.setAuthenticationFailureHandler (myAuthenticationFailureHandler); // Konfigurasikan filter khusus kami ke USERNAMEPASSWORDAuthenticationFilter sebelum http.addfilterbefore (validateCodefilter, usernepasswordAuthenticationFilter.class) .FormLogin () // Definisikan halaman login yang perlu di -login.Unduh kode
Kurin Kecelakaan Musim Semi
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.