تقدم هذه المقالة Springboot مع SpringSecurity لتنفيذ وظيفة رمز التحقق الرسومية ، ومشاركتها معك ، على النحو التالي:
إنشاء رمز التحقق من الرسوم
عملية توليد رموز التحقق من الرسوم البيانية بسيطة نسبيًا وليس لها أي علاقة مع SpringSecurity. لذلك أنا فقط نشرت الرمز
إنشاء الصور على أساس أرقام عشوائية
/** * إنشاء رمز التحقق من الرسوم * @param request * return */private ImageCode إنشاء (ServleTWebRequest request) {int width = 64 ؛ ارتفاع int = 32 ؛ صورة bufferedImage = جديد bufferedImage (العرض ، الارتفاع ، bufferedImage.type_int_rgb) ؛ الرسومات g = image.getGraphics () ؛ عشوائي عشوائي = جديد عشوائي () ؛ G.SetColor (GetRandColor (200 ، 250)) ؛ G.FillRect (0 ، 0 ، العرض ، الارتفاع) ؛ G.SetFont (خط جديد ("Times New Roman" ، font.italic ، 20)) ؛ G.SetColor (GetRandColor (160 ، 200)) ؛ لـ (int i = 0 ؛ i <155 ؛ i ++) {int x = random.nextint (width) ؛ int y = random.nextint (الارتفاع) ؛ int xl = random.nextint (12) ؛ int yl = random.nextint (12) ؛ G.Drawline (x ، y ، x + xl ، y + yl) ؛ } String srand = "" ؛ لـ (int i = 0 ؛ i <4 ؛ i ++) {string rand = string.valueof (random.nextint (10)) ؛ Srand += Rand ؛ G.SetColor (لون جديد (20 + Random.NextInt (110) ، 20 + Random.NextInt (110) ، 20 + Random.Nextint (110))) ؛ G.DrawString (Rand ، 13 * i + 6 ، 16) ؛ } g.dispose () ؛ إرجاع رمز ImageCode جديد (صورة ، srand ، 60) ؛}/** * إنشاء خطوط خلفية عشوائية * * param fc * param bc * regurn */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) ؛}احفظ الأرقام العشوائية في الجلسة && اكتب الصورة التي تم إنشاؤها إلى استجابة الواجهة
RestControllerPublic Class ValidateCodeController {public static Final String Session_Key = "Session_key_Image_Code" ؛ SessionStrategygygygytytrategy = جديد httpsessionsessiontrategy () ؛ GetMapping ("/code/image") public void createCode (طلب httpservletrequest ، httpservletresponse) يلقي ioException {imageCode ImageCode = إنشاء (servletwebrequest (طلب جديد)) ؛ SessionStrategy.setAttribute (ServleTwebRequest جديد (طلب) ، Session_key ، ImageCode) ؛ imageio.write (ImageCode.getImage () ، "jpeg" ، desponse.getOutputStream ()) ؛ }}أضف رمز التحقق الرسومي إلى عملية المصادقة
في التفسير التفصيلي لعملية مصادقة SpringSecurity ، ذكرنا أن SpringSecurity يتم التحقق منه من خلال سلسلة المرشح. نريد التحقق من رمز التحقق من الرسوم ، حتى نتمكن من التحقق منه قبل عملية المصادقة ، أي قبل UsernamePasswordAuthenticationFilter .
مرشح رمز التحقق من الرسوم البيانية المخصصة
@componentpublic class validateCodeFilter يمتد مرة واحدة {private sessionstrategy sessionstrategy = new httpsessionStionTrategy () ؛ المصادقة الخاصة FailureHandler AuthenticationFailureHandler ؛ override محمية void dofilterinternal (httpservletrequest httpservletrequest ، httpservletrespons StringUtils.equalsInsignoreCase (httpservletrequest.getmethod () ، "post")) {try {// 1. } catch (validateCodeException e) {// 2. إذا فشل التحقق ، استدعاء FANTIFICE FAILITY AUTTRIACTICATIONFAILUREHAHNAHERDERLERENTERATICATICATIONTICATIONTICATIONS (httpservletrequest ، httpservletresponse ، e) ؛ يعود ؛ }} // 3. إذا تم تمرير التحقق ، فإن إطلاق Filterchain.dofilter (httpservletrequest ، httpservletresponse) ؛ }}عملية التحقق من رمز التحقق هنا بسيطة نسبيا. إنه يحدد بشكل أساسي ما إذا كانت المعلمات التي تم تمريرها تتفق مع المعلمات المخزنة في الجلسة ، وما إذا كان رمز التحقق في الجلسة قد انتهى.
بعد الحصول على مرشح رمز التحقق الخاص بنا ، نحتاج أيضًا إلى تكوينه قبل usernamepasswordauthenticationfilter:
OverRideProtected void تكوين (httpsecurity http) يلقي استثناء {validateCodeFilter validateCodeFilter = new ValidateCodeFilter () ؛ ValyAteCodeFilter.SetAuthenticationFailureHandler (myAuthenticationFailureHandler) ؛ // قم بتكوين المرشحات المخصصة الخاصة بنا إلى usernamepasswordauthenticationfilter قبل http.addfilterbefore foreتنزيل رمز
الربيع الأمن
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.