Spring安全:防止暴力攻击

Spring Security可以为您做很多事情。

帐户被封锁,密码盐。 但是蛮力阻断剂呢?

那是你必须自己做的。

幸运的是,Spring是一个非常灵活的框架,因此对其进行配置并不是什么大问题。

让我向您展示一些如何针对Grails应用程序执行此操作的指南。

首先,您必须在config.groovy中启用springSecurityEventListener

grails.plugins.springsecurity.useSecurityEventListener = true

然后实现监听器
在/ src / bruteforce中创建类

/**
Registers all failed attempts to login. Main purpose to count attempts for particular account ant block user*/
class AuthenticationFailureListener implements ApplicationListener {LoginAttemptCacheService loginAttemptCacheService@Overridevoid 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@Overridevoid onApplicationEvent(AuthenticationSuccessEvent e) {loginAttemptCacheService.loginSuccess(e.authentication.name)}
}

我们没有将它们放在grails-app文件夹中,因此我们需要将这些类作为spring bean重新命名。
在grails-app / conf / spring / resources.groovy中添加下一行

beans = {authenticationFailureListener(AuthenticationFailureListener) {loginAttemptCacheService = ref('loginAttemptCacheService')}authenticationSuccessEventListener(AuthenticationSuccessEventListener) {loginAttemptCacheService = ref('loginAttemptCacheService')}
}

您可能会注意到LoginAttemptCacheService loginAttemptCacheService的用法
让我们实现它。 这将是典型的grails服务

package com.picsel.officeanywhereimport com.google.common.cache.CacheBuilder
import com.google.common.cache.CacheLoader
import com.google.common.cache.LoadingCacheimport java.util.concurrent.TimeUnit
import org.apache.commons.lang.math.NumberUtils
import javax.annotation.PostConstructclass LoginAttemptCacheService {private LoadingCacheattempts;private int allowedNumberOfAttemptsdef grailsApplication@PostConstructvoid init() {allowedNumberOfAttempts = grailsApplication.config.brutforce.loginAttempts.allowedNumberOfAttemptsint time = grailsApplication.config.brutforce.loginAttempts.timelog.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番石榴库中的CacheBuilder。 因此,将下一行添加到BuildConfig.groovy

dependencies {runtime 'com.google.guava:guava:11.0.1'}

最后一步,将服务配置添加到cinfig.groovy

brutforce {loginAttempts {time = 5allowedNumberOfAttempts = 3}

就是这样,您准备运行您的应用程序。
对于典型的Java项目,几乎一切都是一样的。 相同的侦听器和相同的服务。
有关Spring Security Events的更多信息 有关使用Google番石榴进行缓存的更多信息

Grails用户可以简单地使用此插件https://github.com/grygoriy/bruteforcedefender

祝您编程愉快,别忘了分享!

参考: Grygoriy Mykhalyuno博客博客中的JCG合作伙伴 Grygoriy Mykhalyuno 使用Spring Security防止暴力攻击 。

翻译自: https://www.javacodegeeks.com/2012/10/spring-security-prevent-brute-force.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/371656.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

NopCommerce计划任务

NopCommerce计划任务转载于:https://www.cnblogs.com/chenjz/p/6293210.html

简单谈谈js中的MVC

MVC是什么? MVC是一种架构模式,它将应用抽象为3个部分:模型(数据)、视图、控制器(分发器)。 本文将用一个经典的例子todoList来展开(代码在最后)。 一个事件发生的过程&a…

BTrace:Java开发人员工具箱中的隐藏宝石

这篇文章是关于BTrace的 ,我正在考虑将其作为Java开发人员的隐藏宝藏。 BTrace是用于Java平台的安全,动态跟踪工具。 BTrace可用于动态跟踪正在运行的Java程序(类似于DTrace,适用于OpenSolaris应用程序和OS)。 不久&am…

python 图片转视频ffmpeg_python图片转视频(opencv),ffmpeg压缩视频

要注意:1. 图片传视频要自己设置帧率和分辨率2.读取图片后分辨率要resize为和视频分辨率一样才可以3.写完.avi视频后视频比较大,用ffmpeg将avi视频压缩为mp4import cv2from cv2 import VideoWriter, VideoWriter_fourcc, imread, resizeimport osfrom su…

门面模式

门面模式的定义 门面模式(Facade Pattern)也叫做外观模式,是一种比较常用的封装模式,其定义如 下: Provide a unified interface to a set of interfaces in a subsystem.Facade defines a higher-level interface tha…

Mysql数据库申请

前段时间大部门下新成立了一个推广百度OCR、文字识别、图像识别等科技能力在金融领域应用的子部门。因为部门刚成立,基础设施和人力都是欠缺的。当时分到我们部门的任务是抽调一个人做新部门主站前端开发工作。本来说的是只负责页面的开发工作。当我参加过需求品审会…

Spring–添加SpringMVC –第2部分

在上一部分中,我们为经理和员工实现了控制器。 既然我们知道了解决方法,我们将做很少(但仅做很少)更复杂的事情–任务和时间表的控制器。 因此,让我们从org.timesheet.web开始。 TaskController 。 首先创建一个类&…

php 正则分隔_探讨PHP函数split()如何使用正则表达式切割字符串

对于初学者来说,掌握PHP中常用函数的用法,是其继续学习的基础。今天我们就为大家详细介绍有关PHP函数split()的一些使用方法,希望大家能通过这篇文章介绍的内容增加自己的知识库。说明array split ( string $pattern, string $string [, int …

通用的ProtostuffSerializer for Java

以前使用 protobuf或protostuff的时候觉得很麻烦,每个类都要单独定制,于是封装了一个类。 同事测试过,性能和压缩率都很好,尤其是相比json的序列化。 需注意:只支持Pojo类(即需要有get/set方法)…

SAS笔记(6) PROC MEANS和PROC FREQ

PROC MEANS和PRC FREQ在做描述性分析的时候很常用,用法也比较简单,不过这两个过程步的某些选项容易忘记,本文就梳理一下。 在进入正文前,我们先创建所需的数据集TEST_SCORES: DATA TEST_SCORES; INPUT COUNTY : $9. SC…

休眠:保存vs持久并保存或更新

save和saveOrUpdate之间的区别是什么或save和persist之间的区别是任何Hibernate面试中常见的面试问题,就像Hibernate中get和load方法之间的区别一样。 Hibernate Session类提供了几种通过save , saveOrUpdate和persist等方法将对象保存到数据库中的方法。…

php搜索数据库设计,PHP数据库搜索功能设计

其实搜索功能的设计很简单,几行代码就可以完成。下面是form表单。从表单发出的数据名为search,然后发送到../admin/article_SearchResult.php这个文件处理。下面讲下article_SearchResult.php这个文件如何实现搜索。$searchs $_POST[‘search‘];?>…

2016 Android Top 10 Library

过去的 2016 年,开源社区异常活跃,很多个人与公司争相开源自己的项目,让人眼花缭乱,然而有些项目只是昙花一现,有些项目却持久创造价值,为开发者提供了极大的便利,这些终究由时间来判断。今天&a…

集成JavaFX和Swing

我刚刚完成了对使用Swing的应用程序组件的重写,现在正在使用JavaFX,最后得到了与更大的swing应用程序集成的JavaFX组件。 这是一个很大的应用程序,重写花了我一段时间,最后一切都很好,我很高兴自己做到了。 您可能想在…

提示错误:“应为“providerInvariantName”参数的非空字符串。”

我在调试Petapoco的T4模版的时候&#xff0c;链接一直报如题那个错误。在定性问题为配置文件后找的原因如下&#xff1a; <connectionStrings><add name"这个不行" connectionString"Data Sourcexxx;Initial Catalog数据库名;User ID帐号;Password密码…

php oop面试题,PHP面试题 - 对面向对象的理解

具体的题目应该是&#xff1a;什么是面向对象&#xff1f;主要的特征是什么&#xff1f;当然还有很多类似的题目&#xff0c;如果你说一下你对面向对象的理解&#xff0c;或者是你对比一下面向过程等等&#xff0c;诸如此类吧&#xff1f;如果我来回答这个问题&#xff0c;我会…

NOIP2014自测(晚自习两节+上午两节 共5个小时)

昨天刚刚考完试然后就翘晚自习跟今天上午两节课的语文和英语做做noip2014的题目。然后去评测了一番。首先day1day2的t1基本都是模拟&#xff0c;一看就出思路那种&#xff0c;直接ac掉。代码如下 day1t1&#xff1a;#include<iostream>#define maxn 209using namespace s…

您在eXo平台上的第一个Juzu Portlet

菊珠是佛教的佛珠。 一句话&#xff0c;我相信您已经学到了什么&#xff0c;印象深刻吗&#xff1f; 好的&#xff0c;我在这里不谈论佛教。 Juzu还是一个用于快速开发Portlet&#xff08;以及即将推出的独立应用程序&#xff09;的新框架。 您可以在Juzu网站上找到所需的所有…

Spring注入方式及注解配置

一&#xff1a;基于xml的DI&#xff08;Dependency Injection&#xff09; 注入类型&#xff1a; 定义学生Student实体类和小汽车Car实体类&#xff1a;进行封装和生成ToString(),并自定义属性Car Student 123456789101112131415161718192021222324252627282930313233343536373…

java 切面 不执行,解决springboot的aop切面不起作用问题(失效的排查)

检查下springboot的启动类是否开启扫描springbootapplicationcomponentscan(basepackages {"com.zhangpu.springboot"})另外springboot默认开启的enableaspectjautoproxy为true如果不放心可以增加&#xff1a;enableaspectjautoproxy(proxytargetclasstrue)第二种可…