提交代码

This commit is contained in:
synchronized 2018-05-14 11:39:09 +08:00
parent 668d881479
commit 2c47bde8b2
7 changed files with 203 additions and 1 deletions

16
.idea/compiler.xml Normal file
View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="EasyCaptcha" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="EasyCaptcha" target="1.5" />
</bytecodeTargetLevel>
</component>
</project>

View File

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: javax.servlet:javax.servlet-api:3.1.0">
<CLASSES>
<root url="jar://$USER_HOME$/.m2/repository/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$USER_HOME$/.m2/repository/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$USER_HOME$/.m2/repository/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0-sources.jar!/" />
</SOURCES>
</library>
</component>

13
.idea/misc.xml Normal file
View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/classes" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/EasyCaptcha.iml" filepath="$PROJECT_DIR$/EasyCaptcha.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

14
EasyCaptcha.iml Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="Maven: javax.servlet:javax.servlet-api:3.1.0" level="project" />
</component>
</module>

134
README.md
View File

@ -1,2 +1,134 @@
# EasyCaptcha
JavaWeb图片验证码支持gif验证码可用于RESTful风格的项目
JavaWeb图形验证码支持gif验证码可用于基于的session的web项目和前后端分离的项目。
## 效果展示
![验证码](http://115.159.40.243:8080/EasyWeb/image/captcha?codeKey=a)
## 导入项目
### gradle方式的引入
需要先在project的build.gradle中添加
```
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
```
在项目的build.gradle中添加
```
dependencies {
compile 'com.github.whvcse:JwtPermission:1.1.0'
}
```
### maven方式引入
```xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>JwtPermission</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
```
### jar包下载
[EasyCaptcha-1.1.0.jar](https://github.com/whvcse/EasyCaptcha/releases)
## 使用方法
### 快速使用
1.在web.xml里面加入如下配置
```xml
<!-- 图形验证码 -->
<servlet>
<servlet-name>CaptchaServlet</servlet-name>
<servlet-class>com.wf.captcha.servlet.CaptchaServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CaptchaServlet</servlet-name>
<url-pattern>/image/captcha</url-pattern>
</servlet-mapping>
```
2.前端代码
```html
<img src="/image/captcha" />
```
### 在*SpringMVC*中使用
如果你不想使用项目提供的servlet可以使用controller的形式实现方法也很简单代码如下
```java
@RequestMapping("/images/captcha")
public void captcha(HttpServletRequest request, HttpServletResponse response) {
CaptchaUtil captcha = new CaptchaUtil();
try {
captcha.out(request, response);
} catch (IOException e) {
e.printStackTrace();
}
}
```
前端代码:
```html
<img src="/image/captcha" />
```
### 前后端分离中使用
验证码一般都是保存在session中的但是在前后端分离的项目中不推荐使用session存储可使用如下方式
```java
@RequestMapping("/images/captcha")
public void captcha(String key, HttpServletRequest request, HttpServletResponse response) {
CaptchaUtil captcha = new CaptchaUtil();
try {
captcha.out(key, request, response);
} catch (IOException e) {
e.printStackTrace();
}
}
```
与前面的使用区别就在于多了一个key使用的时候需要前端生成一个随机key传递过来服务器是已这个key为名字存储在servletContext中的取的时候需要根据这个key取值。
前后端分离也同样可以使用框架自带的servlet使用方式如下
```html
<img src="/image/captcha?key=xxx" />
<!-- 此处的key应该有js随机生成并且js在验证的时候也需要传递这个key -->
```
### 判断验证码是否正确
基于session存储的判断
```java
CaptchaUtil captcha = new CaptchaUtil();
if (captcha == null || !captcha.ver(code, request)) {
return JsonResult.error("验证码不正确");
}
```
前后端分离方式的判断:
```java
CaptchaUtil captcha = new CaptchaUtil();
if (captcha == null || !captcha.ver(key, code, request)) {
return JsonResult.error("验证码不正确");
}
//此处的key便是生成的时候传递的key
```
### 参数设置
#### 设置宽高和位数
```java
//三个参数分别是宽、高、位数
CaptchaUtil captcha = new CaptchaUtil(130, 38, 5);
```
#### 修改存储时候的key
```java
CaptchaUtil captcha = new CaptchaUtil();
captcha.setCodeName("captcha");
```
默认存在session中是以captcha为key存储的存储在servletContext中是以captcha-xxx为key存储的xxx是生成的时候前端传递的key。