Skip to content

Commit

Permalink
提交注册登录
Browse files Browse the repository at this point in the history
  • Loading branch information
qiurunze committed Jan 25, 2019
1 parent a7315c1 commit 7e54491
Show file tree
Hide file tree
Showing 24 changed files with 267 additions and 85 deletions.
13 changes: 12 additions & 1 deletion miaosha-admin/miaosha-admin-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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;
Expand All @@ -13,7 +12,7 @@ public interface ILogininfoService {
* @param username
* @param password
*/
ResultGeekQ<Boolean> register(String username, String password);
void register(String username, String password);

/**
* 检查是否有重复的用户名
Expand All @@ -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);

/**
* 是否有管理员
Expand Down
7 changes: 6 additions & 1 deletion miaosha-admin/miaosha-admin-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ 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,
@Param("password") String password, @Param("userType") int userType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand All @@ -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">
Expand All @@ -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>
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -52,29 +51,44 @@ public ResultGeekQ<Boolean> register(String username, String password) {
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
// }
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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: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>
7 changes: 1 addition & 6 deletions miaosha-admin/miaosha-admin-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
}
}
Loading

0 comments on commit 7e54491

Please sign in to comment.