security-basic
基于 SpringSecurity
框架进行二次开发,提供安全拓展
SecurityBasic
添加Maven依赖
<groupId>cn.infinite.security</groupId>
<artifactId>security-basic-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
在应用启动类添加注解 @EnableWxAuthenticate
即可开启微信认证
WxConfigurerProperties
) spring:
security:
security-basic:
wx-authenticate:
app-id: #公众号appId
secret: #密钥
authenticate-end-point:
authenticate-call-back-point:
authenticate-success-url:
通过微信Auth授权,我们可以得到用户的openId信息,但是如果与业务系统集成,就必须将openId 与业务系统的用户服务关联,能够根据openId获取用户信息,完成认证流程
框架提供了 WxUserService
接口,开发者只需要实现此接口,并将实例注入到Spring容器中,即可将用户服务关联到认证流程中
接口定义如下
public interface WxUserService {
/**
* 根据openId 获取用户详情
* @param openId
* @return
* @throws Exception
*/
Optional<UserDetails> findUserDetailByOpenId(String openId) throws Exception;
/**
* 自动注册,传入openId,以及获取到的微信用户的信息
*
* @param openId
* @param jsonObject 用户信息
* @return
* @throws Exception
*/
UserDetails registerUserByOpenId(String openId, String jsonObject) throws Exception;
}
实现 WxAuthenticationSuccessHandler
接口,并注入到Spring容器中,框架会在微信认证成功后调用该实例的onAuthenticationSuccess
方法
@Component
public class SimpleWxAuthenticationSuccessHandle implement WxAuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication
authentication) throws IOException {
//业务逻辑
}
}
实现 WxAuthenticationFailureHandler
接口,并注入到Spring容器中,框架会在微信认证跑出异常后调用该实例的onAuthenticationFailure
方法
@Component
public class DefaultWxAuthenticationFailureHandler implements WxAuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) throws IOException, ServletException {
log.error("wx authenticate error =>{}", exception.getMessage());
}
}
在应用启动类添加注解 @EnableSmsAuthenticate
即可开启短信认证
SmsConfigurerProperties
) spring:
security:
security-basic:
sms-authenticate:
scope-path:
authenticate-success-url:
~~~
实现 SmsAuthenticationSuccessHandler
接口,并注入到Spring容器来实现 自定义认证成功处理器,如果开发者未实现此接口,框架会提供缺省实现
DefaultSmsAuthenticationSuccessHandlerImpl
; 缺省实现中,会根据请求中Accept
来判断是浏览器地址跳转引发的请求,还是页面Ajax异步请求,相应的响应方式也不同,浏览器触发的请求会引导引导进行重定向,ajax请求会响应 json 格式的认证结果信息
实现 SmsAuthenticationFailureHandler
接口,并注入到Spring容器来实现 自定义认证失败处理器,如果开发者未实现此接口,框架会提供缺省实现
DefaultSmsAuthenticationFailureHandler
;缺省实现中,会根据请求中Accept
来判断是浏览器地址跳转引发的请求,还是页面Ajax异步请求,相应的响应方式也不同,浏览器触发的请求会引导引导进行重定向,ajax请求会响应 json 格式的认证结果信息
当系统需要支持短信认证,我们至少需要考虑以下两个问题:
这两个问题都是不确定的,在不同应用中,短信下发渠道和短信验证码验证接口也不尽一样;
短信的下发 需要开发者自己实现,然后将Controller 的映射配置到配置文件中,框架会自动配置该端点,允许Spring Security
放行请求
spring:
security:
security-basic:
sms-authenticate:
send-sms-code-end-point: #配置短信下发端点
短信验证码的认证 框架提供SmsAuthenticateAble
接口,开发者可实现该接口,并注入到Spring容器中
SmsAuthenticateAble接口
定义如下
public interface SmsAuthenticateAble {
/**
* 根据手机号和短信验证码 验证用户
* @param mobile 手机号
* @param smsCode 短信验证码
* @return 验证成功 后返回用户
* @throws AuthenticationException
*/
UserDetails smsAuthenticate(String mobile, String smsCode) throws AuthenticationException;
}
短信验证码一般情况下都是 数字类型的,并且长度一般只有6位,必须采取措施来防止攻击者采用暴力破解的方式,框架提供了 图片验证码的实现帮助开发者快速集成
图片验证码工作原理:
在配置文件中开启 (详细配置参见 ImageCheckCodeConfigProperties
)
spring:
security:
security-basic:
image-check-code:
enable: true #开启图片验证码保护
详细配置中可以对图片验证码端点、验证码图片尺寸、验证码长度等选项进行配置
先引入一个概念 安全端点
: 开发者使用@Controller
和 @RequestMapping
创建一个Http请求能够访问的方法,我们称这个方法为 端点
,如果与这个端点进行交互,需要签名、加密 那么我们称这个端点为安全端点
;有点类似被SSL保护的安全链接;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。