记:最近两天在完善毕业设计和论文终稿,完善毕业设计时想要在登录注册模块添加一个验证码功能,在网上找了很多参考资料,但是在实现的时候都不是很尽如人意。后来找了一个在前端直接生成的验证码代码,根据自己的毕设项目进行了修改,已经可以完美运行(姑且认为是完美吧~)并且符合我的要求。有点长,但是很详细,如果没有c币的可以留下邮箱~
(完整项目资源中包含的也有lib,js,css包,可以只下载完整登录注册项目)
完整登录注册项目下载地址:https://download.csdn.net/download/qq_38938670/12344968
js和css,lib包下载地址(image文件夹在登录注册时用不到):https://download.csdn.net/download/qq_38938670/12344986
我的数据库中管理员账号:admin 密码:123456,普通用户自己注册就可以。后面我会给出数据库的相关代码,具体的界面就自己根据自己的项目进行添加和修改,我的毕设完整项目还未完全完成,完成后会进一步更新全部的项目。
(1)FormController.java
package org.fkit.dym.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
/**
*
* @author
*页面跳转控制
*/
@Controller
public class FormController {
@RequestMapping(value="/{formName}")
/**pathvariabe注解可以方便获得请求url中的动态参数*/
public String loginForm(@PathVariable String formName) {
return formName;
}
}
(2)UserController.java
package org.fkit.dym.controller;
import javax.servlet.http.HttpSession;
import org.fkit.dym.domain.User;
import org.fkit.dym.service.DymService;
import org.fkit.dym.util.common.DymConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
/**
* 处理用户请求控制器
* @author
*
*/
@Controller
public class UserController {
//自动注入UserService
@Autowired
@Qualifier("dymService")
private DymService dymService;
/**
* 处理登陆请求
* @param loginname 登录名
* @param password 密码
* @param session 保存user对象
* @param mv 跳转的视图
*/
@RequestMapping(value="/login")
public ModelAndView login(
@RequestParam("loginname") String loginname,
@RequestParam("password") String password,
@RequestParam("flag") Integer flag,
HttpSession session,
ModelAndView mv) {
//调用业务逻辑组件判断用户是否可以登陆
User user=dymService.login(loginname, password);
Integer status=dymService.testpass(loginname);
if(flag==1)
{
if(user!=null) {
//将用户保存到httpSession中
session.setAttribute(DymConstants.USER_SESSION, user);
if(loginname.equals("admin"))
{
//管理员登陆跳转到main页面,客户端跳转
mv.setViewName("redirect:/main");
}
else
{
if(status==1)
{
//普通已审核用户登陆跳转到usermian页面
mv.setViewName("redirect:/usermain");
}
else
{
//普通未审核用户跳转到LoginForm
mv.addObject("message","用户信息未审核,请等待审核通过再登陆");
mv.setViewName("forward:/LoginForm");
}
}
}else {
//设置登陆失败提示信息
mv.addObject("message","用户未注册或者登录名、密码错误,请重新输入");
//服务器内部跳转到登陆页面
mv.setViewName("forward:/LoginForm");
}
}else {
mv.addObject("message","验证码错误!");
mv.setViewName("forward:/LoginForm");
}
return mv;
}
/**
* 处理用户注册信息
* @param loginname
* @param password
* @param realname
* @param realclass
* @param phonenum
* @param mv
* @return
*/
@RequestMapping(value="/register")
public ModelAndView register(
@RequestParam("loginname") String loginname,
@RequestParam("password") String password,
@RequestParam("realname") String realname,
@RequestParam("realclass") String realclass,
@RequestParam("phonenum") String phonenum,
ModelAndView mv) {
dymService.register(loginname,password,realname,realclass,phonenum);
mv.setViewName("redirect:/LoginForm");
return mv;
}
}
(3)UserDao.java
package org.fkit.dym.dao;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.fkit.dym.domain.User;
import static org.fkit.dym.util.common.DymConstants.USERTABLE;
/**
* UserMapper接口
* @author
*
*/
public interface UserDao {
//根据登录名和密码查询用户是否注册
@Select("select * from "+USERTABLE+" where loginname=#{loginname} and password = #{password}")
User selectByLoginnameAndPassword(
@Param("loginname") String loginname,
@Param("password") String password);
//根据员工注册的信息将员工信息插入到数据库表user_inf中
@Insert("insert into "+USERTABLE+"(loginname,password,realname,realclass,phonenum) values(#{loginname},#{password},#{realname},#{realclass},#{phonenum})")
void insertByall(@Param("loginname") String loginname,
@Param("password") String password,
@Param("realname") String realname,
@Param("realclass") String realclass,
@Param("phonenum") String phonenum);
//根据用户名查询已注册用户是否审核通过
@Select("select userstatus from "+USERTABLE+" where loginname=#{loginname}")
Integer selectByLoginnametostatus(@Param("loginname") String loginname);
}
(4)User.java
package org.fkit.dym.domain;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
private static final long serialVersionUID=1L;
private Integer id;
private String loginname;
private String password;
private Date createdate;
private String realname;
private String realclass;
private String phonenum;
private Integer userstatus;
//无参数构造器
public User() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname;
}
public Date getCreatedate() {
return createdate;
}
public void setCreatedate(Date createdate) {
this.createdate = createdate;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public String getRealclass() {
return realclass;
}
public void setRealclass(String realclass) {
this.realclass = realclass;
}
public String getPhonenum() {
return phonenum;
}
public void setPhonenum(String phonenum) {
this.phonenum = phonenum;
}
public Integer getUserstatus() {
return userstatus;
}
public void setUserstatus(Integer userstatus) {
this.userstatus = userstatus;
}
}
(5)AuthorizedInterceptor.java
package org.fkit.dym.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.fkit.dym.domain.User;
import org.fkit.dym.util.common.DymConstants;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
/**
* 判断用户权限的Spring MVC的拦截器
*/
public class AuthorizedInterceptor implements HandlerInterceptor {
/** 定义不需要拦截的请求 */
private static final String[] IGNORE_URI = {"/LoginForm", "/login","/404.html","registeForm"};
/**
* 该方法需要preHandle方法的返回值为true时才会执行。
* 该方法将在整个请求完成之后执行,主要作用是用于清理资源。
*/
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception exception)
throws Exception {
}
/**
* 这个方法在preHandle方法返回值为true的时候才会执行。
* 执行时间是在处理器进行处理之 后,也就是在Controller的方法调用之后执行。
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object handler, ModelAndView mv) throws Exception {
}
/**
* preHandle方法是进行处理器拦截用的,该方法将在Controller处理之前进行调用,
* 当preHandle的返回值为false的时候整个请求就结束了。
* 如果preHandle的返回值为true,则会继续执行postHandle和afterCompletion。
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handler) throws Exception {
/** 默认用户没有登录 */
boolean flag = false;
/** 获得请求的ServletPath */
String servletPath = request.getServletPath();
/** 判断请求是否需要拦截 */
for (String s : IGNORE_URI) {
if (servletPath.contains(s)) {
flag = true;
break;
}
}
/** 拦截请求 */
if (!flag){
/** 1.获取session中的用户 */
User user = (User) request.getSession().getAttribute(DymConstants.USER_SESSION);
/** 2.判断用户是否已经登录 */
if(user == null){
/** 如果用户没有登录,跳转到登录页面 */
request.setAttribute("message", "请先登录再访问网站!");
request.getRequestDispatcher(DymConstants.LOGIN).forward(request, response);
return flag;
}else{
flag = true;
}
}
return flag;
}
}
(6)DymService.java
package org.fkit.dym.service;
import org.fkit.dym.domain.User;
/**
* @Description:服务层接口
* @author
*
*/
public interface DymService {
/**
* 用户登陆
* @param loginname
* @param password
* @return User对象
*/
User login(String loginname,String password);
/**
* 用户注册
* @param loginname
* @param password
* @param realname
* @param realclass
* @param phonenum
* @return
*/
void register(String loginname, String password, String realname, String realclass, String phonenum);
/**
* 判断用户是否审核通过
* @param loginname
* @return
*/
Integer testpass(String loginname);
}
(7)DymServiceImpl.java
package org.fkit.dym.service.impl;
import org.fkit.dym.dao.UserDao;
import org.fkit.dym.domain.User;
import org.fkit.dym.service.DymService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @Description:服务层接口实现类
* @author
*
*/
@Service("dymService")
public class DymServiceImpl implements DymService {
//自动注入持久层dao对象
@Autowired
private UserDao userDao;
/**
* DymServiceImpl接口login方法实现
* 通过登录名和密码来查看用户是否已注册,返回值为user对象
*/
@Override
public User login(String loginname, String password) {
// TODO Auto-generated method stub
return userDao.selectByLoginnameAndPassword(loginname, password);
}
/**
* DymServiceImpl接口testpass方法实现
* 通过登录名查看已注册的用户是否审核通过,范围之为userstatus的值,为1则审核通过,2审核为通过
*/
@Override
public Integer testpass(String loginname) {
// TODO Auto-generated method stub
return userDao.selectByLoginnametostatus(loginname);
}
/**
* DymServiceImpl接口register方法实现
* 通过用户注册的信息将信息插入到数据库中
*/
@Override
public void register(String loginname, String password, String realname, String realclass, String phonenum) {
// TODO Auto-generated method stub
userDao.insertByall(loginname,password,realname,realclass,phonenum);
}
}
(8)DymConstants.java
package org.fkit.dym.util.common;
public class DymConstants {
//数据库表常量
public static final String USERTABLE="user_inf";
//登陆以及用户的session对象
public static final String LOGIN="LoginForm";
public static final String USER_SESSION="user_session";
}
(9)db.properties
dataSource.driverClass=org.gjt.mm.mysql.Driver
dataSource.jdbcUrl=jdbc:mysql://127.0.0.1:3306/dymapp?useSSL=false
dataSource.user=你的数据库名
dataSource.password=你的数据库密码
dataSource.maxPoolSize=20
dataSource.maxIdleTime = 1000
dataSource.minPoolSize=6
dataSource.initialPoolSize=5
(10)LoginForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
电脑医院信息管理与预约系统
${requestScope.message}
(11)registerForm.jsp(我自己写了个register.js保存在js包中,上面有js包的下载地址)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
User Register
(12)taglib.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
(13)main.jsp和usermain.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
管理员登录成功!!!
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
普通用户登录成功!!
(14)applicationContext.xml
(15)springmvc-config.xml
10485760
UTF-8
(16)web.xml
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
/WEB-INF/applicationContext*.xml
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/springmvc-config.xml
1
springmvc
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
*.jsp
false
true
/WEB-INF/jsp/taglib.jsp
404
/404.html
index.jsp
(17)404.html和index.jsp
电脑医院信息管理与预约系统-资源不存在!
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
(18)数据库(直接从数据库导出的,应该没有错)
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `user_inf`
-- ----------------------------
DROP TABLE IF EXISTS `user_inf`;
CREATE TABLE `user_inf` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`loginname` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`password` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`createdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`realname` varchar(20) NOT NULL,
`realclass` varchar(20) NOT NULL,
`phonenum` varchar(20) NOT NULL,
`userstatus` int(10) NOT NULL DEFAULT '2',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user_inf
-- ----------------------------
INSERT INTO `user_inf` VALUES ('1', 'admin', '123456', '2020-02-09 19:16:27', '范', '计科', '18838256188', '1');
INSERT INTO `user_inf` VALUES ('2', 'test', '123456', '2020-02-11 19:17:11', '范范', '软工', '15936948976', '1');
INSERT INTO `user_inf` VALUES ('3', 'test1', '123456', '2020-02-11 19:17:44', 'fanjunjjie', '信管', '15939845881', '2');
INSERT INTO `user_inf` VALUES ('4', 'test2', '123456', '2020-02-20 11:16:24', '小范', '计科', '18823456789', '2');
INSERT INTO `user_inf` VALUES ('5', 'test3', '123456', '2020-02-28 11:18:23', '公孙砾', '16制药工程', '15933457862', '2');
INSERT INTO `user_inf` VALUES ('6', 'test4', '123456', '2020-03-10 11:19:18', '松江', '17第一临床', '15858952881', '2');