Commit daca4423 by fsn

登录增加验证码功能

parent c7590e56
......@@ -19,6 +19,13 @@
<dependencies>
<!--验证码生成-->
<dependency>
<groupId>com.google.code</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.0</version>
</dependency>
<!--缓存框架-->
<dependency>
<groupId>org.ehcache</groupId>
......
......@@ -25,6 +25,7 @@ public enum BizExceptionEnum {
DB_RESOURCE_NULL(400,"数据库中没有该资源"),
NO_PERMITION(405, "权限异常"),
REQUEST_INVALIDATE(400,"请求数据格式不正确"),
INVALID_KAPTCHA(400,"验证码不正确"),
/**
* 账户问题
......
package com.stylefeng.guns.common.exception;
/**
* 验证码错误异常
*
* @author fengshuonan
* @date 2017-05-05 23:52
*/
public class InvalidKaptchaException extends RuntimeException {
}
......@@ -3,6 +3,7 @@ package com.stylefeng.guns.core.aop;
import com.stylefeng.guns.common.constant.tips.ErrorTip;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.exception.InvalidKaptchaException;
import com.stylefeng.guns.core.log.LogManager;
import com.stylefeng.guns.core.log.factory.LogTaskFactory;
import com.stylefeng.guns.core.shiro.ShiroKit;
......@@ -89,6 +90,20 @@ public class GlobalExceptionHandler {
}
/**
* 验证码错误
*
* @author fengshuonan
*/
@ExceptionHandler(InvalidKaptchaException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public String credentials(InvalidKaptchaException e, Model model) {
String username = getRequest().getParameter("username");
LogManager.me().executeLog(LogTaskFactory.loginLog(username, "验证码错误", getIp()));
model.addAttribute("tips", "验证码错误");
return "/login.html";
}
/**
* 无权访问该资源
*
* @author fengshuonan
......
package com.stylefeng.guns.modular.system.controller;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
/**
* 验证码生成
*
* @author fengshuonan
* @date 2017-05-05 23:10
*/
@Controller
@RequestMapping("/kaptcha")
public class KaptchaController {
@Resource
Producer producer;
/**
* 生成验证码
*/
@RequestMapping("")
public void index(HttpServletRequest request, HttpServletResponse response) {
HttpSession session = request.getSession();
response.setDateHeader("Expires", 0);
// Set standard HTTP/1.1 no-cache headers.
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
// Set standard HTTP/1.0 no-cache header.
response.setHeader("Pragma", "no-cache");
// return a jpeg
response.setContentType("image/jpeg");
// create the text for the image
String capText = producer.createText();
// store the text in the session
session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
// create the image with the text
BufferedImage bi = producer.createImage(capText);
ServletOutputStream out = null;
try {
out = response.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
// write the data out
try {
ImageIO.write(bi, "jpg", out);
} catch (IOException e) {
e.printStackTrace();
}
try {
try {
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.stylefeng.guns.modular.system.controller;
import com.google.code.kaptcha.Constants;
import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.exception.InvalidKaptchaException;
import com.stylefeng.guns.common.node.MenuNode;
import com.stylefeng.guns.core.log.LogManager;
import com.stylefeng.guns.core.log.factory.LogTaskFactory;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.MenuDao;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
......@@ -62,8 +65,16 @@ public class LoginController extends BaseController {
*/
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String loginVali() {
String username = super.getPara("username");
String password = super.getPara("password");
String kaptcha = super.getPara("kaptcha");
//验证验证码是否正确
String code = (String) super.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
if(ToolUtil.isEmpty(kaptcha) || !kaptcha.equals(code)){
throw new InvalidKaptchaException();
}
Subject currentUser = ShiroKit.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(username, password.toCharArray());
......
package project.config.root;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.*;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
......@@ -8,6 +10,8 @@ import project.config.datasource.DataSourceConfig;
import project.config.shiro.ShiroConfig;
import project.config.web.monitor.DruidMonitorConfig;
import java.util.Properties;
/**
* spring的根配置
*
......@@ -22,4 +26,21 @@ import project.config.web.monitor.DruidMonitorConfig;
@Import(value = {DataSourceConfig.class, ShiroConfig.class, DruidMonitorConfig.class, EhcacheConfig.class})
public class RootSpringConfig {
@Bean
public DefaultKaptcha kaptcha(){
Properties properties = new Properties();
properties.put("kaptcha.border","no");
properties.put("kaptcha.border.color","105,179,90");
properties.put("kaptcha.textproducer.font.color","blue");
properties.put("kaptcha.image.width","125");
properties.put("kaptcha.image.height","45");
properties.put("kaptcha.textproducer.font.size","45");
properties.put("kaptcha.session.key","code");
properties.put("kaptcha.textproducer.char.length","4");
properties.put("kaptcha.textproducer.font.names","宋体,楷体,微软雅黑");
Config config = new Config(properties);
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
......@@ -114,7 +114,7 @@ public class ShiroConfig {
Map<String, String> hashMap = new HashMap<String, String>();
hashMap.put("/static/**", "anon");
hashMap.put("/login", "anon");
hashMap.put("/captcha", "anon");
hashMap.put("/kaptcha", "anon");
hashMap.put("/**", "user");
shiroFilter.setFilterChainDefinitionMap(hashMap);
......
......@@ -6,39 +6,56 @@
<title>Guns - 登录</title>
<link rel="shortcut icon" href="${ctxPath}/static/favicon.ico"> <link href="${ctxPath}/static/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
<link rel="shortcut icon" href="${ctxPath}/static/favicon.ico">
<link href="${ctxPath}/static/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
<link href="${ctxPath}/static/css/font-awesome.css?v=4.4.0" rel="stylesheet">
<link href="${ctxPath}/static/css/animate.css" rel="stylesheet">
<link href="${ctxPath}/static/css/style.css?v=4.1.0" rel="stylesheet">
<script>if(window.top !== window.self){ window.top.location = window.location;}</script>
<script>if (window.top !== window.self) {
window.top.location = window.location;
}</script>
<script src="${ctxPath}/static/js/jquery.min.js?v=2.1.4"></script>
<script src="${ctxPath}/static/js/bootstrap.min.js?v=3.3.6"></script>
</head>
<body class="gray-bg">
<div class="middle-box text-center loginscreen animated fadeInDown">
<div style="padding: 100px 0px;">
<div>
<h1 class="logo-name">GS</h1>
<div class="middle-box text-center loginscreen animated fadeInDown">
<div style="padding: 100px 0px;">
<div>
<h1 class="logo-name">GS</h1>
</div>
<h3>欢迎使用 Guns</h3>
<br/>
<h4 style="color: red;">${tips!}</h4>
<form class="m-t" role="form" action="${ctxPath}/login" method="post">
<div class="form-group">
<input type="text" name="username" class="form-control" placeholder="用户名" required="">
</div>
<div class="form-group">
<input type="password" name="password" class="form-control" placeholder="密码" required="">
</div>
<h3>欢迎使用 Guns</h3>
<br/>
<h4 style="color: red;">${tips!}</h4>
<form class="m-t" role="form" action="${ctxPath}/login" method="post">
<div class="form-group">
<input type="text" name="username" class="form-control" placeholder="用户名" required="">
<div class="form-group" style="float: left;">
<div class="col-sm-8" style="padding-left: 0px; padding-right: 0px;">
<input class="form-control" type="text" name="kaptcha" placeholder="验证码" required="">
</div>
<div class="form-group">
<input type="password" name="password" class="form-control" placeholder="密码" required="">
<div class="col-sm-4" style="padding-left: 0px; padding-right: 0px;">
<img src="${ctxPath}/kaptcha" id="kaptcha" width="100%" height="100%"/>
</div>
<button type="submit" class="btn btn-primary block full-width m-b">登 录</button>
</p>
</form>
</div>
</div>
<button type="submit" class="btn btn-primary block full-width m-b">登 录</button>
</p>
</form>
</div>
</div>
<!-- 全局js -->
<script src="${ctxPath}/static/js/jquery.min.js?v=2.1.4"></script>
<script src="${ctxPath}/static/js/bootstrap.min.js?v=3.3.6"></script>
<script>
$(function(){
$("#kaptcha").on('click',function(){
$("#kaptcha").attr('src', '${ctxPath}/kaptcha?' + Math.floor(Math.random()*100) ).fadeIn();
});
});
</script>
</body>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment