代码复用和被重新定义(抽象类的使用——增量的形式适应变化)
需求:前端登录页面需要一个验证码,后端生成并返回
这是一个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);
}
}