提交注册登录

This commit is contained in:
qiurunze 2019-01-25 19:34:07 +08:00
parent a7315c1fca
commit 7e54491e68
24 changed files with 267 additions and 85 deletions

View File

@ -16,7 +16,18 @@
<version>1.18.2</version>
<scope>provided</scope>
</dependency>
<!-- servlet-api -->
<!--<dependency>-->
<!--<groupId>javax.servlet</groupId>-->
<!--<artifactId>servlet-api</artifactId>-->
<!--<version>2.5</version>-->
<!--<scope>provided</scope>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.springframework</groupId>-->
<!--<artifactId>spring-web</artifactId>-->
<!--<version>4.0.0.RELEASE</version>-->
<!--</dependency>-->
<dependency>
<groupId>com.geekq</groupId>
<artifactId>miaosha-common</artifactId>

View File

@ -6,13 +6,14 @@ import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.io.Serializable;
import java.util.Date;
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class Logininfo {
public class Logininfo implements Serializable {
private Long id;
private String nickname;

View File

@ -2,7 +2,6 @@ package com.geekq.admin.service;
import com.geekq.admin.entity.Logininfo;
import com.geekq.common.utils.resultbean.ResultGeekQ;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
@ -13,7 +12,7 @@ public interface ILogininfoService {
* @param username
* @param password
*/
ResultGeekQ<Boolean> register(String username, String password);
void register(String username, String password);
/**
* 检查是否有重复的用户名
@ -26,7 +25,7 @@ public interface ILogininfoService {
* @param password
* @return
*/
Logininfo login(String name, String password, int userType, String ip);
ResultGeekQ<Logininfo> login(String name, String password, int userType, String ip);
/**
* 是否有管理员

View File

@ -17,10 +17,10 @@
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 引入dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
@ -86,6 +86,11 @@
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>

View File

@ -18,7 +18,10 @@ public interface LogininfoMapper {
int updateByPrimaryKey(Logininfo record);
int getCountByUsername(@Param("username") String username,
int getCountByNickname(@Param("nickname") String nickname,
@Param("userType") int userType);
Logininfo getLoginInfoByNickname(@Param("nickname") String nickname,
@Param("userType") int userType);
Logininfo login(@Param("name") String name,

View File

@ -10,6 +10,11 @@
<result column="state" jdbcType="TINYINT" property="state" />
<result column="admin" property="admin" />
<result column="usertype" property="userType" />
<result column="salt" property="salt" />
<result column="usertype" property="userType" />
<result column="registerDate" property="registerDate" />
<result column="lastLoginDate" property="lastLoginDate" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
@ -19,18 +24,20 @@
</delete>
<insert id="insert" keyProperty="id" parameterType="com.geekq.admin.entity.Logininfo" useGeneratedKeys="true">
insert into logininfo (username,password, state,usertype,admin)
values (#{username,jdbcType=VARCHAR},#{password,jdbcType=VARCHAR},#{state,jdbcType=TINYINT},#{userType},#{admin})
insert into logininfo (nickname,password, state,usertype,admin,salt,registerdate,lastlogindate)
values (#{nickname,jdbcType=VARCHAR},#{password,jdbcType=VARCHAR},#{state,jdbcType=TINYINT},#{userType},#{admin}
,#{salt},#{registerDate},#{lastLoginDate})
</insert>
<update id="updateByPrimaryKey" parameterType="com.geekq.admin.entity.Logininfo">
update logininfo set username = #{username,jdbcType=VARCHAR},password =#{password,jdbcType=VARCHAR},
state = #{state,jdbcType=TINYINT},usertype = #{userType},admin=#{admin}
update logininfo set nickname = #{nickname,jdbcType=VARCHAR},password =#{password,jdbcType=VARCHAR},
state = #{state,jdbcType=TINYINT},usertype = #{userType},admin=#{admin},salt = #{salt},
registerdate = #{registerDate},lastlogindate = #{lastLoginDate}
where id = #{id,jdbcType=BIGINT}
</update>
<sql id="base_column">
id, username, password, state,usertype,admin
id, nickname, password, state,usertype,admin ,salt , registerdate , lastlogindate
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap" useCache="true">
@ -43,17 +50,24 @@
from logininfo
</select>
<select id="getCountByUsername" resultType="int" useCache="false">
select count(id) from logininfo where username=#{username} and usertype = #{userType}
<select id="getCountByNickname" resultType="int" useCache="false">
select count(id) from logininfo where nickname=#{nickname} and usertype = #{userType}
</select>
<select id="getLoginInfoByNickname" resultMap="BaseResultMap" useCache="false">
select <include refid="base_column" />
from logininfo where nickname=#{nickname} and usertype = #{userType}
</select>
<select id="login" resultMap="BaseResultMap" useCache="true">
select <include refid="base_column"/>
from logininfo where username= #{name} and password=#{password} and usertype=#{userType}
from logininfo where nickname= #{name} and password=#{password} and usertype=#{userType}
</select>
<select id="autoComplate" resultType="hashmap" useCache="false">
select id,username as name
from logininfo where username LIKE concat(#{word},'%') AND usertype = #{userType}
select id,nickname as name
from logininfo where nickname LIKE concat(#{word},'%') AND usertype = #{userType}
</select>
</mapper>

View File

@ -1,14 +1,15 @@
package com.geekq.admin.service.impl;
import com.geekq.admin.entity.IpLog;
import com.geekq.admin.entity.Logininfo;
import com.geekq.admin.entity.Userinfo;
import com.geekq.admin.mapper.IpLogMapper;
import com.geekq.admin.mapper.LogininfoMapper;
import com.geekq.admin.mapper.UserinfoMapper;
import com.geekq.admin.service.ILogininfoService;
import com.geekq.admin.utils.UserContext;
import com.geekq.common.enums.Constants;
import com.geekq.common.enums.ResultStatus;
import com.geekq.common.utils.Constanst;
import com.geekq.common.utils.MD5.MD5Utils;
import com.geekq.common.utils.resultbean.ResultGeekQ;
import org.slf4j.Logger;
@ -37,12 +38,10 @@ public class LogininfoServiceImpl implements ILogininfoService {
private UserinfoMapper userinfoMapper;
@Override
public ResultGeekQ<Boolean> register(String username, String password) {
public void register(String username, String password) {
ResultGeekQ<Boolean> resultGeekQ = ResultGeekQ.build();
int count = loginInfoMapper.getCountByUsername(username, Constants.USERTYPE_NORMAL);
if(count > 0) {
try {
int count = loginInfoMapper.getCountByNickname(username, Constants.USERTYPE_NORMAL);
if(count <= 0) {
Logininfo logininfo =new Logininfo();
logininfo.setNickname(username);
//获取随机salt
@ -52,29 +51,44 @@ public class LogininfoServiceImpl implements ILogininfoService {
logininfo.setState(Constants.STATE_NORMAL);
logininfo.setUserType(Constants.USERTYPE_NORMAL);
logininfo.setRegisterDate(new Date());
logininfo.setLastLoginDate(new Date());
logininfo.setSalt(salt);
this.loginInfoMapper.insert(logininfo);
//初始化一个Userinfo
Userinfo userinfo = Userinfo.empty(logininfo.getId());
this.userinfoMapper.insert(userinfo);
} catch (Exception e) {
logger.error("注册失败!",e);
resultGeekQ.withError(ResultStatus.RESIGETER_FAIL);
}
int result = this.userinfoMapper.insert(userinfo);
}else{
resultGeekQ.withError(ResultStatus.RESIGETER_NICKNAMEEXIST);
throw new RuntimeException("用户名已经存在!");
}
return resultGeekQ;
}
@Override
public boolean checkUsername(String name, int userType) {
return this.loginInfoMapper.getCountByUsername(name, userType)<=0;
return this.loginInfoMapper.getCountByNickname(name, userType)<=0;
}
@Override
public Logininfo login(String name, String password, int userType, String ip) {
return null;
public ResultGeekQ<Logininfo> login(String name, String password, int userType, String ip) {
ResultGeekQ<Logininfo> resultGeekQ = ResultGeekQ.build();
try {
IpLog log = new IpLog(name,new Date(),ip,userType,null);
Logininfo logininfo = loginInfoMapper.getLoginInfoByNickname(name,Constants.USERTYPE_NORMAL);
String salt = logininfo.getSalt();
Logininfo current = this.loginInfoMapper.login(name,
MD5Utils.formPassToDBPass(password,salt), userType);
if(current != null){
UserContext.putLogininfo(current);
log.setLoginInfoId(current.getId());
log.setLoginState(IpLog.LOGINSTATE_SUCCESS);
}
ipLogMapper.insert(log);
resultGeekQ.setData(logininfo);
} catch (Exception e) {
logger.error("登录发生错误!",e);
resultGeekQ.withError(ResultStatus.LOGIN_FIAL);
}
return resultGeekQ;
}
@Override

View File

@ -0,0 +1,37 @@
package com.geekq.admin.utils;
import com.geekq.admin.entity.Logininfo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
public class UserContext {
public static final String LOGIN_IN_SESSION = "logininfo";
public static final String VERIFYCODE_IN_SESSION = "VERIFYCODE_IN_SESSION";
private static HttpServletRequest getRequest() {
return ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();
}
public static void putLogininfo(Logininfo logininfo) {
getRequest().getSession().setAttribute(LOGIN_IN_SESSION, logininfo);
}
public static Logininfo getCurrent() {
return (Logininfo) getRequest().getSession().getAttribute(
LOGIN_IN_SESSION);
}
// public static void putVerifyCode(VerifyCode code) {
// getRequest().getSession().setAttribute(VERIFYCODE_IN_SESSION, code);
// }
//
// public static VerifyCode getVerifyCode() {
// return (VerifyCode) getRequest().getSession().getAttribute(
// VERIFYCODE_IN_SESSION);
// }
}

View File

@ -1,4 +1,4 @@
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/miaosha?characterEncoding=utf-8
jdbc.url=jdbc:mysql://localhost:3306/miaosha2?characterEncoding=utf-8
jdbc.username=root
jdbc.password=nihaoma

View File

@ -6,19 +6,19 @@
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 为当前服务提供者取个名字,并且提供给注册中心 -->
<dubbo:application name="imooc-dubbo-order-service"></dubbo:application>
<dubbo:application name="miaosha-service"></dubbo:application>
<!-- 注册中心的配置使用zk暴露服务 -->
<dubbo:registry protocol="zookeeper" address="39.107.245.253:2181"></dubbo:registry>
<dubbo:registry protocol="zookeeper" address="localhost:2181"></dubbo:registry>
<!-- 定义暴露服务的端口号 -->
<dubbo:protocol name="dubbo" port="20881" ></dubbo:protocol>
<!-- 暴露具体的服务接口 -->
<dubbo:service retries="3" interface="com.geekq.admin.service.OrdersService"
ref="ordersService" timeout="6000"></dubbo:service>
ref="ordersService" timeout="60000"></dubbo:service>
<dubbo:service retries="3" interface="com.geekq.admin.service.ILogininfoService"
ref="logininfoServiceImpl" timeout="6000"></dubbo:service>
ref="logininfoServiceImpl" timeout="60000"></dubbo:service>
</beans>

View File

@ -31,12 +31,7 @@
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>

View File

@ -1,13 +1,18 @@
package com.geekq.web.controller;
import com.geekq.admin.entity.Logininfo;
import com.geekq.admin.service.ILogininfoService;
import com.geekq.common.enums.Constants;
import com.geekq.common.utils.resultbean.ResultGeekQ;
import com.geekq.common.vo.LoginVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
@ -20,11 +25,20 @@ public class LoginController extends BaseController {
private static Logger logger = LoggerFactory.getLogger(LoginController.class);
@Autowired
private ILogininfoService iLogininfoService;
@RequestMapping("/login" )
@ResponseBody
public ResultGeekQ<Boolean> dologin(HttpServletResponse response, LoginVo loginVo) {
public ResultGeekQ<Boolean> dologin(HttpServletResponse response,
HttpServletRequest request,
String username, String password) {
ResultGeekQ<Boolean> result = ResultGeekQ.build();
logger.info(loginVo.toString());
ResultGeekQ<Logininfo> login = this.iLogininfoService.login(username, password,
Constants.USERTYPE_NORMAL,request.getRemoteAddr());
if(ResultGeekQ.isSuccess(login)){
result.withError(login.getCode(),login.getMessage());
}
return result;
}
}

View File

@ -1,7 +1,9 @@
package com.geekq.web.controller;
import com.geekq.web.interceptor.RequiredLogin;
import com.geekq.web.service.CulsterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@ -14,6 +16,7 @@ public class PayController {
@Autowired
private CulsterService buyService;
@RequiredLogin
@RequestMapping("/index")
public String index() {
return "login";

View File

@ -4,6 +4,7 @@ import com.geekq.admin.service.ILogininfoService;
import com.geekq.common.enums.Constants;
import com.geekq.common.enums.ResultStatus;
import com.geekq.common.utils.resultbean.ResultGeekQ;
import com.geekq.common.utils.resultbean.ResultJSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -24,12 +25,16 @@ public class RegisterController extends BaseController{
@RequestMapping("/register")
@ResponseBody
public ResultGeekQ<Boolean> register(String username, String password) {
/**
* 登录注册
*/
return logininfoService.register(username,password);
public ResultJSON register(String username, String password) {
ResultJSON json = new ResultJSON();
try {
this.logininfoService.register(username, password);
json.setSuccess(true);
} catch (RuntimeException e) {
logger.error("注册失败",e);
json.setMsg("注册失败,请联系相关人员!");
}
return json;
}
@RequestMapping("/checkUsername")

View File

@ -1,32 +1,29 @@
package com.geekq.web.interceptor;
import com.geekq.web.utils.UserContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 邱润泽
* 拦截器 拦截至主页面
*/
public class LoginInterceptor extends HandlerInterceptorAdapter {
// @Override
// public boolean preHandle(HttpServletRequest request,
// HttpServletResponse response, Object handler) throws Exception {
// if (handler instanceof HandlerMethod) {
// HandlerMethod hm = (HandlerMethod) handler;
// RequiredLogin rl = hm.getMethodAnnotation(RequiredLogin.class);
// if (rl != null) {
//// if (request.getSession().getAttribute(
//// UserContext.LOGIN_IN_SESSION) == null) {
// response.sendRedirect("/login.html");
// return false;
// }
//// }
// }
// return super.preHandle(request, response, handler);
// }
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod hm = (HandlerMethod) handler;
RequiredLogin rl = hm.getMethodAnnotation(RequiredLogin.class);
if (rl != null) {
if (request.getSession().getAttribute(
UserContext.LOGIN_IN_SESSION) == null) {
response.sendRedirect("/login.html");
return false;
}
}
}
return super.preHandle(request, response, handler);
}
}

View File

@ -0,0 +1,37 @@
package com.geekq.web.utils;
import com.geekq.admin.entity.Logininfo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
public class UserContext {
public static final String LOGIN_IN_SESSION = "logininfo";
public static final String VERIFYCODE_IN_SESSION = "VERIFYCODE_IN_SESSION";
private static HttpServletRequest getRequest() {
return ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();
}
public static void putLogininfo(Logininfo logininfo) {
getRequest().getSession().setAttribute(LOGIN_IN_SESSION, logininfo);
}
public static Logininfo getCurrent() {
return (Logininfo) getRequest().getSession().getAttribute(
LOGIN_IN_SESSION);
}
// public static void putVerifyCode(VerifyCode code) {
// getRequest().getSession().setAttribute(VERIFYCODE_IN_SESSION, code);
// }
//
// public static VerifyCode getVerifyCode() {
// return (VerifyCode) getRequest().getSession().getAttribute(
// VERIFYCODE_IN_SESSION);
// }
}

View File

@ -6,10 +6,10 @@
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 为当前服务提供者取个名字,并且提供给注册中心 -->
<dubbo:application name="imooc-dubbo-web"></dubbo:application>
<dubbo:application name="miaosha-dubbo-web"></dubbo:application>
<!-- 注册中心的配置,用于消费者的监听 -->
<dubbo:registry protocol="zookeeper" address="39.107.245.253:2181"></dubbo:registry>
<dubbo:registry protocol="zookeeper" address="localhost:2181"></dubbo:registry>
<!--&lt;!&ndash; 监听服务,通过注册中心去进行查找,查找到后进行服务调用 &ndash;&gt;-->
<!--<dubbo:reference id="itemService" interface="com.imooc.item.service.ItemsService"-->

View File

@ -42,7 +42,7 @@
//验证成功后提交操作
submitHandler:function(form){
$(form).ajaxSubmit(function(data){
if(data.success){
if(data.status='success'){
$.messager.confirm("提示","登陆成功,点击确定跳转到个人中心",function(){
window.location.href="/personal.do";
});
@ -91,7 +91,7 @@
<div class="form-group">
<label class="control-label col-sm-2">手机号码</label>
<div class="col-sm-10">
<input type="text" autocomplete="off" name="mobile" class="form-control" value="stef"/>
<input type="text" autocomplete="off" name="username" class="form-control" value="stef"/>
</div>
</div>
<div class="form-group">

View File

@ -49,7 +49,7 @@
data:{username:value},
async:false,
success:function(data){
ret= data.success;
ret= data.data;
}
});
return ret;
@ -85,7 +85,7 @@
},
submitHandler:function(form){
$(form).ajaxSubmit(function(data){
if(data.success){
if(data.data){
$.messager.confirm("提示","注册成功,请重新登录系统!",function(){
window.location.href="/login.html";
});
@ -121,7 +121,7 @@
<div class="navbar navbar-default el-navbar">
<div class="container">
<div class="navbar-header">
<a href=""><img alt="Brand" src="/images/logo.png"></a>
<a href=""><img alt="Brand" src="/images/miaoshaxitong.png"></a>
<span class="el-page-title">用户注册</span>
</div>
</div>

View File

@ -32,6 +32,8 @@ public enum ResultStatus {
RESIGETER_FAIL(200001,"注册失败!"),
CODE_FAIL(200002,"验证码不一致!"),
RESIGETER_NICKNAMEEXIST(200003,"用户名已经存在!"),
LOGIN_FIAL(200004,"登录失败!"),
/**
* check

View File

@ -8,6 +8,7 @@ public class AbstractResult {
private int code;
private String message;
AbstractResult() {}
protected AbstractResult(ResultStatus status, String message) {
this.code = status.getCode();
this.status = status;

View File

@ -1,14 +1,16 @@
package com.geekq.common.utils.resultbean;
import com.geekq.common.enums.ResultStatus;
import java.io.Serializable;
public class ResultGeekQ<T> extends AbstractResult implements Serializable {
private static final long serialVersionUID = 867933019328199779L;
private T data;
private Integer count;
protected ResultGeekQ() {
}
protected ResultGeekQ(ResultStatus status, String message) {
super(status, message);
}
@ -16,7 +18,7 @@ public class ResultGeekQ<T> extends AbstractResult implements Serializable {
super(status);
}
public static <T> ResultGeekQ<T> build() {
return new ResultGeekQ(ResultStatus.SUCCESS, (String)null);
return new ResultGeekQ(ResultStatus.SUCCESS, "构造函数");
}
public static <T> ResultGeekQ<T> build(String message) {

View File

@ -0,0 +1,30 @@
package com.geekq.common.utils.resultbean;
import lombok.Getter;
import lombok.Setter;
/**
* @author 邱润泽
*/
@Getter
@Setter
public class ResultJSON {
private Boolean success = false;
private String msg;
public ResultJSON() {
super();
}
public ResultJSON(Boolean success, String msg) {
super();
this.success = success;
this.msg = msg;
}
public ResultJSON(String msg) {
super();
this.msg = msg;
}
}

View File

@ -30,7 +30,7 @@
<!-- 使用redisTamplate结合jedisPoolConifg 必须使用jedis版本2.9.0 -->
<jedis.version>2.9.0</jedis.version>
<commons.httpclient.version>3.1</commons.httpclient.version>
<dubbo.version>2.5.3</dubbo.version>
<dubbo.version>2.6.0</dubbo.version>
<zookeeper.version>3.4.11</zookeeper.version>
<zkclient.version>0.1</zkclient.version>
<apache.curator.version>4.0.0</apache.curator.version>
@ -145,6 +145,12 @@
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework</groupId>-->
<!--<artifactId>spring-web</artifactId>-->
<!--<version>4.0.0.RELEASE</version>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
@ -215,6 +221,12 @@
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>