Skip to content

Commit

Permalink
加入kaptcha验证码框架并实现验证码
Browse files Browse the repository at this point in the history
  • Loading branch information
xwj-vic committed Mar 8, 2017
1 parent 6b39120 commit 3b253d8
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 24 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@
<version>1.0</version>
</dependency>

<!--kaptcha验证码-->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>

</dependencies>
<build>
<finalName>ssmshiro</finalName>
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/web/CaptchaController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package web;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;

/**
* Created by xuweijie on 2017/3/8.
* 生成验证码Controller.
*/
@Controller
public class CaptchaController {

private Producer kaptchaProducer=null;

@Autowired
public void setCaptchaProducer(Producer kaptchaProducer) {
this.kaptchaProducer = kaptchaProducer;
}

@RequestMapping("/kaptcha")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception{
response.setDateHeader("Expires",0);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
String capText = kaptchaProducer.createText();
request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
BufferedImage bi = kaptchaProducer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
return null;
}
}
43 changes: 21 additions & 22 deletions src/main/java/web/LoginController.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package web;

import entity.User;
import com.google.code.kaptcha.Constants;
import exception.CustomException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import service.ShiroUserService;
import util.MD5Util;

import javax.servlet.http.HttpServletRequest;
import java.rmi.activation.UnknownObjectException;

/**
* Created by xuweijie on 2017/3/3.
Expand All @@ -27,24 +21,29 @@ public class LoginController {
@RequestMapping(value = "/login",method = RequestMethod.POST)
public String login(HttpServletRequest request, Model model){
CustomException customException=null;
String verifyCode=request.getParameter("verifyCode").toUpperCase();
String username=request.getParameter("username");
String password=request.getParameter("password");
if((username!=null && password!=null)){
UsernamePasswordToken token=new UsernamePasswordToken(username,password);
Subject subject= SecurityUtils.getSubject();
try{
subject.login(token);
}catch (AuthenticationException e){
customException=new CustomException(e.getMessage());
}
if( subject.isAuthenticated()){
subject.logout();
model.addAttribute("username",username);
return "/loginsuccess";
}else {
model.addAttribute("exception",customException.getMessage());
return "/refuse";
if(verifyCode.equals(request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY))){
if((username!=null && password!=null)){
UsernamePasswordToken token=new UsernamePasswordToken(username,password);
Subject subject= SecurityUtils.getSubject();
try{
subject.login(token);
}catch (AuthenticationException e){
customException=new CustomException(e.getMessage());
}
if( subject.isAuthenticated()){
subject.logout();
model.addAttribute("username",username);
return "/loginsuccess";
}else {
model.addAttribute("exception",customException.getMessage());
return "/refuse";
}
}
}else {
System.out.print("验证码输入不正确");
}
return "login";
}
Expand Down
43 changes: 43 additions & 0 deletions src/main/resources/spring/spring-kaptcha.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="defaultKaptcha" class="com.google.code.kaptcha.impl.DefaultKaptcha">
<property name="config">
<bean class="com.google.code.kaptcha.util.Config">
<constructor-arg>
<props>
<!-- 验证码宽度 -->
<prop key="kaptcha.image.width">110</prop>
<!-- 验证码高度 -->
<prop key="kaptcha.image.height">50</prop>
<!-- 生成验证码内容范围 -->
<prop key="kaptcha.textproducer.char.string">0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ</prop>
<!-- 验证码个数 -->
<prop key="kaptcha.textproducer.char.length">4</prop>
<!-- 是否有边框 -->
<prop key="kaptcha.border">no</prop>
<!-- 边框颜色 -->
<prop key="kaptcha.border.color">105,179,90</prop>
<!-- 边框厚度 -->
<prop key="kaptcha.border.thickness">1</prop>
<!-- 验证码字体颜色 -->
<prop key="kaptcha.textproducer.font.color">black</prop>
<!-- 验证码字体大小 -->
<prop key="kaptcha.textproducer.font.size">30</prop>
<!-- 验证码所属字体样式 -->
<prop key="kaptcha.textproducer.font.names">楷体</prop>
<!-- 干扰线颜色 -->
<prop key="kaptcha.noise.color">black</prop>
<!-- 验证码文本字符间距 -->
<prop key="kaptcha.textproducer.char.space">3</prop>
<!-- 图片样式 :阴影-->
<prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.ShadowGimpy</prop>
</props>
</constructor-arg>
</bean>
</property>
</bean>

</beans>
12 changes: 10 additions & 2 deletions src/main/webapp/WEB-INF/jsp/login.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
<html>
<head>
<title>用户登录</title>
<script type="text/javascript">
function changeVerifyCode() {
var time=new Date().getTime();
document.getElementById("kaptchaImage").src="/kaptcha?d="+time;//为了不让验证码缓存,为了安全起见,需要次次都刷新
}
</script>
</head>

<body>
Expand All @@ -10,8 +16,10 @@
<h1>登录</h1>
<br><br><br>
<form action="/login" method="POST">
&nbsp;&nbsp;&nbsp;&nbsp;名:<input type="text" name="username"><br><br><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码:<input type="password" name="password"><br><br>
&nbsp;&nbsp;&nbsp;&nbsp;名:<input type="text" name="username" placeholder="请输入用户名"><br><br><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码:<input type="password" name="password" placeholder="请输入密码"><br><br>
&nbsp;&nbsp;&nbsp;&nbsp;码:<input type="text" name="verifyCode" placeholder="请输入验证码"/>
<img src="/kaptcha.jpg" id="kaptchaImage" title="看不清,点击换一张" onclick="changeVerifyCode()"><br><br>
<input type="submit" value="登录">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="取消">
</form>
</div>
Expand Down

0 comments on commit 3b253d8

Please sign in to comment.