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的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
spring基于注解配置實現(xiàn)事務(wù)控制操作
這篇文章主要介紹了spring基于注解配置實現(xiàn)事務(wù)控制操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
java并發(fā)高的情況下用ThreadLocalRandom來生成隨機數(shù)
如果我們想要生成一個隨機數(shù),通常會使用Random類。但是在并發(fā)情況下Random生成隨機數(shù)的性能并不是很理想,本文主要介紹了java并發(fā)高的情況下用ThreadLocalRandom來生成隨機數(shù),感興趣的可以了解一下2022-05-05
Spring?Security圖形驗證碼的實現(xiàn)代碼
本文介紹了如何在SpringSecurity自定義認證中添加圖形驗證碼,首先需要在maven中添加相關(guān)依賴并創(chuàng)建驗證碼對象,然后通過Spring的HttpSessionSessionStrategy對象將驗證碼存儲到Session中,感興趣的朋友跟隨小編一起看看吧2024-10-10
mybatis 查詢sql中in條件用法詳解(foreach)
這篇文章主要介紹了mybatis 查詢sql中in條件用法詳解(foreach),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02

