最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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 {
//第二種
// // 設(shè)置位數(shù)
CaptchaUtil.out(5, request, response);
// // 設(shè)置寬、高、位數(shù)
CaptchaUtil.out(130, 48, 5, request, response);
//
// // 使用gif驗證碼
GifCaptcha gifCaptcha = new GifCaptcha(130,48,4);
CaptchaUtil.out(gifCaptcha, request, response);
}

3、接口測試三(結(jié)合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);
}

驗證碼數(shù)據(jù)統(tǒng)一轉(zhuǎn)化為大寫 

   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");
//設(shè)置編碼格式
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());
}
/// 獲取當前環(huán)境
public static String getActiveProfile() {
return context.getEnvironment().getActiveProfiles()[0];
}
}

二、獲取當前環(huán)境

String activeProfile = SpringContextUtil.getActiveProfile();

3、獲取當前ip地址

1、獲取本地IP

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

2、獲取公網(wǎng)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();
// 設(shè)置通用的請求屬性
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();
// 獲取所有響應(yīng)頭字段
Map<String, List<String>> map = connection.getHeaderFields();
// 定義 BufferedReader輸入流來讀取URL的響應(yīng)
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
// log.error("獲取ipv4公網(wǎng)地址異常");
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('"', ' ');
}

到此這篇關(guān)于java生成在線驗證碼的文章就介紹到這了,更多相關(guān)java生成在線驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解決springboot服務(wù)啟動報錯:Unable?to?start?embedded?contain

    解決springboot服務(wù)啟動報錯:Unable?to?start?embedded?contain

    這篇文章主要介紹了解決springboot服務(wù)啟動報錯:Unable?to?start?embedded?contain的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • 深入理解Java設(shè)計模式之狀態(tài)模式

    深入理解Java設(shè)計模式之狀態(tài)模式

    這篇文章主要介紹了JAVA設(shè)計模式之職責鏈模式的的相關(guān)資料,文中示例代碼非常詳細,供大家參考和學習,感興趣的朋友可以了解
    2021-11-11
  • springboot如何獲取接口下所有實現(xiàn)類

    springboot如何獲取接口下所有實現(xiàn)類

    這篇文章主要介紹了springboot如何獲取接口下所有實現(xiàn)類問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • spring基于注解配置實現(xiàn)事務(wù)控制操作

    spring基于注解配置實現(xiàn)事務(wù)控制操作

    這篇文章主要介紹了spring基于注解配置實現(xiàn)事務(wù)控制操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java并發(fā)高的情況下用ThreadLocalRandom來生成隨機數(shù)

    java并發(fā)高的情況下用ThreadLocalRandom來生成隨機數(shù)

    如果我們想要生成一個隨機數(shù),通常會使用Random類。但是在并發(fā)情況下Random生成隨機數(shù)的性能并不是很理想,本文主要介紹了java并發(fā)高的情況下用ThreadLocalRandom來生成隨機數(shù),感興趣的可以了解一下
    2022-05-05
  • Spring?AOP手寫動態(tài)代理代碼實例

    Spring?AOP手寫動態(tài)代理代碼實例

    這篇文章主要介紹了Spring?AOP手寫動態(tài)代理代碼實例,AOP我們知道,是在不修改源代碼的情況下,為代碼添加一些新功能的技術(shù),通過動態(tài)代理,可以在不修改原始類代碼的前提下,對方法進行攔截和增強,需要的朋友可以參考下
    2024-01-01
  • 手把手寫Spring框架

    手把手寫Spring框架

    Spring是于2003 年興起的一個輕量級的Java 開發(fā)框架,由Rod Johnson創(chuàng)建。簡單來說,Spring是一個分層的JavaSE/EE full-stack(一站式) 輕量級開源框架
    2021-08-08
  • Flowable 設(shè)置流程變量的四種方式詳解

    Flowable 設(shè)置流程變量的四種方式詳解

    這篇文章主要為大家介紹了Flowable 設(shè)置流程變量的四種方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Spring?Security圖形驗證碼的實現(xiàn)代碼

    Spring?Security圖形驗證碼的實現(xiàn)代碼

    本文介紹了如何在SpringSecurity自定義認證中添加圖形驗證碼,首先需要在maven中添加相關(guān)依賴并創(chuàng)建驗證碼對象,然后通過Spring的HttpSessionSessionStrategy對象將驗證碼存儲到Session中,感興趣的朋友跟隨小編一起看看吧
    2024-10-10
  • mybatis 查詢sql中in條件用法詳解(foreach)

    mybatis 查詢sql中in條件用法詳解(foreach)

    這篇文章主要介紹了mybatis 查詢sql中in條件用法詳解(foreach),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02

最新評論

云南省| 马公市| 满洲里市| 株洲市| 中超| 灯塔市| 沙坪坝区| 辽阳县| 东光县| 忻城县| 廊坊市| 曲周县| 廉江市| 四子王旗| 嘉黎县| 邵武市| 商都县| 建宁县| 平湖市| 八宿县| 邹平县| 灵武市| 定结县| 永定县| 涟源市| 西充县| 东丽区| 赞皇县| 健康| 沭阳县| 宁安市| 林州市| 邹城市| 潞西市| 康平县| 紫云| 宣威市| 宝丰县| 喜德县| 丹巴县| 定南县|