代码复用和被重新定义(抽象类的使用——增量的形式适应变化)

代码复用和被重新定义(抽象类的使用——增量的形式适应变化)

需求:前端登录页面需要一个验证码,后端生成并返回
这是一个spring-cloud项目现在目前有两个模块 llc-security-core(通用配置模块) 、llc-security-demo(业务模块) 。
llc-security-core(通用配置模块)中含有一个验证码生成的一个类:

package com.llcbenwu.security;

public class SecurityProperies  {


    /**
     * 认证成功或者错误返回的数据类型
     */
    public static final LoginType  LOGINTYPE=LoginType.JSON;

    /**
     * 图片验证码宽度
     */
    public  static  final  Integer WIDTH =100;

    /**
     * 图片验证码高度
     */
    public  static  final  Integer HEIGHT =20;

    /**
     * 图片验证码位数
     */
    public  static  final  Integer LENGHT =6;


    public  static  final  String URL="/user,/user/*,/authentication/form";




}

package com.llcbenwu.generator;

import com.llcbenwu.code.ImageCode;
import javax.servlet.http.HttpServletRequest;


public interface ImageCodeService {

    ImageCode createImageCode(HttpServletRequest request);
}

package com.llcbenwu.generator.impGenerator;

import com.llcbenwu.code.ImageCode;
import com.llcbenwu.generator.ImageCodeService;
import com.llcbenwu.security.SecurityProperies;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

public class ImageCodeServiceImpl implements ImageCodeService {


    @Override
    public ImageCode createImageCode(HttpServletRequest request) {
        /**
         * 生成一个随机验证码
         * @param request
         * @return
         */
            int width = SecurityProperies.WIDTH;
            int height = SecurityProperies.HEIGHT;
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

            Graphics g = image.getGraphics();

            Random random = new Random();

            g.setColor(getRandColor(200, 250));
            g.fillRect(0, 0, width, height);
            g.setFont(new Font("Times New Roman", Font.ITALIC, 20));
            g.setColor(getRandColor(160, 200));
            for (int i = 0; i < 155; i++) {
                int x = random.nextInt(width);
                int y = random.nextInt(height);
                int xl = random.nextInt(12);
                int yl = random.nextInt(12);
                g.drawLine(x, y, x + xl, y + yl);
            }

            String sRand = "";
            for (int i = 0; i < SecurityProperies.LENGHT; i++) {
                String rand = String.valueOf(random.nextInt(10));
                sRand += rand;
                g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
                g.drawString(rand, 13 * i + 6, 16);
            }

            g.dispose();

            return new ImageCode(image, sRand, 60);
        }

    /**
     * 生成随机背景条纹
     *
     * @param fc
     * @param bc
     * @return
     */
    public static Color getRandColor(int fc, int bc) {
        Random random = new Random();
        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);
    }
}

llc-security-core(通用配置模块)的配置(重点在于@ConditionalOnMissingBean注解):

package com.llcbenwu.config;



import com.llcbenwu.generator.ImageCodeService;
import com.llcbenwu.generator.impGenerator.ImageCodeServiceImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;





@Configuration
public class WebConfig {
    /**
     * 这里是一个生成验证码的一个方法
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(name = "imageValidateCodeGenerator")  //如果有人重新写了一个叫imageValidateCodeGenerator那么就会覆盖该方法
    public ImageCodeService imageValidateCodeGenerator() {
        return new ImageCodeServiceImpl();
    }
}

当llc-security-demo(业务模块)不想重新定义一个验证码
那么
当llc-security-demo(业务模块)想重新定义一个验证码(这里只是简单修改了一下验证码的位数)

package com.llcbenwu.service.imp;

import com.llcbenwu.code.ImageCode;
import com.llcbenwu.generator.ImageCodeService;
import com.llcbenwu.security.SecurityProperies;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * @author lilinchun
 * @date 2021/10/27 0027 15:49
 */
@Component("imageValidateCodeGenerator") //加上这个就会覆盖配置上的那个定义方法
public class DemoImageCodeServiceImpl implements ImageCodeService {


    @Override
    public ImageCode createImageCode(HttpServletRequest request) {
        /**
         * 生成一个随机验证码
         * @param request
         * @return
         */
        int width = SecurityProperies.WIDTH;
        int height = SecurityProperies.HEIGHT;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        Graphics g = image.getGraphics();

        Random random = new Random();

        g.setColor(getRandColor(200, 250));
        g.fillRect(0, 0, width, height);
        g.setFont(new Font("Times New Roman", Font.ITALIC, 20));
        g.setColor(getRandColor(160, 200));
        for (int i = 0; i < 155; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            g.drawLine(x, y, x + xl, y + yl);
        }

        String sRand = "";
        //验证码的位数
        for (int i = 0; i < 4; i++) {
            String rand = String.valueOf(random.nextInt(10));
            sRand += rand;
            g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
            g.drawString(rand, 13 * i + 6, 16);
        }

        g.dispose();

        return new ImageCode(image, sRand, 60);
    }

    /**
     * 生成随机背景条纹
     *
     * @param fc
     * @param bc
     * @return
     */
    public static Color getRandColor(int fc, int bc) {
        Random random = new Random();
        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);
    }
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱上编程2705

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值