验证码实现

验证码

===================验证码================
 /**
     * 生成图片验证码
     *
     * @param type           验证码类型,参见本类的静态属性
     * @param length         验证码字符长度,要求大于0的整数
     * @param excludeString  需排除的特殊字符
     * @param width          图片宽度(注意此宽度若过小,容易造成验证码文本显示不全,如4个字符的文本可使用85到90的宽度)
     * @param height         图片高度
     * @param interLine      图片中干扰线的条数
     * @param randomLocation 每个字符的高低位置是否随机
     * @param backColor      图片颜色,若为null则表示采用随机颜色
     * @param foreColor      字体颜色,若为null则表示采用随机颜色
     * @param lineColor      干扰线颜色,若为null则表示采用随机颜色
     * @return 图片缓存对象
     */
    public static BufferedImage generateImageCode(int type, int length,
                                                  String excludeString, int width, int height, int interLine,
                                                  boolean randomLocation, Color backColor, Color foreColor,
                                                  Color lineColor) {
        String textCode = generateTextCode(type, length, excludeString);
        return generateImageCode(textCode, width, height, interLine,
                randomLocation, backColor, foreColor, lineColor);
    }
	
	
	
    /**
     * 已有验证码,生成验证码图片
     *
     * @param textCode       文本验证码
     * @param width          图片宽度(注意此宽度若过小,容易造成验证码文本显示不全,如4个字符的文本可使用85到90的宽度)
     * @param height         图片高度
     * @param interLine      图片中干扰线的条数
     * @param randomLocation 每个字符的高低位置是否随机
     * @param backColor      图片颜色,若为null则表示采用随机颜色
     * @param foreColor      字体颜色,若为null则表示采用随机颜色
     * @param lineColor      干扰线颜色,若为null则表示采用随机颜色
     * @return 图片缓存对象
		 */
	import java.awt.Color;
	import java.awt.Font;
	import java.awt.Graphics;
	import java.awt.image.BufferedImage;
    public static BufferedImage generateImageCode(String textCode, int width,
                                                  int height, int interLine, boolean randomLocation, Color backColor,
                                                  Color foreColor, Color lineColor) {
        // 创建内存图像
        BufferedImage bufferedImage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        // 获取图形上下文
        Graphics graphics = bufferedImage.getGraphics();
        // 画背景图
        graphics.setColor(null == backColor ? generateRandomColor() : backColor);
        graphics.fillRect(0, 0, width, height);
        // 画干扰线
        Random random = new Random();
        if (interLine > 0) {
            int x = 0, y = 0, x1 = width, y1 = 0;
            for (int i = 0; i < interLine; i++) {
                graphics.setColor(null == lineColor ? generateRandomColor()
                        : lineColor);
                y = random.nextInt(height);
                y1 = random.nextInt(height);
                graphics.drawLine(x, y, x1, y1);
            }
        }
        // 字体大小为图片高度的80%
        int fsize = (int) (height * 0.8);
        int fx = height - fsize;
        int fy = fsize;
        // 设定字体
        graphics.setFont(new Font("Default", Font.PLAIN, fsize));
        // 写验证码字符
        for (int i = 0; i < textCode.length(); i++) {
            fy = randomLocation ? (int) ((Math.random() * 0.3 + 0.6) * height)
                    : fy;
            graphics.setColor(null == foreColor ? generateRandomColor()
                    : foreColor);
            // 将验证码字符显示到图象中
            graphics.drawString(textCode.charAt(i) + "", fx, fy);
            fx += fsize * 0.9;
        }
        graphics.dispose();
        return bufferedImage;
    }
	
	===============java.rmi.Naming 远程访问对象方法=========https://www.cnblogs.com/sanhuan/p/4806480.html
	RMI:远程方法调用(Remote Method Invocation)。能够让在某个java虚拟机上的对象像调用本地对象一样调用另一个java 虚拟机中的对象上的方法。
	
	//调用远程接口
	SmsRmiService rhello = (SmsRmiService) Naming.lookup(url);
	
	==========================tokenService=========================================================================
	import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.hu.libraryManagement.VO.UserVO;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;

/***
 * token 下发
 */
@Service
public class TokenService {

  public String getToken(UserVO userVo) {
  	  Date start = new Date();
  	  long currentTime = System.currentTimeMillis() + 60* 60 * 1000;//一小时有效时间
      Date end = new Date(currentTime);
   	 //设置头信息
   	 HashMap<String, Object> header = new HashMap<>(2);
   	 header.put("typ", "JWT");
   	 header.put("alg", "HS256");
     String token = "";

   	 /**
     * withHeader http头部信息
     * withAudience 用户登录(user)信息   在Token拦截器(AuthenticationInterceptor)中通过	JWT.decode(token).getAudience().get(0)提取
     * withIssuedAt(start) withExpiresAt(end) 有效时间
     * sign Algorithm.私钥及加密算法
     * */
    token = JWT.create().withHeader(header).withAudience(userVo.getUserAccount()).withIssuedAt(start).withExpiresAt(end)
            .sign(Algorithm.HMAC256(userVo.getUserPassword()));  // Algorithm. 私钥及加密算法
   	 return token;
	}
}

关注不迷路在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值