Hutool homonym “confused”, meaning the pursuit of “everything is confused view, no loss, no gain” realm.

Hutool is a Java toolkit, and only a toolkit, that helps us simplify every line of code, eliminate every method, and make the Java language “sweet.” Hutool was originally a compilation of the “util” package in my project. Later, it gradually accumulated and added more non-business related functions. Besides, I learned the essence of other open source projects extensively, and finally formed a rich set of open source tools through my own sorting and modification. (From author’s introduction)

function

Util utility class a Java basic utility class that encapsulates JDK methods such as files, streams, encryption and decryption, transcoding, re, threads, and XML. Util utility classes provide the following components:

  • Hutool – AOP JDK dynamic proxy package, providing aspect support under non-IOC
  • Hutool-bloomfilter Provides Bloomed filtering based on Hash algorithms
  • Hutool – cache cache
  • Hutool-core, including Bean operations, dates, various utils, etc
  • Hutool -cron Scheduled task module, which provides scheduled tasks like Crontab expressions
  • Hutool – Crypto encryption and decryption module
  • Hutool -db JDBC encapsulated data operations based on ActiveRecord
  • Hutool – DFA Multi-keyword lookup based on DFA model
  • Hutool – Extra extension module for third party encapsulation (template engine, mail, etc.)
  • Hutool-http HTTP client encapsulation based on HttpUrlConnection
  • Hutool-log Automatically identifies the log facade of the log implementation
  • Hutool-script Scripts perform encapsulation, such as Javascript
  • Hutool-setting features more powerful setting profile and Properties package
  • Hutool-system System parameters call encapsulation (JVM information, etc.)
  • Hutool – json json
  • Hutool – CaptCHA image verification code implementation

A simple test

I replaced some of the code in Halo with Hutool these days, and I have to say that it was very pleasant to use it. Here are some of the Hutool classes I used.

SecureUtil (Encryption and decryption tool)

It is mainly used when logging in and changing the password. Because the password in the database is encrypted by MD5, it needs to be encrypted before querying the database. Using Hutool, you only need to call the MD5 method in SecureUtil.

user = userService.userLoginByName(loginName,SecureUtil.md5(loginPwd));
Copy the code

HtmlUtil (HTML utility class)

This utility class is a bit more powerful, but the one I use most in Halo is htmlutil.encode, which converts some characters into safe characters that prevent XSS injection and SQL injection, such as comment submission below.

comment.setCommentAuthor(HtmlUtil.encode(comment.getCommentAuthor()));
Copy the code

This is to prevent some bad guys deliberately write some executable JS code, and then submit comments, in the background panel will execute this code, relatively dangerous, using the encode method can be converted to tags, so that after conversion, JS code will not execute.

In addition, HtmlUtil provides the following methods for those interested to try out.

  • Htmlutil. restoreEscaped Restores escaped HTML special characters
  • Htmlutil. encode Escapes HTML characters in text as safe characters
  • Htmlutil. cleanHtmlTag Clears all HTML tags
  • Htmlutil. removeHtmlTag Clears the specified HTML tag and the content surrounded by the tag
  • Htmlutil. unwrapHtmlTag Clears specified HTML tags, excluding content
  • Htmlutil. removeHtmlAttr Removes attributes from HTML tags
  • HtmlUtil removeAllHtmlAttr remove all attributes of the specified tag
  • Htmlutil. filter Filters HTML text to prevent XSS attacks

CronUtil (scheduled task)

CronUtil doesn’t need a framework like Quartz to do scheduled tasks. CronUtil also doesn’t need any dependencies, just create a configuration file under Resources and enable scheduled tasks when the application starts. Such as Halo’s scheduled backup feature (backups every day at 1am).

cron.setting:

cc.ryanc.halo.web.controller.admin.BackupController.backupResources = 0 0 1 * * ? cc.ryanc.halo.web.controller.admin.BackupController.backupDatabase = 0 0 1 * * ? cc.ryanc.halo.web.controller.admin.BackupController.backupPosts = 0 0 1 * * ? @Override public void onApplicationEvent(ContextRefreshedEvent event){ this.loadActiveTheme(); this.loadOptions(); this.loadFiles(); this.loadThemes(); // Start scheduled task cronutil.start (); Log.info (" Scheduled task started successfully!" ); }Copy the code

Please refer to the document hutool.mydoc. IO /? t=255673

Ok, introduce these three tool classes, interested can go to try other tools, quite complete, this should be I have used the best use of a tool class library, worth a try.

Official website: www.hutool.cn/