验证码拦截器
验证码拦截器,通常使用在用户登录
场景,用户登录时输入图片验证码,验证成功后,调用登录接口。可以避免暴力破解漏洞。
拦截地址配置
验证码拦截器模式不启用
enabled
:是否启用拦截器,默认不启用false
url
:需要拦截的地址,多个逗号分隔
url匹配规则:
- 模糊匹配:可以通过
/**
或/*
模糊匹配所有的地址 - 请求类型匹配,匹配
GET
或POST
请求,示例POST /login
wueasy:
gateway:
interceptor:
captcha:
enabled: true #是否启用,默认false
url: POST /login
redis配置
当使用动态口令验证时,必须加载以下redis信息,用于存储每分钟验证次数(解决暴力破解安全漏洞)。
wueasy :
data:
redis: #redis配置
captcha: #验证相关redis配置
database : 1 #数据库索引(默认为0)
host : 127.0.0.1 #服务器地址
port : 6379 #服务器连接端口
password : 123456 #服务器连接密码(默认为空)
pool : #连接池配置
maxIdle : 8 # 连接池中的最大空闲连接,默认值也是8。
minIdle : 0 #连接池中的最小空闲连接,默认值也是0。
maxTotal : 2000 # 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
maxWaitMillis : 1000 # 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException
timeout : 3000 #连接超时时间(毫秒)
验证码类型配置
腾讯验证码地址
https://007.qq.com
3.4.0以上版本
验证码类型:
NONE
:不开启验证码,默认IMAGE
:图片验证码ONETIME
:动态口令QQ
:腾讯验证码CUSTOM
:自定义验证码实现,3.7.3新增
wueasy:
gateway:
interceptor:
captcha:
type: IMAGE #验证码类型
qqApps: #腾讯验证码应用配置
- appId: #应用id
appSecret: #应用密钥
3.x版本
验证码类型,0:不开通验证码,1:普通验证码,2:腾讯验证码,3:动态口令。默认为0
wueasy:
gateway:
interceptor:
captcha:
type: 1 #验证码类型
qqApps: #腾讯验证码应用配置
- appId: #应用id
appSecret: #应用密钥
请求参数说明
需要传入验证码信息,进行验证码验证。
普通验证码(图片验证码)
普通验证码需要传入,需要传入图片的验证码信息。
图片接口地址/captcha
validateCode
:图片验证码参数
腾讯验证码
ticket
:验证码客户端验证回调的票据randstr
:验证码客户端验证回调的随机串aid
:应用id
动态口令验证
validateCode
:动态口令值
动态口令需要继承
com.wueasy.gateway.security.service.UserAuthenticatorService
接口实现接口中的
getUserSecret
方法,返回当前用户的动态口令密钥
参考示例
/**
* 用户动态口令服务
* @author: fallsea
* @version 1.0
*/
@Service
public class UserAuthenticatorServiceImpl implements UserAuthenticatorService {
@Override
public String getUserSecret() {
//获取session
Session session = SessionUtil.getSession();
//获取request信息
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = servletRequestAttributes.getRequest();
//返回密钥
return "123456";
}
}
自定义验证码
validateCode
:验证码值
动态口令需要继承
com.wueasy.gateway.security.service.CaptchaValidateService
接口实现接口中的
validate
方法,返回当前验证结果
参考示例
/**
* 自定义验证码服务
* @author: fallsea
* @version 1.0
*/
@Service(GatewayConstants.CAPTCHA_VALIDATE_CUSTOM)
public class CaptchaValidateCustomServiceImpl implements CaptchaValidateService {
@Override
public boolean validate() {
//获取session
Session session = SessionUtil.getSession();
//获取request信息
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = servletRequestAttributes.getRequest();
//返回验证结果,true 验证通过,false 验证失败
return true;
}
}