Spring安全:暴力攻撃防止


Spring Securityはたくさんのことをしてくれます。
口座は閉鎖されています。パスワードは塩です。しかし、蛮力遮断剤は?
それはあなたが自分でしなければならないことです。
幸い、Springは非常にフレキシブルなフレームなので、配置に大きな問題はありません。
この操作をGrairsアプリケーションに対してどのように実行するかのマニュアルを見せます。
まず、config.groovyでspring SecurityEventListenerを有効にしてください。
grails.plugins.springsecurity.useSecurityEventListener = true
そしてモニターを実現して/src/breuteforceにクラスを作成します。
/**
Registers all failed attempts to login. Main purpose to count attempts for particular account ant block user

*/
class AuthenticationFailureListener implements ApplicationListener {

    LoginAttemptCacheService loginAttemptCacheService

    @Override
    void onApplicationEvent(AuthenticationFailureBadCredentialsEvent e) {
        loginAttemptCacheService.failLogin(e.authentication.name)
    }
}
続いて、私達は必ず成功的に登録するためのトランシーバーを作成します。同じパッケージの中にあります。
/**
 Listener for successfull logins. Used for reseting number on unsuccessfull logins for specific account
*/
class AuthenticationSuccessEventListener implements ApplicationListener{

    LoginAttemptCacheService loginAttemptCacheService

    @Override
    void onApplicationEvent(AuthenticationSuccessEvent e) {
        loginAttemptCacheService.loginSuccess(e.authentication.name)
    }
}
私たちはそれらをgrails-apフォルダに置いていませんので、これらをspring beanとして改めて命名したいです。grails-ap/conf/spring/resource.groovyに次の行を追加します。
beans = {
    authenticationFailureListener(AuthenticationFailureListener) {
        loginAttemptCacheService = ref('loginAttemptCacheService')
    }

    authenticationSuccessEventListener(AuthenticationSuccessEventListener) {
        loginAttemptCacheService = ref('loginAttemptCacheService')
    }
}
LoginaAttemptCachece Service loginAttemptCachServiceの使い方に気づくかもしれません。それを実現します。これは典型的なgrailsサービスです。
package com.picsel.officeanywhere

import com.google.common.cache.CacheBuilder
import com.google.common.cache.CacheLoader
import com.google.common.cache.LoadingCache

import java.util.concurrent.TimeUnit
import org.apache.commons.lang.math.NumberUtils
import javax.annotation.PostConstruct

class LoginAttemptCacheService {

    private LoadingCache
               attempts;
    private int allowedNumberOfAttempts
    def grailsApplication

    @PostConstruct
    void init() {
        allowedNumberOfAttempts = grailsApplication.config.brutforce.loginAttempts.allowedNumberOfAttempts
        int time = grailsApplication.config.brutforce.loginAttempts.time

        log.info 'account block configured for $time minutes'
        attempts = CacheBuilder.newBuilder()
                   .expireAfterWrite(time, TimeUnit.MINUTES)
                   .build({0} as CacheLoader);
    }

    /**
     * Triggers on each unsuccessful login attempt and increases number of attempts in local accumulator
     * @param login - username which is trying to login
     * @return
     */
    def failLogin(String login) {
        def numberOfAttempts = attempts.get(login)
        log.debug 'fail login $login previous number for attempts $numberOfAttempts'
        numberOfAttempts++

        if (numberOfAttempts > allowedNumberOfAttempts) {
            blockUser(login)
            attempts.invalidate(login)
        } else {
            attempts.put(login, numberOfAttempts)
        }
    }

    /**
     * Triggers on each successful login attempt and resets number of attempts in local accumulator
     * @param login - username which is login
     */
    def loginSuccess(String login) {
        log.debug 'successfull login for $login'
        attempts.invalidate(login)
    }

    /**
     * Disable user account so it would not able to login
     * @param login - username that has to be disabled
     */
    private void blockUser(String login) {
        log.debug 'blocking user: $login'
        def user = User.findByUsername(login)
        if (user) {
            user.accountLocked = true;
            user.save(flush: true)
        }
    }
}
私たちはGoogle番石榴庫の中のCacheBuiderを使います。そこで、次の行をBuiildConfig.groovyに追加します。
dependencies {
        runtime 'com.google.guava:guava:11.0.1'
        }
最後のステップは、サービス構成をcinfig.groovyに追加します。
brutforce {
    loginAttempts {
        time = 5
        allowedNumberOfAttempts = 3
    }
このように、あなたはあなたのアプリケーションを実行します。典型的なJavaプロジェクトについてはほとんどすべて同じです。同じリスニングマシンと同じサービスです。Spring Security Eventsに関する詳細は、Google番ざくろを使ってキャッシュすることに関するより多くの情報です。
Grairsユーザはこのプラグインを簡単に使用できます。https://github.com/grygoriy/bruteforcedefender
プログラムを楽しく作ってください。共有を忘れないでください。
参考:Grygoriy MykhalyunoブログのJCGパートナーGrygoriy MykhalyunoはSpring Securityを使って暴力攻撃を防止します。
翻訳元:https://www.javacodegeeks.com/2012/10/spring-security-prevent-brute-force.html