Example code of spring security picture verification code function
Verification code logic
Verification codes have been made in the project before. There are many codes for generating verification codes on the Internet, and some third-party jar packages can also generate beautiful verification codes. The logic of the verification code is very simple, that is, put an image tag on the login page, and Src points to a controller. The controller returns the generated picture to the page as an output stream. When generating the picture, put the text on the picture in the session. When logging in, bring the input verification code and take it out of the session. Compare the two. The teacher talked about using spring security integration verification code. The general idea is the same as I said, but it is more standardized and general.
Spring security is a series of filter chains, so here the verification code is also declared as a filter, added before the login filter of the filter chain, and then customize an exception class to respond to the error message of the verification code.
Code structure:
The verification code is placed in the core project and configured in the browser project.
Main code:
1,ImageCode:
The first is the Imagecode class, which encapsulates the verification code image, text and expiration time
Verifycode: tool class for generating verification code, here http://www.cnblogs.com/lihaoyang/p/7131512.html Of course, you can also use third-party jar packages. It doesn't matter.
visit http://localhost:8080/demo -login. html:
The complete code is placed in GitHub: https://github.com/lhy1234/spring-security