Compare commits

...

22 Commits

@ -0,0 +1,22 @@
package com.controller.admin;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.po.Auser;
import com.service.admin.AdminService;
@Controller
public class AdminController {
@Autowired
private AdminService adminService;
@RequestMapping("/admin")
public String toLogin(@ModelAttribute Auser auser) {
return "admin/login";
}
@RequestMapping("admin/login")
public String login(@ModelAttribute Auser auser, Model model, HttpSession session) {
return adminService.login(auser, model, session);
}
}

@ -0,0 +1,36 @@
package com.controller.admin;
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 com.service.admin.AdminEmailAndUserService;
@Controller
@RequestMapping("/admin")
public class AdminEmailAndUserController extends BaseController{
@Autowired
private AdminEmailAndUserService adminEmailAndUserService;
@RequestMapping("/selectEmail")
public String selectAllEmail(Model model) {
return adminEmailAndUserService.selectAllEmail(model);
}
@RequestMapping("/delete")
public String delete(Integer id) {
return adminEmailAndUserService.delete(id);
}
@RequestMapping("/selectUser")
public String selectUser(Model model) {
return adminEmailAndUserService.selectBuserEmail(model);
}
@RequestMapping("/lock")
public String lock(Integer id) {
return adminEmailAndUserService.lock(id);
}
@RequestMapping("/unLock")
public String unLock(Integer id) {
return adminEmailAndUserService.unLock(id);
}
}

@ -0,0 +1,22 @@
package com.controller.before;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.exception.UserLoginNoException;
@Controller
public class BaseBeforeController {
/**
* 前台用户登录权限控制,处理方法执行前执行该方法
* @throws UserLoginNoException
*/
@ModelAttribute
public void isLogin(HttpSession session, HttpServletRequest request) throws UserLoginNoException {
if(session.getAttribute("emailuser") == null){
throw new UserLoginNoException("没有登录");
}
}
}

@ -0,0 +1,21 @@
package com.controller.admin;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.exception.AdminLoginNoException;
@Controller
public class BaseController {
/**
* 登录权限控制,处理方法执行前执行该方法
* @throws AdminLoginNoException
*/
@ModelAttribute
public void isLogin(HttpSession session, HttpServletRequest request) throws AdminLoginNoException {
if(session.getAttribute("auser") == null){
throw new AdminLoginNoException("没有登录");
}
}
}

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.forum.dao.CommentDao">
<resultMap id="comment" type="Comment">
<id property="id" column="cid"/>
<result property="username" column="username"/>
<result property="email" column="email"/>
<result property="content" column="content"/>
<result property="userComment" column="usercomment"/>
<result property="avatar" column="avatar"/>
<result property="createTime" column="create_time"/>
<result property="forumId" column="forum_id"/>
<association property="forum" javaType="Forum">
<id property="id" column="id"/>
</association>
</resultMap>
<delete id="delById">
delete from comment where id=#{id}
</delete>
<select id="findByForumIdAndParentCommentNull" resultMap="comment">
select c.id cid,c.username,c.email,c.content,c.avatar,
c.create_time,c.forum_id,c.parent_comment_id
from comment c, forum b
where c.forum_id = b.id and c.forum_id = #{forumId}
order by c.create_time desc
</select>
<insert id="saveComment" parameterType="Comment">
insert into comment (username,email,content,avatar,
create_time,forum_id,parent_comment_id, usercomment)
values (#{username},#{email},#{content},#{avatar},
#{createTime},#{forumId},#{parentCommentId}, #{userComment});
</insert>
<select id="findByParentCommentId" resultMap="comment">
select c.id cid, c.username, c.email, c.content, c.avatar,
c.create_time, c.forum_id, c.parent_comment_id
from comment c
where c.parent_comment_id = #{parentCommentId}
</select>
<select id="getAllComment" resultType="com.forum.entity.Comment">
select id,username,email,content,create_time from comment
</select>
</mapper>

@ -0,0 +1,106 @@
package com.controller.before;
import java.io.FileInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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 com.po.Email;
import com.service.before.EmailService;
import com.util.MyUtil;
@Controller
@RequestMapping("/email")
public class EmailController extends BaseBeforeController{
@Autowired
private EmailService emailService;
/**
* 写信
*/
@RequestMapping("/write")
public String write(Email myemail, Model model, HttpServletRequest request, HttpSession session) {
return emailService.write(myemail,model,request,session);
}
/**
* 回复初始化
*/
@RequestMapping("/reEmailInit")
public String reEmailInit(Integer id, Model model, HttpSession session) {
return emailService.reEmailInit(id, model);
}
/**
* 收信
*/
@RequestMapping("/recieve")
public String recieve(Model model, HttpSession session) {
return emailService.recieve(model, session);
}
/**
* 已发送
*/
@RequestMapping("/send")
public String send(Model model, HttpSession session) {
return emailService.send(model, session);
}
/**
* 打开信件看详情
*/
@RequestMapping("/detail")
public String detail(Model model, Integer id) {//id接收参数id
return emailService.detail(model, id);
}
/**
* 删除一个邮件
*/
@RequestMapping("/deleteOne")
public String deleteOne(Integer id) {//id接收参数id
return emailService.deleteOne(id);
}
/***
* 删除多个邮件
*/
@RequestMapping("/deleteMore")
public String deleteMore(Integer ids[]) {//id接收参数id
return emailService.deleteMore(ids);
}
/**
* 下载附件,该方法不访问数据库,故只写控制层
*/
@RequestMapping("/down")
public String down(String fileid, HttpServletRequest request, HttpServletResponse response) {//fileid接收参数fileid
String aFilePath = null; //要下载的文件路径
FileInputStream in = null; //输入流
ServletOutputStream out = null; //输出流
try {
//从workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps下载
aFilePath = request.getServletContext().getRealPath("uploadfiles");
//设置下载文件使用的报头
response.setHeader("Content-Type", "application/x-msdownload" );
response.setHeader("Content-Disposition", "attachment; filename="
+ MyUtil.toUTF8String(fileid));
// 读入文件
in = new FileInputStream(aFilePath + "\\"+ fileid);
//得到响应对象的输出流,用于向客户端输出二进制数据
out = response.getOutputStream();
out.flush();
int aRead = 0;
byte b[] = new byte[1024];
while ((aRead = in.read(b)) != -1 & in != null) {
out.write(b,0,aRead);
}
out.flush();
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
}

@ -0,0 +1,202 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.forum.dao.ForumDao">
<resultMap id="forum" type="Forum">
<id property="id" column="id"/>
<result property="title" column="title"/>
<result property="content" column="content"/>
<result property="flag" column="flag"/>
<result property="views" column="views"/>
<result property="updateTime" column="update_time"/>
<result property="avatars" column="avatars"/>
<result property="commentabled" column="commentabled"/>
<result property="description" column="description"/>
<result property="createTime" column="create_time"/>
<result property="userId" column="user_id"/>
<result property="like" column="like"/>
<result property="tagIds" column="tag_ids"/>
<association property="user" javaType="User">
<id property="id" column="uid"/>
<result property="nickname" column="nickname"/>
<result property="username" column="username"/>
<result property="email" column="email"/>
<result property="avatar" column="avatar"/>
</association>
<collection property="tags" ofType="Tag">
<id property="id" column="tagid"/>
<result property="name" column="tagname"/>
</collection>
<!-- <collection property="tagsId" ofType="tagIds">-->
<!-- <result property="tag_id" column="tag_id"/>-->
<!-- </collection>-->
</resultMap>
<resultMap id="ForumAndIds" type="forum">
<id property="id" column="id"/>
<result property="title" column="title"/>
<result property="updateTime" column="update_time"/>
<!-- <collection property="tagsId" ofType="tagIds">-->
<!-- <result property="tag_id" column="tag_id"/>-->
<!-- </collection>-->
</resultMap>
<delete id="deleteForum">
delete from forum where id = #{id}
</delete>
<select id="getIndexForum" resultMap="forum"> /*主页帖子展示*/
select b.id, b.title, b.avatars,b.flag, b.views, b.create_time, b.description,b.like,
a.username, a.avatar
from forum b,user a
where a.id = b.user_id order by b.create_time desc
</select>
<select id="getForumTadIds" resultMap="forum"> /*主页帖子展示*/
select b.id, b.title, b.avatars, b.views, b.update_time, b.description,b.like,
a.username, a.avatar,
bt.tag_id
from user a,forum b
left join forum_tags bt on b.id=bt.forum_id
where a.id = b.user_id
-- order by b.update_time desc
-- 0.0.
-- 0
</select>
<select id="getAllForum" resultMap="forum"> /*后台帖子展示*/
select b.id, b.title, b.create_time, b.commentabled
from forum b order by b.create_time desc
</select>
<select id="getByTagId" resultMap="forum">
select b.id, b.title,b.flag, b.avatars, b.views, b.update_time, b.description,
t1.name tagname, t1.id tagid,
a.username, a.avatar
from forum b, user a, forum_tags tb, tag t1
where a.id = b.user_id and tb.forum_id = b.id and tb.tag_id = t1.id and t1.id = #{tagId}
order by b.update_time desc
</select>
<select id="getForum" resultMap="forum"> /*后台展示帖子*/
select b.id, b.flag, b.title, b.content,
b.tag_ids, b.avatars, b.description, b.commentabled
from forum b where b.id = #{id};
</select>
<select id="getDetailedForum" resultMap="forum"> /*帖子详情*/
select b.id, b.avatars, b.flag, b.title, b.content, b.views,
b.commentabled,b.like,a.username, a.avatar,tag.id tagid, tag.name tagname
from forum b, user a, tag tag, forum_tags tb
where b.user_id = a.id and tb.forum_id = b.id and tb.tag_id = tag.id and b.id = #{id}
</select>
<select id="getSearchForum" resultMap="forum">
<bind name="pattern" value="'%' + query + '%'"/>
select b.id, b.title, b.avatars, b.views, b.create_time, b.description,b.like,
a.username, a.avatar
from forum b, user a
where a.id = b.user_id and (b.title like #{pattern} or b.content like #{pattern})
order by b.create_time desc
</select>
<select id="searchAllForum" parameterType="Forum" resultMap="forum">
<bind name="pattern" value="'%' + title + '%'"/>
/*模糊查询*/
select b.id, b.title, b.update_time, b.published, t.id, t.name
from forum b where b.title like #{pattern}
</select>
<update id="updateForum" parameterType="Forum">
update forum set flag = #{flag} ,
title = #{title}, content = #{content}, tag_ids = #{tagIds},
avatars = #{avatars} , description = #{description} ,
commentabled = #{commentabled} ,update_time = #{updateTime} where id = #{id};
</update>
<update id="updateView">
update forum b set b.views=b.views+1 where id=#{id};
</update>
<!--useGeneratedKeys="true";使用自增主键获取主键值策略
keyProperty指定对应的主键属性也就是mybatis获取到主键值以后将这个值封装给javaBean的哪个属性
-->
<insert id="saveForum" parameterType="Forum" useGeneratedKeys="true" keyProperty="id">
insert into forum (title, content, avatars, flag,
views, commentabled, create_time, update_time, tag_ids, user_id, description)
values (#{title}, #{content}, #{avatars}, #{flag}, #{views}, #{commentabled}, #{createTime},
#{updateTime}, #{tagIds}, #{userId}, #{description});
</insert>
<insert id="saveForumAndTag" parameterType="ForumAndTag">
insert into forum_tags (tag_id, forum_id) values (#{tagId},#{forumId});
</insert>
<select id="findGroupYear" resultType="String">
select DATE_FORMAT(b.updateTime, '%Y') from forum b
</select>
<!-- 查找所有月份月份-->
<select id="findGroupMonth" resultType="String">
select DATE_FORMAT(b.update_time, '%M') from forum b order by b.update_time desc
</select>
<!-- 把所有文章下的标签id查询出来-->
<select id="findForumAndTagsByYear" resultMap="ForumAndIds">
select b.title, b.id,b.update_time,
bt.tag_id
from forum b
left join forum_tags bt on b.id=bt.forum_id
where DATE_FORMAT(b.update_time, "%Y") = #{year} order by b.update_time desc;
</select>
<!-- 按年份+月份查找-->
<select id="findByMonth" resultMap="forum">
select b.title, b.update_time, b.id, b.flag
from forum b
where DATE_FORMAT(b.update_time, "%Y%M") = #{month}
</select>
<!--最热推荐-->
<select id="findForumByRank" resultType="rankForum">
select b.title,b.id from forum b order by b.views desc limit 0,3
</select>
<!--最新推荐-->
<select id="getNewForum" resultType="rankForum">
select b.title,b.id from forum b order by b.create_time desc limit 0,3
</select>
<select id="getLike" resultType="Long">
select b.like from forum b where b.id=#{id}
</select>
<update id="addLike" >
update forum b set b.like=b.like+1 where b.id=#{id}
</update>
<select id="countView" resultType="java.lang.Integer">
select count(views) from forum
</select>
<delete id="cancelLike">
update forum b set b.like=b.like-1 where b.id=#{forumId}
</delete>
</mapper>

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.forum.dao.LinkDao">
<insert id="saveLink" parameterType="Link">
insert into link(name ,address,create_time) values (#{name},#{address},#{createTime});
</insert>
<delete id="deleteLink">
delete from link where id = #{id}
</delete>
<select id="getAllLink" resultType="Link">
select id,name,address,create_time from link
</select>
<select id="countLink" resultType="java.lang.Integer">
select count(*) from link
</select>
</mapper>

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.forum.dao.MessageDao">
<resultMap id="messages" type="message">
<id property="id" column="id"/>
<result property="content" column="content"/>
<result property="name" column="name"/>
<result property="msg_avatar" column="msg_avatar"/>
<result property="createTime" column="create_time"/>
<collection property="replies" ofType="reply">
<id property="id" column="rid"/>
<result property="content" column="con"/>
<!-- <result property="reply_name" column="reply_name"/>-->
<result property="names" column="names"/>
<result property="createTime" column="ctime"/>
<result property="avatar" column="avatar"/>
<result property="mes_id" column="mes_id"/>
</collection>
</resultMap>
<delete id="deleteById">
delete from message where id=#{id}
</delete>
<select id="findAllMessage" resultMap="messages">
select m.*,
r.id rid,
r.content con,
r.names names,
r.create_time ctime,
r.avatar,
r.mes_id
from message m left join reply r on r.mes_id=m.id
order by m.create_time desc
</select>
<select id="getIndexMessage" resultMap="messages">
select m.*
from message m
order by m.create_time desc
limit 0,6
</select>
<select id="findAll" resultType="com.forum.entity.Message">
select id,content,name, create_time from message
</select>
<insert id="saveMessage" parameterType="message">
insert into message (content,name,create_time,msg_avatar)
values (#{content},#{name},#{createTime},#{msg_avatar})
</insert>
<insert id="saveReplyMessage" parameterType="reply">
insert into reply (content,names,create_time,mes_id,avatar)
values (#{content},#{names},#{createTime},#{mes_id},#{avatar})
</insert>
</mapper>

@ -0,0 +1,40 @@
package com.controller.before;
import javax.servlet.http.HttpSession;
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 com.po.Buser;
import com.service.before.NavigateService;
@Controller
public class NavigateController extends BaseBeforeController{
@Autowired
private NavigateService navigateService;
/**
* 转到写信页面
*/
@RequestMapping("/toWrite")
public String toWrite(Model model) {
return navigateService.toWrite(model);
}
/***
* 安全退出不需访问数据库这里不需要service层和dao层
*/
@RequestMapping("/exit")
public String exit(HttpSession session, Model model) {
session.invalidate();
model.addAttribute("buser", new Buser());
return "before/login";
}
/***
* 去修改密码页面不需访问数据库这里不需要service层和dao层
*/
@RequestMapping("/toUpdate")
public String toUpdate(HttpSession session, Model model) {
model.addAttribute("buser", (Buser)session.getAttribute("emailuser"));
return "before/update";
}
}

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.forum.dao.NoticeDao">
<select id="findAllNotice" resultType="notice">
select * from notice
</select>
<select id="countNotice" resultType="java.lang.Integer">
select count(*) from notice
</select>
<insert id="addNotices" parameterType="notice">
insert into notice(content) values(#{content})
</insert>
<delete id="deleteNotice" parameterType="int">
delete from notice where id=#{id}
</delete>
</mapper>

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.forum.dao.ReplyDao">
<delete id="del">
delete from reply where id=#{id}
</delete>
<select id="findAll" resultType="com.forum.entity.Reply">
select id,content,names ,create_time from reply
</select>
</mapper>

@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.forum.dao.TagDao">
<select id="getTag" resultType="Tag">
select id,name from tag where id = #{id}
</select>
<insert id="saveTag" parameterType="Tag">
insert into tag values (#{id},#{name});
</insert>
<!--两个id可能会相冲取别名即可-->
<resultMap id="tags" type="Tag">
<id property="id" column="tid"/>
<result property="name" column="name"/>
<collection property="forums" ofType="Forum">
<id property="id" column="bid"/>
<result property="title" column="title"/>
</collection>
</resultMap>
<select id="getAllTag" resultType="com.forum.entity.Tag">
select * from tag
</select>
<select id="getForumTag" resultMap="tags">
select t.id tid, t.name, b.id bid, b.title
from tag t, forum b, forum_tags bt
where t.id = bt.tag_id and b.id = bt.forum_id
</select>
<select id="getTagByName" resultType="Tag">
select * from tag where name = #{name}
</select>
<select id="getTagById" resultType="Tag">
select * from tag where id = #{id}
</select>
<select id="countTag" resultType="java.lang.Integer">
select count(*) from tag
</select>
<delete id="deleteTag">
delete from tag where id = #{id}
</delete>
<update id="updateTag" parameterType="Tag">
update tag set name = #{name} where id = #{id};
</update>
</mapper>

@ -0,0 +1,49 @@
package com.controller.before;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.po.Buser;
import com.service.before.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/register")
public String register(@ModelAttribute Buser buser,Model model, HttpSession session, String code) {
return userService.register(buser, model, session, code);
}
@RequestMapping("/login")
public String login(@ModelAttribute Buser buser,Model model, HttpSession session, String code) {
return userService.login(buser, model, session, code);
}
/**
* 转到登录页面
*/
@RequestMapping("/toLogin")
public String toLogin(Model model) {
return userService.toLogin(model);
}
/**
* 转到注册页面
*/
@RequestMapping("/toRegister")
public String toRegister(Model model) {
return userService.toRegister(model);
}
/**
* 修改密码
*/
@RequestMapping("/update")
public String update(Model model, HttpSession session, String bpwd) {
return userService.update(model, session, bpwd);
}
}

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.forum.dao.UserDao">
<update id="update">
update user set password=#{password} where username=#{username}
</update>
<select id="queryByUsernameAndPassword" resultType="com.forum.entity.User">
select * from user
where username = #{username} and password = #{password};
</select>
<insert id="addUser" parameterType="user">
insert into user(username,password,email,create_time,update_time,type,school,avatar)
values(#{username},#{password},#{email},#{createTime},#{updateTime},#{type},#{school},#{avatar})
</insert>
<select id="getAllUser" resultType="com.forum.entity.User">
select id,username,password,email,create_time,type from user
</select>
<delete id="deleteUser" >
delete from user where id = #{id}
</delete>
<select id="count" resultType="Integer" >
select count(*) from user where type = 0
</select>
</mapper>

@ -0,0 +1,95 @@
package com.controller.before;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 验证码
*/
@Controller
public class ValidateCodeController {
private char code[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2',
'3', '4', '5', '6', '7', '8', '9' };
private static final int WIDTH = 50;
private static final int HEIGHT = 20;
private static final int LENGTH = 4;
@RequestMapping("/validateCode")
public void validateCode(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// 设置响应报头信息
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 设置响应的MIME类型
response.setContentType("image/jpeg");
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
Font mFont = new Font("Arial", Font.TRUETYPE_FONT, 18);
Graphics g = image.getGraphics();
Random rd = new Random();
// 设置背景颜色
g.setColor(new Color(rd.nextInt(55) + 200, rd.nextInt(55) + 200, rd
.nextInt(55) + 200));
g.fillRect(0, 0, WIDTH, HEIGHT);
// 设置字体
g.setFont(mFont);
// 画边框
g.setColor(Color.black);
g.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);
// 随机产生的验证码
String result = "";
for (int i = 0; i < LENGTH; ++i) {
result += code[rd.nextInt(code.length)];
}
HttpSession se = request.getSession();
se.setAttribute("code", result);
// 画验证码
for (int i = 0; i < result.length(); i++) {
g.setColor(new Color(rd.nextInt(200), rd.nextInt(200), rd
.nextInt(200)));
g.drawString(result.charAt(i) + "", 12 * i + 1, 16);
}
// 随机产生2个干扰线
for (int i = 0; i < 2; i++) {
g.setColor(new Color(rd.nextInt(200), rd.nextInt(200), rd
.nextInt(200)));
int x1 = rd.nextInt(WIDTH);
int x2 = rd.nextInt(WIDTH);
int y1 = rd.nextInt(HEIGHT);
int y2 = rd.nextInt(HEIGHT);
g.drawLine(x1, y1, x2, y2);
}
// 释放图形资源
g.dispose();
try {
OutputStream os = response.getOutputStream();
// 输出图像到页面
ImageIO.write(image, "JPEG", os);
} catch (IOException e) {
e.printStackTrace();
}
}
}

@ -130,12 +130,18 @@
10%, 30%, 50%, 70%, 90% {
transform: translate3d(-10px, 0, 0);
}
20%, 40%, 60%, 80% {
transform: translate3d(10px, 0, 0);
}
}
.shake {
animation-name: shake;
}
@ -149,6 +155,8 @@
transform: translateX(-6px) rotateY(-9deg);
}
18.5% {
transform: translateX(5px) rotateY(7deg);
}

Loading…
Cancel
Save