# SecurityBasicParent **Repository Path**: infinatefun/SecurityBasicParent ## Basic Information - **Project Name**: SecurityBasicParent - **Description**: No description available - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 1 - **Created**: 2019-03-23 - **Last Updated**: 2021-01-07 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # SecurityBasicParent #### 介绍 `security-basic` 基于 `SpringSecurity` 框架进行二次开发,提供安全拓展 - 微信授权AUTH认证 - 短信验证码认证 - 图片验证码防护 - 接口安全验证 - 重放攻击检测 #### 软件架构 - spring-boot 2.1.9release - spring-redis-starter - spring-security-starter #### 使用`SecurityBasic` ##### 安全认证模块 添加Maven依赖 ``` cn.infinite.security security-basic-spring-boot-starter 1.0-SNAPSHOT ``` ###### 使用微信Auth认证 **在应用启动类添加注解** `@EnableWxAuthenticate` 即可开启微信认证 1. 配置微信AUTH授权 (_详细配置参见`WxConfigurerProperties`_) ``` spring: security: security-basic: wx-authenticate: app-id: #公众号appId secret: #密钥 authenticate-end-point: authenticate-call-back-point: authenticate-success-url: ``` 2. 关联用户服务 通过微信Auth授权,我们可以得到用户的openId信息,但是如果与业务系统集成,就必须将openId 与业务系统的用户服务关联,能够根据openId获取用户信息,完成认证流程 框架提供了 `WxUserService` 接口,开发者只需要实现此接口,并将实例注入到Spring容器中,即可将用户服务关联到认证流程中 > 接口定义如下 ``` public interface WxUserService { /** * 根据openId 获取用户详情 * @param openId * @return * @throws Exception */ Optional findUserDetailByOpenId(String openId) throws Exception; /** * 自动注册,传入openId,以及获取到的微信用户的信息 * * @param openId * @param jsonObject 用户信息 * @return * @throws Exception */ UserDetails registerUserByOpenId(String openId, String jsonObject) throws Exception; } ``` 3. 自定义微信认证成功处理器 实现 `WxAuthenticationSuccessHandler` 接口,并注入到Spring容器中,框架会在微信认证成功后调用该实例的`onAuthenticationSuccess`方法 ``` @Component public class SimpleWxAuthenticationSuccessHandle implement WxAuthenticationSuccessHandler { @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException { //业务逻辑 } } ``` 4. 自定义微信认证失败处理器 实现 `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` 即可开启短信认证 1. 配置短信认证 (详细配置参见 _`SmsConfigurerProperties`_) ``` spring: security: security-basic: sms-authenticate: scope-path: authenticate-success-url: ~~~ ``` 2. 自定义短信认证成功处理器 实现 `SmsAuthenticationSuccessHandler` 接口,并注入到Spring容器来实现 自定义认证成功处理器,如果开发者未实现此接口,框架会提供缺省实现 `DefaultSmsAuthenticationSuccessHandlerImpl`; 缺省实现中,会根据请求中`Accept` 来判断是浏览器地址跳转引发的请求,还是页面Ajax异步请求,相应的响应方式也不同,浏览器触发的请求会引导引导进行重定向,ajax请求会响应 json 格式的认证结果信息 3. 自定义短信认证失败处理器 实现 `SmsAuthenticationFailureHandler` 接口,并注入到Spring容器来实现 自定义认证失败处理器,如果开发者未实现此接口,框架会提供缺省实现 `DefaultSmsAuthenticationFailureHandler` ;缺省实现中,会根据请求中`Accept` 来判断是浏览器地址跳转引发的请求,还是页面Ajax异步请求,相应的响应方式也不同,浏览器触发的请求会引导引导进行重定向,ajax请求会响应 json 格式的认证结果信息 4. 短信验证码的下发和认证 当系统需要支持短信认证,我们至少需要考虑以下两个问题: - 短信验证码下发渠道 - 短信验证码的验证 这两个问题都是不确定的,在不同应用中,短信下发渠道和短信验证码验证接口也不尽一样; _短信的下发_ 需要开发者自己实现,然后将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保护的安全链接; ###### 开启重放攻击检测