5 Star 3 Fork 1

北京掌趣无限科技有限公司 / SecurityBasicParent

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

SecurityBasicParent

介绍

security-basic 基于 SpringSecurity 框架进行二次开发,提供安全拓展

  • 微信授权AUTH认证
  • 短信验证码认证
  • 图片验证码防护
  • 接口安全验证
  • 重放攻击检测

软件架构

  • spring-boot 2.1.9release
  • spring-redis-starter
  • spring-security-starter

使用SecurityBasic

安全认证模块

添加Maven依赖

        <groupId>cn.infinite.security</groupId>
        <artifactId>security-basic-spring-boot-starter</artifactId>
        <version>1.0-SNAPSHOT</version>
使用微信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:
  1. 关联用户服务

通过微信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;
    }
  1. 自定义微信认证成功处理器

实现 WxAuthenticationSuccessHandler 接口,并注入到Spring容器中,框架会在微信认证成功后调用该实例的onAuthenticationSuccess方法

    @Component
    public class SimpleWxAuthenticationSuccessHandle implement WxAuthenticationSuccessHandler {
        @Override
        public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication
                authentication) throws IOException {
            //业务逻辑
        }
    
    }

  1. 自定义微信认证失败处理器

实现 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:
            ~~~
  1. 自定义短信认证成功处理器

实现 SmsAuthenticationSuccessHandler 接口,并注入到Spring容器来实现 自定义认证成功处理器,如果开发者未实现此接口,框架会提供缺省实现 DefaultSmsAuthenticationSuccessHandlerImpl; 缺省实现中,会根据请求中Accept 来判断是浏览器地址跳转引发的请求,还是页面Ajax异步请求,相应的响应方式也不同,浏览器触发的请求会引导引导进行重定向,ajax请求会响应 json 格式的认证结果信息

  1. 自定义短信认证失败处理器

实现 SmsAuthenticationFailureHandler 接口,并注入到Spring容器来实现 自定义认证失败处理器,如果开发者未实现此接口,框架会提供缺省实现 DefaultSmsAuthenticationFailureHandler ;缺省实现中,会根据请求中Accept 来判断是浏览器地址跳转引发的请求,还是页面Ajax异步请求,相应的响应方式也不同,浏览器触发的请求会引导引导进行重定向,ajax请求会响应 json 格式的认证结果信息

  1. 短信验证码的下发和认证

当系统需要支持短信认证,我们至少需要考虑以下两个问题:

  • 短信验证码下发渠道
  • 短信验证码的验证

这两个问题都是不确定的,在不同应用中,短信下发渠道和短信验证码验证接口也不尽一样;

短信的下发 需要开发者自己实现,然后将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保护的安全链接;

开启重放攻击检测

空文件

简介

取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/infinatefun/SecurityBasicParent.git
git@gitee.com:infinatefun/SecurityBasicParent.git
infinatefun
SecurityBasicParent
SecurityBasicParent
master

搜索帮助