1. 介绍
提供简单的验证图片功能
2. 使用
使用在线initilizer工具生成的工程中默认提供此插件。
也可以通过以下方式在工程中添加
2.1. build.gradle文件
implementation('org.yunchen.gb:gb-plugin-springsecurity-captcha:1.4.0.0.M1')
2.2. application.yml文件
在application.yml文件中加入如下的配置,注意gb顶级项的合并
gb:
springsecuritycaptcha:
enabled: true #启用
time: 5 # 过期时间 (分钟)
allowedNumberOfAttempts: 3 #无验证码尝试次数
2.3. 修改i18n文件
在messages_zh_CN.properties中增加
springSecurity.errors.captcha.invalid=验证码不正确
在messages.properties中增加
springSecurity.errors.captcha.invalid=Validation code is incorrect
2.4. 修改loginController文件
在getExceptionMessage方法中增加对CaptchaVerificationFailedException的判断
private String getExceptionMessage(HttpSession httpSession){
String msg = '';
def exception = httpSession.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION)
if (exception) {
if (exception instanceof AccountExpiredException) {
msg = messageSource.getMessage("springSecurity.errors.login.expired",[].toArray(),LocaleContextHolder.getLocale())
}
else if (exception instanceof CredentialsExpiredException) {
msg = messageSource.getMessage("springSecurity.errors.login.passwordExpired",[].toArray(),LocaleContextHolder.getLocale())
}
else if (exception instanceof DisabledException) {
msg = messageSource.getMessage("springSecurity.errors.login.disabled",[].toArray(),LocaleContextHolder.getLocale())
}
else if (exception instanceof LockedException) {
msg = messageSource.getMessage("springSecurity.errors.login.locked",[].toArray(),LocaleContextHolder.getLocale())
}else if (exception instanceof SessionAuthenticationException){
msg = messageSource.getMessage("springSecurity.errors.login.maximum",[].toArray(),LocaleContextHolder.getLocale())
}else if (exception instanceof CaptchaVerificationFailedException){
msg = messageSource.getMessage("springSecurity.errors.captcha.invalid",[].toArray(),LocaleContextHolder.getLocale())
}else {
//org.springframework.security.authentication.BadCredentialsException: Bad credentials
//用户名或密码不正确
msg = messageSource.getMessage("springSecurity.errors.login.fail",[].toArray(),LocaleContextHolder.getLocale())
}
}
return msg;
}
2.5. 修改auth页面文件
themleaf3中,在auth.html中增加验证码片段
<div th:unless="${#httpServletRequest.getSession().getAttribute('captchaForLogin')==null}">
<div class="form-group">
<div class="col-md-6">
<img th:src="${#httpServletRequest.contextPath}+'/simpleCaptcha/image'"/>
</div>
<div class="col-md-6">
<input type="text" name="captchaChallenge" class="form-control" placeholder="验证码"/>
</div>
</div>
</div>
2.6. 增加requestmap访问控制
在Startup.groovy文件中,中增加一条requestmap记录,配置/simpleCaptcha地址的访问权限 注意添加在/**的访问配置前面
new Requestmap(name:'simpleCaptcha管理',url: '/simpleCaptcha/**', configAttribute: 'permitAll').save(flush: true);