java生成在线验证码

 更新时间:2023年10月02日 09:27:50   投稿:wdc  
这篇文章主要介绍了java生成在线验证码,需要的朋友可以参考下

1、生成验证码

1、maven包

<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>

2、接口测试一

@GetMapping("/captcha")
public Result captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
CaptchaUtil.out(request, response);
}

3、接口测试二

@GetMapping("/captcha")
public Result captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
//第二种
// // 设置位数
CaptchaUtil.out(5, request, response);
// // 设置宽、高、位数
CaptchaUtil.out(130, 48, 5, request, response);
//
// // 使用gif验证码
GifCaptcha gifCaptcha = new GifCaptcha(130,48,4);
CaptchaUtil.out(gifCaptcha, request, response);
}

3、接口测试三(结合redis)

@GetMapping("/captcha")
public Result captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
String verCode = specCaptcha.text().toLowerCase();
String key = UUID.randomUUID().toString();
redisUtil.opsForValue().set(key, verCode, 30, TimeUnit.MINUTES);
// 将key和base64返回给前端
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("key", key);
hashMap.put("image", specCaptcha.toBase64());
return Result.ok(hashMap);
}
@Override
public Result makeCode() {
SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
specCaptcha.setCharType(Captcha.TYPE_ONLY_LOWER);
String verCode = specCaptcha.text().toUpperCase();
String key = UUID.randomUUID().toString();
// System.out.println(key);
// System.out.println(verCode);
redisUtil.opsForValue().set(key, verCode, 60*10, TimeUnit.SECONDS);
// 将key和base64返回给前端
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("codeKey", key);
hashMap.put("imageCode", specCaptcha.toBase64());
return Result.ok(hashMap);
}

验证码数据统一转化为大写 

   String verCode = specCaptcha.text().toUpperCase();
@Override
public void makeCodeTwo(String uuid, HttpServletResponse response) throws IOException {
if(StringUtils.isBlank(uuid)){
response.setContentType("application/json;charset=UTF-8");
//设置编码格式
response.setCharacterEncoding("UTF-8");
response.setStatus(401);
response.getWriter().write("uuid不能为空");
}
SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
specCaptcha.setCharType(Captcha.TYPE_ONLY_LOWER);
String verCode = specCaptcha.text().toUpperCase();
redisUtil.opsForValue().set(uuid, verCode, 60*10, TimeUnit.SECONDS);
specCaptcha.out(response.getOutputStream());
}

2、java加载配置信息判断(dev或者pro)

一、配置信息注入容器

@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext context = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.context = applicationContext;
}
// 传入线程中
public static <T> T getBean(String beanName) {
return (T) context.getBean(beanName);
}
// 国际化使用
public static String getMessage(String key) {
return context.getMessage(key, null, Locale.getDefault());
}
/// 获取当前环境
public static String getActiveProfile() {
return context.getEnvironment().getActiveProfiles()[0];
}
}

二、获取当前环境

String activeProfile = SpringContextUtil.getActiveProfile();

3、获取当前ip地址

1、获取本地IP

  String ip= InetAddress.getLocalHost().getHostAddress();

2、获取公网IP

public String getIpv4IP() {
StringBuilder result = new StringBuilder();
BufferedReader in = null;
try {
URL realUrl = new URL("https://www.taobao.com/help/getip.php");
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
// log.error("获取ipv4公网地址异常");
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
String str = result.toString().replace("ipCallback({ip:", "");
String ipStr = str.replace("})", "");
return ipStr.replace('"', ' ');
}

到此这篇关于java生成在线验证码的文章就介绍到这了,更多相关java生成在线验证码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 解决springboot服务启动报错:Unable to start embedded contain

    解决springboot服务启动报错:Unable to start embedded contain

    这篇文章主要介绍了解决springboot服务启动报错:Unable to start embedded contain的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08
  • 深入理解Java设计模式之状态模式

    深入理解Java设计模式之状态模式

    这篇文章主要介绍了JAVA设计模式之职责链模式的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解
    2021-11-11
  • springboot如何获取接口下所有实现类

    springboot如何获取接口下所有实现类

    这篇文章主要介绍了springboot如何获取接口下所有实现类问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • spring基于注解配置实现事务控制操作

    spring基于注解配置实现事务控制操作

    这篇文章主要介绍了spring基于注解配置实现事务控制操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • java并发高的情况下用ThreadLocalRandom来生成随机数

    java并发高的情况下用ThreadLocalRandom来生成随机数

    如果我们想要生成一个随机数,通常会使用Random类。但是在并发情况下Random生成随机数的性能并不是很理想,本文主要介绍了java并发高的情况下用ThreadLocalRandom来生成随机数,感兴趣的可以了解一下
    2022-05-05
  • Spring AOP手写动态代理代码实例

    Spring AOP手写动态代理代码实例

    这篇文章主要介绍了Spring AOP手写动态代理代码实例,AOP我们知道,是在不修改源代码的情况下,为代码添加一些新功能的技术,通过动态代理,可以在不修改原始类代码的前提下,对方法进行拦截和增强,需要的朋友可以参考下
    2024-01-01
  • 手把手写Spring框架

    手把手写Spring框架

    Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架
    2021-08-08
  • Flowable 设置流程变量的四种方式详解

    Flowable 设置流程变量的四种方式详解

    这篇文章主要为大家介绍了Flowable 设置流程变量的四种方式详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • Spring Security图形验证码的实现代码

    Spring Security图形验证码的实现代码

    本文介绍了如何在SpringSecurity自定义认证中添加图形验证码,首先需要在maven中添加相关依赖并创建验证码对象,然后通过Spring的HttpSessionSessionStrategy对象将验证码存储到Session中,感兴趣的朋友跟随小编一起看看吧
    2024-10-10
  • mybatis 查询sql中in条件用法详解(foreach)

    mybatis 查询sql中in条件用法详解(foreach)

    这篇文章主要介绍了mybatis 查询sql中in条件用法详解(foreach),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02

最新评论