spring mvc 發送短信驗證碼功能 阿里大於

我們自己的服務器是不具備發送短信的功能的 發送短信需要藉助第三方平臺
這裏選擇的是阿里大於 原因:免費給10塊錢 測試足夠了。。。。

新建應用

申請模板 模板要符合規範 可以帶變量 比如我這個

驗證碼:${number},打死不告訴別人!

下載jar包

我這裏用的maven maven項目添加本地jar包 有幾種解決方案 比如安裝到本地倉庫
或者直接在pom文件中使用 路徑加載

  <dependency>
            <groupId>com.taobao</groupId>
            <artifactId>taobao</artifactId>
            <version>1.1.1</version>
            <scope>system</scope>
            <!--本地jar的路徑,相對或者絕對都可以-->
            <systemPath>${project.basedir}/lib/taobao-sdk-java-auto_1455552377940-20160607-source.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>com.taobao1</groupId>
            <artifactId>taobao1</artifactId>
            <version>1.1.1</version>
            <scope>system</scope>
            <!--本地jar的路徑,相對或者絕對都可以-->
            <systemPath>${project.basedir}/lib/taobao-sdk-java-auto_1455552377940-20160607.jar</systemPath>
        </dependency>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <warName>${project.artifactId}</warName>
                    <webResources>
                        <resource>
                            <directory>lib/</directory>
                            <targetPath>WEB-INF/lib</targetPath>
                            <includes>
                                <include>**/*.jar</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>

代碼

相關id換成自己申請的

/**
     * 獲取驗證碼
     * @return
     * @throws ApiException
     */
    @RequestMapping(value = "/vcode",method = RequestMethod.POST)
    @ResponseBody
    public String getVcode(String phone, HttpSession session) throws ApiException {
        TaobaoClient client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest",
                "23648980", "d4fc983e69b0172cc8d9f0355a32a4d9");
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend("");
        req.setSmsType("normal");
        req.setSmsFreeSignName("快遞幫");
        //生成驗證碼數字
        String vcode = VcodeUtils.gentVcode(6);
        //存到session域中
        session.setAttribute("vcode",vcode);
        logger.info("驗證碼:"+vcode);
        req.setSmsParamString("{number:'"+vcode+"'}");
        req.setRecNum(phone);
        req.setSmsTemplateCode("SMS_49000057");
        AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
        System.out.println(rsp.getBody());
        return rsp.getBody();
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章