亚洲国产精品乱码一区二区,美景房屋2免费观看,哎呀哎呀在线观看视频高清国语,从镜子里看我是怎么C哭你

Article / 文章中心

記一次短信盜刷問題的解決方案

發(fā)布時(shí)間:2020-03-24 點(diǎn)擊數(shù):4107

來源: CSDN

編輯:劉光昕

前言

        最近公司的注冊(cè)接口經(jīng)常在半夜被惡意訪問,從而引發(fā)短信盜刷事件,原本在手機(jī)號(hào)等參數(shù)校驗(yàn)通過后,注冊(cè)接口會(huì)對(duì)圖形驗(yàn)證碼進(jìn)行正確性校驗(yàn),校驗(yàn)通過后再進(jìn)行短信發(fā)送。通過短信發(fā)送記錄發(fā)現(xiàn)我們的圖形驗(yàn)證碼很容易就被識(shí)別了,沒有起到安全過濾的作用,同時(shí)對(duì)短信發(fā)送次數(shù)沒有進(jìn)行上限設(shè)置,所以這此短信盜刷問題我們做了以下解決方案。

一、圖形驗(yàn)證碼增加識(shí)別難度

1.1自定義圖形驗(yàn)證碼

我們需要實(shí)現(xiàn)一個(gè)生成圖形驗(yàn)證碼的工具類VerifyCodeImageUtil.java

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import java.awt.geom.AffineTransform;

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.util.Arrays;

import java.util.Random;

public class VerifyCodeImageUtil {

    //去除I、l、1、0、o、O這這些容易混淆的字符

    public static final String VERIFY_CODES = "23456789ABCDEFGHJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz";

    private static int WEIGHT=230;

    private static int HIGH=100;

    private static Random random = new Random();

    /**

     * 使用系統(tǒng)默認(rèn)字符源生成驗(yàn)證碼

     * @param verifySize    驗(yàn)證碼長(zhǎng)度

     * @return

     */

    public static String generateVerifyCode(int verifySize){

        return generateVerifyCode(verifySize, VERIFY_CODES);

    }

    /**

     * 使用指定源生成驗(yàn)證碼

     * @param verifySize    驗(yàn)證碼長(zhǎng)度

     * @param sources   驗(yàn)證碼字符源

     * @return

     */

    public static String generateVerifyCode(int verifySize, String sources){

        if(sources == null || sources.length() == 0){

            sources = VERIFY_CODES;

        }

        int codesLen = sources.length();

        Random rand = new Random(System.currentTimeMillis());

        StringBuilder verifyCode = new StringBuilder(verifySize);

        for(int i = 0; i < verifySize; i++){

            verifyCode.append(sources.charAt(rand.nextInt(codesLen-1)));

        }

        return verifyCode.toString();

    }

 

    public static BufferedImage createVerifyImageNew(String verifyCode){

        BufferedImage image=getImage(WEIGHT, HIGH, verifyCode);

        return image;

        }

 

 

    /**

     * 設(shè)置驗(yàn)證碼圖片

     * @param w

     * @param h

     * @param code

     * @return

     */

    public static BufferedImage getImage(int w,int h,String code){

        int verifySize = code.length();

        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

        Random rand = new Random();

        Graphics2D g2 = image.createGraphics();

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

        Color[] colors = new Color[5];

        Color[] colorSpaces = new Color[] { Color.WHITE, Color.CYAN,

                Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,

                Color.PINK, Color.YELLOW };

        float[] fractions = new float[colors.length];

        for(int i = 0; i < colors.length; i++){

            colors[i] = colorSpaces[rand.nextInt(colorSpaces.length)];

            fractions[i] = rand.nextFloat();

        }

        Arrays.sort(fractions);

 

        g2.setColor(Color.GRAY);// 設(shè)置邊框色

        g2.fillRect(0, 0, w, h);

 

        Color c = getRandColor(200, 250);

        g2.setColor(c);// 設(shè)置背景色

        g2.fillRect(0, 2, w, h-4);

 

        //繪制干擾線

        Random random = new Random();

        g2.setColor(getRandColor(10, 200));

        for (int i = 0; i < 100; i++) {//干擾線的條數(shù)

            int x = random.nextInt(w - 10);

            int y = random.nextInt(h - 20);

            int xl = random.nextInt(10) + 1;

            int yl = random.nextInt(20) + 1;

            g2.drawLine(x, y, x + xl + 100, y + yl + 120);

        }

 

        // 添加噪點(diǎn)

        float yawpRate = 0.20f;// 噪聲率

        int area = (int) (yawpRate * w * h);

        for (int i = 0; i < area; i++) {

            int x = random.nextInt(w);

            int y = random.nextInt(h);

            int rgb = getRandomIntColor();

            image.setRGB(x, y, rgb);

        }

 

        shear(g2, w, h, c);

 

        g2.setColor(getRandColor(100, 160));

        int fontSize = h-35;

        Font font = new Font("Algerian", Font.ITALIC, fontSize);

        g2.setFont(font);

        char[] chars = code.toCharArray();

        for(int i = 0; i < verifySize; i++){

            AffineTransform affine = new AffineTransform();

            affine.setToRotation(Math.PI / 4 * rand.nextDouble() * (rand.nextBoolean() ? 1 : -1), (w / verifySize) * i + fontSize/2, h/2);

            g2.setTransform(affine);

            g2.drawChars(chars, i, 1, ((w-10) / verifySize) * i + 5, h/2 + fontSize/2 - 10);

        }

 

        g2.dispose();

        return image;

    }

 

    /**

     * 設(shè)置線條的顏色

     * @param fc

     * @param bc

     * @return

     */

    private static Color getRandColor(int fc, int bc) {

        if (fc > 255)

            fc = 255;

        if (bc > 255)

            bc = 255;

        int r = fc + random.nextInt(bc - fc);

        int g = fc + random.nextInt(bc - fc);

        int b = fc + random.nextInt(bc - fc);

        return new Color(r, g, b);

    }

 

    /**

     * 設(shè)置噪點(diǎn)的顏色

     * @return

     */

    public static int getRandomIntColor() {

        int[] rgb = getRandomRgb();

        int color = 0;

        for (int c : rgb) {

            color = color << 8;

            color = color | c;

        }

        return color;

    }

 

    private static int[] getRandomRgb() {

        int[] rgb = new int[3];

        for (int i = 0; i < 3; i++) {

            rgb[i] = random.nextInt(255);

        }

        return rgb;

    }

 

    /**

     * 使圖片扭曲

     * @param g

     * @param w1

     * @param h1

     * @param color

     */

    private static void shear(Graphics g, int w1, int h1, Color color) {

        shearX(g, w1, h1, color);

        shearY(g, w1, h1, color);

    }

    private static void shearX(Graphics g, int w1, int h1, Color color) {

        int period = random.nextInt(2);

        boolean borderGap = true;

        int frames = 1;

        int phase = random.nextInt(2);

 

        for (int i = 0; i < h1; i++) {

            double d = (double) (period >> 1)

                    * Math.sin((double) i / (double) period

                    + (6.2831853071795862D * (double) phase)

                    / (double) frames);

            g.copyArea(0, i, w1, 1, (int) d, 0);

            if (borderGap) {

                g.setColor(color);

                g.drawLine((int) d, i, 0, i);

                g.drawLine((int) d + w1, i, w1, i);

            }

        }

    }

    private static void shearY(Graphics g, int w1, int h1, Color color) {

        int period = random.nextInt(40) + 10;

        boolean borderGap = true;

        int frames = 20;

        int phase = 7;

        for (int i = 0; i < w1; i++) {

            double d = (double) (period >> 1)

                    * Math.sin((double) i / (double) period

                    + (6.2831853071795862D * (double) phase)

                    / (double) frames);

            g.copyArea(i, 0, 1, h1, 0, (int) d);

            if (borderGap) {

                g.setColor(color);

                g.drawLine(i, (int) d, i, 0);

                g.drawLine(i, (int) d + h1, i, h1);

            }

        }

    }

}

通過以上方法生成如下圖形驗(yàn)證碼 

1.2使用kaptcha驗(yàn)證碼組件

Kaptcha是一個(gè)基于SimpleCaptcha的驗(yàn)證碼開源項(xiàng)目。

KAPTCHA 參數(shù)詳解 

pom.xml中配置依賴

配置驗(yàn)證碼Kaptcha相關(guān)設(shè)置

import java.util.Properties;

import org.springframework.context.annotation.Bean;

import org.springframework.stereotype.Component;

import com.google.code.kaptcha.impl.DefaultKaptcha;

import com.google.code.kaptcha.util.Config;

 

@Component

public class KaptchaConfig {

    @Bean

    public DefaultKaptcha getDefaultKaptcha(){

        com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();

        Properties properties = new Properties();

        properties.setProperty("kaptcha.border", "yes");

        properties.setProperty("kaptcha.border.color", "105,179,90");

        properties.setProperty("kaptcha.image.width", "200");

        properties.setProperty("kaptcha.image.height", "90");

        properties.setProperty("kaptcha.session.key", "code");

        properties.setProperty("kaptcha.textproducer.font.color", "black");

        properties.setProperty("kaptcha.textproducer.font.size", "70");

        properties.setProperty("kaptcha.textproducer.char.length", "4");

        properties.setProperty("kaptcha.textproducer.font.names", "宋體,楷體,微軟雅黑");

        Config config = new Config(properties);

        defaultKaptcha.setConfig(config);

        return defaultKaptcha;

    }

}

然后在啟動(dòng)類Application中加載配置

@Configuration

@EnableAutoConfiguration

@ComponentScan(basePackages = { "com.lll" })

@SpringBootApplication

@EnableAspectJAutoProxy

@EnableAsync

@EnableScheduling

@ImportResource(locations={"classpath:mykaptcha.xml"})

public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) throws Exception {

        SpringApplication.run(Application.class, args);

    }

}

實(shí)現(xiàn)類

package com.example.demo.util;

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;

import javax.imageio.ImageIO;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import com.google.code.kaptcha.impl.DefaultKaptcha;

 

public class KaptchaTest {

    @Autowired

    DefaultKaptcha defaultKaptcha;

 

    @RequestMapping("/defaultKaptcha")

    public void defaultKaptcha(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse) throws Exception{

            byte[] captchaChallengeAsJpeg = null;  

             ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();  

             try {  

             //生產(chǎn)驗(yàn)證碼字符串并保存到session中

             String createText = defaultKaptcha.createText();

             httpServletRequest.getSession().setAttribute("vrifyCode", createText);

             //使用生產(chǎn)的驗(yàn)證碼字符串返回一個(gè)BufferedImage對(duì)象并轉(zhuǎn)為byte寫入到byte數(shù)組中

             BufferedImage challenge = defaultKaptcha.createImage(createText);

             ImageIO.write(challenge, "jpg", jpegOutputStream);

             } catch (IllegalArgumentException e) {  

                 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);  

                 return;  

             } 

 

             //定義response輸出類型為image/jpeg類型,使用response輸出流輸出圖片的byte數(shù)組

             captchaChallengeAsJpeg = jpegOutputStream.toByteArray();  

             httpServletResponse.setHeader("Cache-Control", "no-store");  

             httpServletResponse.setHeader("Pragma", "no-cache");  

             httpServletResponse.setDateHeader("Expires", 0);  

             httpServletResponse.setContentType("image/jpeg");  

             ServletOutputStream responseOutputStream =  

                     httpServletResponse.getOutputStream();  

             responseOutputStream.write(captchaChallengeAsJpeg);  

             responseOutputStream.flush();  

             responseOutputStream.close();  

    }

}

使用Kaptcha生成圖形驗(yàn)證碼 

二、針對(duì)ip進(jìn)行訪問次數(shù)限制

2.1獲取真實(shí)的IP地址

    public String getRemoteIp(HttpServletRequest request) {

        String remoteIp = "";

        remoteIp = request.getHeader("x-forwarded-for");

        if (remoteIp == null || remoteIp.isEmpty() || "unknown".equalsIgnoreCase(remoteIp)) {

            remoteIp = request.getHeader("X-Real-IP");

        }

        if (remoteIp == null || remoteIp.isEmpty() || "unknown".equalsIgnoreCase(remoteIp)) {

            remoteIp = request.getHeader("Proxy-Client-IP");

        }

        if (remoteIp == null || remoteIp.isEmpty() || "unknown".equalsIgnoreCase(remoteIp)) {

            remoteIp = request.getHeader("WL-Proxy-Client-IP");

        }

        if (remoteIp == null || remoteIp.isEmpty() || "unknown".equalsIgnoreCase(remoteIp)) {

            remoteIp = request.getHeader("HTTP_CLIENT_IP");

        }

        if (remoteIp == null || remoteIp.isEmpty() || "unknown".equalsIgnoreCase(remoteIp)) {

            remoteIp = request.getHeader("HTTP_X_FORWARDED_FOR");

        }

        if (remoteIp == null || remoteIp.isEmpty() || "unknown".equalsIgnoreCase(remoteIp)) {

            remoteIp = request.getRemoteAddr();

        }

        if (remoteIp == null || remoteIp.isEmpty() || "unknown".equalsIgnoreCase(remoteIp)) {

            remoteIp = request.getRemoteHost();

        }

        return remoteIp;

    }

對(duì)于通過多個(gè)代理的情況,我們會(huì)得到例如:112.28.176.62,10.102.201.130 這樣的結(jié)果,那么第一個(gè)IP為客戶端真實(shí)IP,多個(gè)IP按照’,’分割 ,以上代碼可以獲取到用戶沒有進(jìn)行偽造的請(qǐng)求。

 

如果運(yùn)行這段代碼后仍然獲取的是Nginx代理地址的話,說明需要對(duì)Nginx進(jìn)行配置。 

在代理的每個(gè)location處加上以下配置:

 

proxy_set_header Host $http_host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header X-Forwarded-Proto $scheme;

Nginx反向代理后,Servlet應(yīng)用通過request.getRemoteAddr()取到的IP是Nginx的IP地址,并非客戶端真實(shí)IP,通過request.getRequestURL()獲取的域名、協(xié)議、端口都是Nginx訪問Web應(yīng)用時(shí)的域名、協(xié)議、端口,而非客戶端瀏覽器地址欄上的真實(shí)域名、協(xié)議、端口。 

Nginx的反向代理實(shí)際上是客戶端和真實(shí)的應(yīng)用服務(wù)器之間的一個(gè)橋梁,客戶端(一般是瀏覽器)訪問Nginx服務(wù)器,Nginx再去訪問Web應(yīng)用服務(wù)器。對(duì)于Web應(yīng)用來說,這次HTTP請(qǐng)求的客戶端是Nginx而非真實(shí)的客戶端瀏覽器,如果不做特殊處理的話,Web應(yīng)用會(huì)把Nginx當(dāng)作請(qǐng)求的客戶端,獲取到的客戶端信息就是Nginx的一些信息。 

解決這個(gè)問題要從兩個(gè)方面來解決: 

1. 由于Nginx是代理服務(wù)器,所有客戶端請(qǐng)求都從Nginx轉(zhuǎn)發(fā)到Tomcat,如果Nginx不把客戶端真實(shí)IP、域名、協(xié)議、端口告訴Tomcat,那么Tomcat應(yīng)用是永遠(yuǎn)不會(huì)知道這些信息的,所以需要Nginx配置一些HTTP Header來將這些信息告訴被代理的Tomcat; 

2. Tomcat這一端,不能再傻乎乎的獲取直接和它連接的客戶端(也就是Nginx)的信息,而是要從Nginx傳遞過來的HTTP Header中獲取客戶端信息。 

原鏈接:https://blog.csdn.net/it_0101/article/details/78390700

 

2.2設(shè)置訪問限制

根據(jù)IP和手機(jī)號(hào)碼針對(duì)該接口請(qǐng)求: 

1、同一號(hào)碼在同一天內(nèi)只能發(fā)送不超過5條驗(yàn)證碼; 

2、同一IP在1分鐘內(nèi)出現(xiàn)3次以上 

3、同一IP在30分鐘內(nèi)超過5次 

4、同一IP在24*60分鐘內(nèi)出現(xiàn)10次以上 

5、同一IP在48*60內(nèi)出現(xiàn)20以上 

將以上IP列為黑名單;黑名單可以手動(dòng)刪除;

經(jīng)過這次的安全訪問限制,短信盜刷的請(qǐng)求在圖形驗(yàn)證碼校驗(yàn)時(shí)就被拒絕了,目前還沒有黑名單生成。產(chǎn)品方面也針對(duì)其他類型的圖形驗(yàn)證碼方案進(jìn)行評(píng)審,下次說不定就是人臉識(shí)別了。。。

版權(quán)聲明:本文為CSDN博主「Mia_li」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。

原文鏈接:https://blog.csdn.net/sinat_23324343/article/details/80854034

三、新昕科技短信防盜刷方案

  提高圖片的復(fù)雜度及增加簡(jiǎn)單的Ip規(guī)則,包括滑動(dòng)驗(yàn)證其實(shí)都容易被黑客破解, 只有新昕科技的無(wú)感短信防轟炸,在真正無(wú)感的前提下,徹底解決短信轟炸問題。

   新昕科技 www.newxtc.com ,創(chuàng)始團(tuán)隊(duì)來自百度旗下去哪兒、易寶支付、聯(lián)動(dòng)優(yōu)勢(shì)、高陽(yáng)捷訊(19pay)等支付及航旅知名企業(yè),歷時(shí)3年時(shí)間,在價(jià)值百萬(wàn)的風(fēng)控引擎基礎(chǔ)上 ,訓(xùn)練出“防短信轟炸”智能模型,徹底解決“安全”與“用戶體驗(yàn)”的矛盾,產(chǎn)品經(jīng)理只需專注用戶體驗(yàn),無(wú)需為安全讓步。

  1)無(wú)感:去類12306、對(duì)缺口拼圖、拖動(dòng)等所謂人機(jī)驗(yàn)證有感方式。 

化繁為簡(jiǎn),簡(jiǎn)單到只需要輸入手機(jī)號(hào),還產(chǎn)品本來面目

  2)保障:攻防對(duì)抗大數(shù)據(jù)訓(xùn)練的 AI模型,去前端交互驗(yàn)證方式,后端防御確保短信安全。

     比如,同一個(gè)IP ,即使有1萬(wàn)個(gè)正常用戶同時(shí)共同使用,可以確保放行,但常規(guī)的防控大多數(shù)被誤攔。

      反之,攻擊者控制1萬(wàn)臺(tái)主機(jī),1萬(wàn)個(gè)不同IP、手機(jī),也保證攔截,但常規(guī)的防控對(duì)此無(wú)能為力。

    

    如何做到的, 基于三層AI防御體系,

     報(bào)文對(duì)抗層 在最外層應(yīng)用加解密及混淆技術(shù),對(duì)抗普通的攻擊,

     蜂窩防護(hù)層 由時(shí)空主體組成蜂窩,確保被攻擊后蜂窩之間互不影響,縮小受影響的范圍,

     安全氣囊   在確保老用戶不受影響下, 根據(jù)攻擊規(guī)模自動(dòng)啟停并進(jìn)行動(dòng)態(tài)控制。

 

  3)高效:價(jià)值百萬(wàn)的風(fēng)控引擎濃縮的10M 短信防火墻安裝包,本地部署運(yùn)行,毫秒級(jí)響應(yīng)。

  避免“云模式”的網(wǎng)絡(luò)延時(shí)問題,導(dǎo)致滑動(dòng)條出不來等情

 

關(guān)鍵技術(shù)說明:

    懸浮式指標(biāo)引擎加載AI模型,懸浮于磁盤超高速運(yùn)行,隨輸入的業(yè)務(wù)數(shù)據(jù)生成統(tǒng)計(jì)指標(biāo),提供給決策引擎做進(jìn)一步分析處理,

 決策引擎加載“短信防轟炸”AI模型和指標(biāo)后,和輸入的業(yè)務(wù)數(shù)據(jù)流做邏輯判斷后,輸出風(fēng)險(xiǎn)結(jié)果,響應(yīng)速度達(dá)到恐怖的1毫秒。

 

總結(jié):隨著互聯(lián)網(wǎng)技術(shù)的不斷發(fā)展,我們每日都離不開與互聯(lián)網(wǎng)的交互。短信驗(yàn)證碼作為互聯(lián)網(wǎng)交互中的重要環(huán)節(jié),保衛(wèi)著網(wǎng)站的安全以及我們的信息安全。用戶體驗(yàn)差、毫無(wú)安全性可言的圖片驗(yàn)證碼將退出歷史舞臺(tái),未來將會(huì)是安全與體驗(yàn)雙重保障的驗(yàn)證碼的時(shí)代。