亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java生成在線驗(yàn)證碼

 更新時(shí)間:2023年10月02日 09:27:50   投稿:wdc  
這篇文章主要介紹了java生成在線驗(yàn)證碼,需要的朋友可以參考下

1、生成驗(yàn)證碼

1、maven包

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

2、接口測(cè)試一

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

3、接口測(cè)試二

@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驗(yàn)證碼
GifCaptcha gifCaptcha = new GifCaptcha(130,48,4);
CaptchaUtil.out(gifCaptcha, request, response);
}

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

驗(yàn)證碼數(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);
}
// 國(guó)際化使用
public static String getMessage(String key) {
return context.getMessage(key, null, Locale.getDefault());
}
/// 獲取當(dāng)前環(huán)境
public static String getActiveProfile() {
return context.getEnvironment().getActiveProfiles()[0];
}
}

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

String activeProfile = SpringContextUtil.getActiveProfile();

3、獲取當(dāng)前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è)置通用的請(qǐng)求屬性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立實(shí)際的連接
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生成在線驗(yàn)證碼的文章就介紹到這了,更多相關(guān)java生成在線驗(yàn)證碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用idea自動(dòng)生成序列化ID全過程

    使用idea自動(dòng)生成序列化ID全過程

    這篇文章主要介紹了使用idea自動(dòng)生成序列化ID全過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • java實(shí)現(xiàn)簡(jiǎn)單注冊(cè)選擇所在城市

    java實(shí)現(xiàn)簡(jiǎn)單注冊(cè)選擇所在城市

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單注冊(cè)選擇所在城市的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-04-04
  • java語言基礎(chǔ)之標(biāo)識(shí)符和命名規(guī)則詳解

    java語言基礎(chǔ)之標(biāo)識(shí)符和命名規(guī)則詳解

    這篇文章主要給大家介紹了關(guān)于java語言基礎(chǔ)之標(biāo)識(shí)符和命名規(guī)則的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Java正則替換手機(jī)號(hào)代碼實(shí)例

    Java正則替換手機(jī)號(hào)代碼實(shí)例

    本文的主要內(nèi)容是Java語言中正則表達(dá)式替換手機(jī)號(hào)的第4到第7位,實(shí)現(xiàn)方法十分簡(jiǎn)單,同時(shí)涉及了一些正則表達(dá)式的相關(guān)用法,需要的朋友可以參考下。
    2017-09-09
  • java使用common-httpclient包實(shí)現(xiàn)post請(qǐng)求方法示例

    java使用common-httpclient包實(shí)現(xiàn)post請(qǐng)求方法示例

    這篇文章主要給大家介紹了關(guān)于java使用common-httpclient包實(shí)現(xiàn)post請(qǐng)求的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • 利用Spring Social輕松搞定微信授權(quán)登錄的方法示例

    利用Spring Social輕松搞定微信授權(quán)登錄的方法示例

    這篇文章主要介紹了利用Spring Social輕松搞定微信授權(quán)登錄的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-12-12
  • 詳解Java synchronized關(guān)鍵字的用法

    詳解Java synchronized關(guān)鍵字的用法

    在多線程編程中常常使用鎖機(jī)制來確保同一時(shí)刻只有一個(gè)線程能夠修改共享內(nèi)存,在Java中一般是使用synchronized作為鎖機(jī)制,下面就讓我們來學(xué)習(xí)一下如何使用synchronized實(shí)現(xiàn)線程安全吧
    2023-08-08
  • RocketMQ根據(jù)Tag進(jìn)行消息過濾

    RocketMQ根據(jù)Tag進(jìn)行消息過濾

    消費(fèi)者訂閱了某個(gè)主題后,Apache RocketMQ 會(huì)將該主題中的所有消息投遞給消費(fèi)者。若消費(fèi)者只需要關(guān)注部分消息,可通過設(shè)置過濾條件在 Apache RocketMQ 服務(wù)端進(jìn)行過濾,只獲取到需要關(guān)注的消息子集,避免接收到大量無效的消息
    2023-02-02
  • Spring的事件監(jiān)聽機(jī)制示例詳解

    Spring的事件監(jiān)聽機(jī)制示例詳解

    這篇文章主要給大家介紹了關(guān)于Spring的事件監(jiān)聽機(jī)制的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • springboot集成websocket的四種方式小結(jié)

    springboot集成websocket的四種方式小結(jié)

    本文主要介紹了springboot集成websocket的四種方式小結(jié),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12

最新評(píng)論