Skip to content

Commit f1921cf

Browse files
committed
新增Maven项目模板
1 parent 26ee2af commit f1921cf

137 files changed

Lines changed: 6243 additions & 19 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>com.jun.plugin.account</groupId>
6+
<artifactId>maven_account_parent</artifactId>
7+
<version>1.0.0-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>maven_account_captcha</artifactId>
11+
<name>Account Captcha</name>
12+
13+
<properties>
14+
<kaptcha.version>2.3</kaptcha.version>
15+
</properties>
16+
17+
<dependencies>
18+
<!-- kaptcha -->
19+
<dependency>
20+
<groupId>com.github.axet</groupId>
21+
<artifactId>kaptcha</artifactId>
22+
<version>0.0.9</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework</groupId>
26+
<artifactId>spring-core</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework</groupId>
30+
<artifactId>spring-beans</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework</groupId>
34+
<artifactId>spring-context</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>junit</groupId>
38+
<artifactId>junit</artifactId>
39+
</dependency>
40+
</dependencies>
41+
42+
<!-- <repositories>
43+
<repository>
44+
<id>sonatype-forge</id>
45+
<name>Sonatype Forge</name>
46+
<url>http://repository.sonatype.org/content/groups/forge/</url>
47+
<releases>
48+
<enabled>true</enabled>
49+
</releases>
50+
<snapshots>
51+
<enabled>false</enabled>
52+
</snapshots>
53+
</repository>
54+
</repositories> -->
55+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.jun.plugin.teamplate.account.captcha;
2+
3+
public class AccountCaptchaException
4+
extends Exception
5+
{
6+
private static final long serialVersionUID = 1339439433313285858L;
7+
8+
public AccountCaptchaException( String message )
9+
{
10+
super( message );
11+
}
12+
13+
public AccountCaptchaException( String message, Throwable throwable )
14+
{
15+
super( message, throwable );
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.jun.plugin.teamplate.account.captcha;
2+
3+
import java.util.List;
4+
5+
public interface AccountCaptchaService
6+
{
7+
String generateCaptchaKey()
8+
throws AccountCaptchaException;
9+
10+
byte[] generateCaptchaImage( String captchaKey )
11+
throws AccountCaptchaException;
12+
13+
boolean validateCaptcha( String captchaKey, String captchaValue )
14+
throws AccountCaptchaException;
15+
16+
List<String> getPreDefinedTexts();
17+
18+
void setPreDefinedTexts( List<String> preDefinedTexts );
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package com.jun.plugin.teamplate.account.captcha;
2+
3+
import java.awt.image.BufferedImage;
4+
import java.io.ByteArrayOutputStream;
5+
import java.io.IOException;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.Properties;
10+
11+
import javax.imageio.ImageIO;
12+
13+
import org.springframework.beans.factory.InitializingBean;
14+
15+
import com.google.code.kaptcha.impl.DefaultKaptcha;
16+
import com.google.code.kaptcha.util.Config;
17+
18+
public class AccountCaptchaServiceImpl
19+
implements AccountCaptchaService, InitializingBean
20+
{
21+
private DefaultKaptcha producer;
22+
23+
private Map<String, String> captchaMap = new HashMap<String, String>();
24+
25+
private List<String> preDefinedTexts;
26+
27+
private int textCount = 0;
28+
29+
public void afterPropertiesSet()
30+
throws Exception
31+
{
32+
producer = new DefaultKaptcha();
33+
34+
producer.setConfig( new Config( new Properties() ) );
35+
}
36+
37+
public String generateCaptchaKey()
38+
{
39+
String key = RandomGenerator.getRandomString();
40+
41+
String value = getCaptchaText();
42+
43+
captchaMap.put( key, value );
44+
45+
return key;
46+
}
47+
48+
public List<String> getPreDefinedTexts()
49+
{
50+
return preDefinedTexts;
51+
}
52+
53+
public void setPreDefinedTexts( List<String> preDefinedTexts )
54+
{
55+
this.preDefinedTexts = preDefinedTexts;
56+
}
57+
58+
private String getCaptchaText()
59+
{
60+
if ( preDefinedTexts != null && !preDefinedTexts.isEmpty() )
61+
{
62+
String text = preDefinedTexts.get( textCount );
63+
64+
textCount = ( textCount + 1 ) % preDefinedTexts.size();
65+
66+
return text;
67+
}
68+
else
69+
{
70+
return producer.createText();
71+
}
72+
}
73+
74+
public byte[] generateCaptchaImage( String captchaKey )
75+
throws AccountCaptchaException
76+
{
77+
String text = captchaMap.get( captchaKey );
78+
79+
if ( text == null )
80+
{
81+
throw new AccountCaptchaException( "Captch key '" + captchaKey + "' not found!" );
82+
}
83+
84+
BufferedImage image = producer.createImage( text );
85+
86+
ByteArrayOutputStream out = new ByteArrayOutputStream();
87+
88+
try
89+
{
90+
ImageIO.write( image, "jpg", out );
91+
}
92+
catch ( IOException e )
93+
{
94+
throw new AccountCaptchaException( "Failed to write captcha stream!", e );
95+
}
96+
97+
return out.toByteArray();
98+
}
99+
100+
public boolean validateCaptcha( String captchaKey, String captchaValue )
101+
throws AccountCaptchaException
102+
{
103+
String text = captchaMap.get( captchaKey );
104+
105+
if ( text == null )
106+
{
107+
throw new AccountCaptchaException( "Captch key '" + captchaKey + "' not found!" );
108+
}
109+
110+
if ( text.equals( captchaValue ) )
111+
{
112+
captchaMap.remove( captchaKey );
113+
114+
return true;
115+
}
116+
else
117+
{
118+
return false;
119+
}
120+
}
121+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.jun.plugin.teamplate.account.captcha;
2+
3+
import java.util.Random;
4+
5+
public class RandomGenerator
6+
{
7+
private static String range = "0123456789abcdefghijklmnopqrstuvwxyz";
8+
9+
public static synchronized String getRandomString()
10+
{
11+
Random random = new Random();
12+
13+
StringBuffer result = new StringBuffer();
14+
15+
for ( int i = 0; i < 8; i++ )
16+
{
17+
result.append( range.charAt( random.nextInt( range.length() ) ) );
18+
}
19+
20+
return result.toString();
21+
}
22+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans
5+
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
6+
7+
<bean id="accountCaptchaService"
8+
class="com.jun.plugin.teamplate.account.captcha.AccountCaptchaServiceImpl">
9+
</bean>
10+
11+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.jun.plugin.teamplate.account.captcha;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.io.File;
6+
import java.io.FileOutputStream;
7+
import java.io.OutputStream;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
import org.springframework.context.ApplicationContext;
14+
import org.springframework.context.support.ClassPathXmlApplicationContext;
15+
16+
import com.jun.plugin.teamplate.account.captcha.AccountCaptchaService;
17+
18+
public class AccountCaptchaServiceTest
19+
{
20+
private AccountCaptchaService service;
21+
22+
@Before
23+
public void prepare()
24+
throws Exception
25+
{
26+
ApplicationContext ctx = new ClassPathXmlApplicationContext( "account-captcha.xml" );
27+
service = (AccountCaptchaService) ctx.getBean( "accountCaptchaService" );
28+
}
29+
30+
@Test
31+
public void testGenerateCaptcha()
32+
throws Exception
33+
{
34+
String captchaKey = service.generateCaptchaKey();
35+
assertNotNull( captchaKey );
36+
37+
byte[] captchaImage = service.generateCaptchaImage( captchaKey );
38+
assertTrue( captchaImage.length > 0 );
39+
40+
File image = new File( "target/" + captchaKey + ".jpg" );
41+
OutputStream output = null;
42+
try
43+
{
44+
output = new FileOutputStream( image );
45+
output.write( captchaImage );
46+
}
47+
finally
48+
{
49+
if ( output != null )
50+
{
51+
output.close();
52+
}
53+
}
54+
assertTrue( image.exists() && image.length() > 0 );
55+
}
56+
57+
@Test
58+
public void testValidateCaptchaCorrect()
59+
throws Exception
60+
{
61+
List<String> preDefinedTexts = new ArrayList<String>();
62+
preDefinedTexts.add( "12345" );
63+
preDefinedTexts.add( "abcde" );
64+
service.setPreDefinedTexts( preDefinedTexts );
65+
66+
String captchaKey = service.generateCaptchaKey();
67+
service.generateCaptchaImage( captchaKey );
68+
assertTrue( service.validateCaptcha( captchaKey, "12345" ) );
69+
70+
captchaKey = service.generateCaptchaKey();
71+
service.generateCaptchaImage( captchaKey );
72+
assertTrue( service.validateCaptcha( captchaKey, "abcde" ) );
73+
}
74+
75+
@Test
76+
public void testValidateCaptchaIncorrect()
77+
throws Exception
78+
{
79+
List<String> preDefinedTexts = new ArrayList<String>();
80+
preDefinedTexts.add( "12345" );
81+
service.setPreDefinedTexts( preDefinedTexts );
82+
83+
String captchaKey = service.generateCaptchaKey();
84+
service.generateCaptchaImage( captchaKey );
85+
assertFalse( service.validateCaptcha( captchaKey, "67890" ) );
86+
}
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.jun.plugin.teamplate.account.captcha;
2+
3+
import static org.junit.Assert.assertFalse;
4+
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
import org.junit.Test;
9+
10+
import com.jun.plugin.teamplate.account.captcha.RandomGenerator;
11+
12+
public class RandomGeneratorTest
13+
{
14+
@Test
15+
public void testGetRandomString()
16+
throws Exception
17+
{
18+
Set<String> randoms = new HashSet<String>( 100 );
19+
20+
for ( int i = 0; i < 100; i++ )
21+
{
22+
String random = RandomGenerator.getRandomString();
23+
24+
assertFalse( randoms.contains( random ) );
25+
26+
randoms.add( random );
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)