この記事では、スプリングブートとスプリングスセキュリティを組み合わせてグラフィカル検証コード機能を実装し、次のように共有します。
グラフィック検証コードを生成します
グラフィック検証コードを生成するプロセスは比較的単純であり、スプリングセキュリティとは何の関係もありません。だから私はちょうどコードを投稿しました
乱数に基づいて写真を生成します
/** *グラフィック検証コードを生成 * @param request * @return */private imagecode generate(servletwebrequest request){int width = 64; int height = 32; bufferedimage image = new BufferedImage(幅、高さ、bufferedimage.type_int_rgb);グラフィックg = image.getGraphics(); RANDOM RANDOM = new Random(); G.SetColor(GetRandColor(200、250)); G.FillRect(0、0、幅、高さ); g.setFont(new Font( "Times New Roman"、font.italic、20)); 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); }文字列srand = ""; for(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.ドローストリング(RAND、13 * I + 6、16); } g.dispose();新しいImageCode(Image、Srand、60);}/** *ランダムな背景ストライプを生成 * * @param fc * @param bc * @return */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"; Private SessionsTrategy SessionsTrategy = new httpsessionsessionstrategy(); @getMapping( "/code/image")public void createCode(httpservletrequest request、httpservletResponse応答)throws ioexception {imagecode imagecode = generate(new servletwebrequest(request)); sessionstrategy.setattribute(new servletwebrequest(request)、session_key、imagecode); Imageio.write(imageCode.getImage()、 "jpeg"、respons.getOutputStream()); }}認証プロセスにグラフィック検証コードを追加します
Springsecurity認証プロセスの詳細な説明では、Springsecurityがフィルターチェーンを介して検証されていると述べました。グラフィック検証コードを確認したいので、認証プロセスの前、つまりUsernamePasswordAuthenticationFilterの前に確認できます。
カスタムグラフィック検証コードフィルター
@componentPublic class validAteCodeFilterはwoneperRequestFilter {private sessionstrategy sessionstrategy = new httpsessionsessionsionsionstrategy(); Private AuthenticationFailureHandler AuthenticationFailureHandler; @Override Protected void dofilterinternal(httpservletrequest、httpservletrequest、httpservletresponse httpservletresponse、filterchain filterchain)srevletexception、ioexception、ioexception {if(stringutils.equals( "/user/user/login"、httpservletreletreletreletreletreletreletreletrelequest.getRequestRequest.getRequeTuri() stringutils.equalsignorecase(httpservletrequest.getmethod()、 "post")){try {//1。検証コードの検証(new servletwebrequest(httpservletrequest)); } catch(validAteCodeException e){//2。検証が失敗した場合、Springsecurityの検証障害プロセッサAuthenticationFailureHandler.OnauthenticationFailure(httpservletResponse、e)を呼び出します。戻る ; }} // 3。検証が渡された場合、filterchain.dofilter(httpservletrequest、httpservletresponse)をリリースします。 }}ここでの検証コード検証プロセスは比較的簡単です。主に、合格したパラメーターがセッション内の保存されたパラメーターと一致しているかどうか、およびセッションの検証コードが有効になっているかどうかを決定します。
独自の検証コードフィルターを使用した後、usernamepasswordauthenticationfilterの前に構成する必要があります。
@OverRideProtected void configure(httpsecurity http)throws exception {validatecodefilter validatecodefilter = new validatecodefilter(); validatecodefilter.setAuthenticationFailureHandler(myAuthenticationFailureHandler); // http.addfilterbefore(validatecodefilter、usernamepasswordauthenticationfilter.class.class).formlogin()//ユーザーがログインする必要があるログインページを定義する前に、usernamepasswordauthenticationfilterにカスタムフィルターを構成します。コードダウンロード
春のセキュリティ
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。