当前位置 主页 > 网站技术 > 代码类 >

    SpringBoot下token短信验证登入登出权限操作(token存放redis,ali

    栏目:代码类 时间:2019-11-12 12:06

    SpringBoot下token短信验证登入登出(token存放redis)

    不对SpringBoot进行介绍,具体的可以参考官方文档

    介绍:token基本使用,redis基本使用

    思路:获取短信(验证并限制发送次数,将code存放redis)-->登入(验证并限制错误次数,将用户信息及权限放token,token放redis)-->查询操作(略),主要将前两点,不足的希望指出,谢谢

    步骤:

    1.整合Redis需要的依赖,yml自行配置,ali短信接口依赖(使用引入外部包的方式)

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
      <groupId>ali</groupId>
      <artifactId>taobao-sdk-java-auto</artifactId>
      <scope>system</scope>
      <!--将jar包放在项目/libs/xxx.jar-->
      <systemPath>${project.basedir}/libs/taobao-sdk-java-auto.jar</systemPath>
    </dependency>
    .......
    <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
              <!--打包的时候引用该属性的包-->
              <includeSystemScope>true</includeSystemScope>
            </configuration>
          </plugin>
        </plugins>
      </build>

    2.ali短信接口工具类,发送验证码

    @Autowired
      private StringRedisTemplate redisTemplate;
    ....略....
    //查询是否有此用户,记录单位时间内发送短信次数,并限制发送次数
    Account account = accountService.findByUserName(phone);
    if (account==null){
     return ResultVOUtil.erro(0,"未注册用户");
     }
    ValueOperations<String, String> ops = redisTemplate.opsForValue();
    String getTimes = ops.get(account + "code");
    Integer gts=getTimes==null?0:Integer.valueOf(getTimes);
    if (gts>5){
     return ResultVOUtil.erro(0,"获取短信次数过多,请稍后再试");
    }
    ops.set(account+"code",String.valueOf(gts+1),5,TimeUnit.MINUTES);
    NoteUtils noteUtils=new NoteUtils();
    String validCode = UidUtil.getValidCode(); //生成随机数
    try {
     String yzmcode = noteUtils.yzmcode(validCode, phone);
      //redis设置验证码有效时间5分组
     ops.set(phone,validCode,5,TimeUnit.MINUTES);
     }catch (Exception e){
       throw new YunExceptions(0,"获取验证码服务器bug");
     }
     
    //短信接口工具类
    public class NoteUtils {
      //仅当示例:具体参考官方文档
      public String url="***************";
      public String appkey="****************";
      public String secret="*********************";
      public String yzmcode(String code,String telnum) throws ApiException, JSONException {
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend( "extend" );
        req.setSmsType( "normal" );
        req.setSmsFreeSignName( "*************" );
        req.setSmsParamString( "{code:'"+code+"'}" );
        req.setRecNum(telnum);
        req.setSmsTemplateCode( "******************" );
        AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
        return "true";
     
      }
    }