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ù) CaptchaUtil.out(5, request, response); // // 設置寬、高、位數(shù) 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); }
驗證碼數(shù)據(jù)統(tǒng)一轉化為大寫
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()); } /// 獲取當前環(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、獲取公網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生成在線驗證碼內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java使用common-httpclient包實現(xiàn)post請求方法示例
這篇文章主要給大家介紹了關于java使用common-httpclient包實現(xiàn)post請求的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-08-08利用Spring Social輕松搞定微信授權登錄的方法示例
這篇文章主要介紹了利用Spring Social輕松搞定微信授權登錄的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12