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、接口測試一
@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驗(yàn)證碼
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);
}驗(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);
}
// 國際化使用
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è)置通用的請求屬性
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java如何生成4位、6位隨機(jī)數(shù)短信驗(yàn)證碼(高效實(shí)現(xiàn))
- Java實(shí)現(xiàn)滑動(dòng)驗(yàn)證碼生成(后端工具類)
- Java實(shí)現(xiàn)滑動(dòng)驗(yàn)證碼(前端部分)
- 利用Java工具類Hutool實(shí)現(xiàn)驗(yàn)證碼校驗(yàn)功能
- Java實(shí)現(xiàn)動(dòng)態(tài)驗(yàn)證碼生成
- java實(shí)現(xiàn)圖片驗(yàn)證碼
- Java實(shí)現(xiàn)發(fā)送短信驗(yàn)證碼+redis限制發(fā)送的次數(shù)功能
相關(guān)文章
java語言基礎(chǔ)之標(biāo)識符和命名規(guī)則詳解
這篇文章主要給大家介紹了關(guān)于java語言基礎(chǔ)之標(biāo)識符和命名規(guī)則的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
java使用common-httpclient包實(shí)現(xiàn)post請求方法示例
這篇文章主要給大家介紹了關(guān)于java使用common-httpclient包實(shí)現(xiàn)post請求的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08
利用Spring Social輕松搞定微信授權(quán)登錄的方法示例
這篇文章主要介紹了利用Spring Social輕松搞定微信授權(quán)登錄的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
詳解Java synchronized關(guān)鍵字的用法
在多線程編程中常常使用鎖機(jī)制來確保同一時(shí)刻只有一個(gè)線程能夠修改共享內(nèi)存,在Java中一般是使用synchronized作為鎖機(jī)制,下面就讓我們來學(xué)習(xí)一下如何使用synchronized實(shí)現(xiàn)線程安全吧2023-08-08
springboot集成websocket的四種方式小結(jié)
本文主要介紹了springboot集成websocket的四種方式小結(jié),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12

