登出,註冊,校驗功能開發
阿新 • • 發佈:2018-01-08
用戶 登出 think logout isn 實現 ssi keys response
Ctrl+T:進入實現類
<?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.mmall.dao.UserMapper" > <resultMap id="BaseResultMap" type="com.mmall.pojo.User" > <constructor> <idArg column="id" jdbcType="INTEGER" javaType="java.lang.Integer" /> <arg column="username" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="password" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="email" jdbcType="VARCHAR"javaType="java.lang.String" /> <arg column="phone" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="question" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="answer" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="role" jdbcType="INTEGER" javaType="java.lang.Integer" /> <arg column="create_time" jdbcType="TIMESTAMP" javaType="java.util.Date" /> <arg column="update_time" jdbcType="TIMESTAMP" javaType="java.util.Date" /> </constructor> </resultMap> <sql id="Base_Column_List" > id, username, password, email, phone, question, answer, role, create_time, update_time </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from mmall_user where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from mmall_user where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.mmall.pojo.User" > insert into mmall_user (id, username, password, email, phone, question, answer, role, create_time, update_time) values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{question,jdbcType=VARCHAR}, #{answer,jdbcType=VARCHAR}, #{role,jdbcType=INTEGER}, now(), now()) </insert> <insert id="insertSelective" parameterType="com.mmall.pojo.User" > insert into mmall_user <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="username != null" > username, </if> <if test="password != null" > password, </if> <if test="email != null" > email, </if> <if test="phone != null" > phone, </if> <if test="question != null" > question, </if> <if test="answer != null" > answer, </if> <if test="role != null" > role, </if> <if test="createTime != null" > create_time, </if> <if test="updateTime != null" > update_time, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=INTEGER}, </if> <if test="username != null" > #{username,jdbcType=VARCHAR}, </if> <if test="password != null" > #{password,jdbcType=VARCHAR}, </if> <if test="email != null" > #{email,jdbcType=VARCHAR}, </if> <if test="phone != null" > #{phone,jdbcType=VARCHAR}, </if> <if test="question != null" > #{question,jdbcType=VARCHAR}, </if> <if test="answer != null" > #{answer,jdbcType=VARCHAR}, </if> <if test="role != null" > #{role,jdbcType=INTEGER}, </if> <if test="createTime != null" > now(), </if> <if test="updateTime != null" > now(), </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.mmall.pojo.User" > update mmall_user <set > <if test="username != null" > username = #{username,jdbcType=VARCHAR}, </if> <if test="password != null" > password = #{password,jdbcType=VARCHAR}, </if> <if test="email != null" > email = #{email,jdbcType=VARCHAR}, </if> <if test="phone != null" > phone = #{phone,jdbcType=VARCHAR}, </if> <if test="question != null" > question = #{question,jdbcType=VARCHAR}, </if> <if test="answer != null" > answer = #{answer,jdbcType=VARCHAR}, </if> <if test="role != null" > role = #{role,jdbcType=INTEGER}, </if> <if test="createTime != null" > create_time = #{createTime,jdbcType=TIMESTAMP}, </if> <if test="updateTime != null" > update_time = now(), </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.mmall.pojo.User" > update mmall_user set username = #{username,jdbcType=VARCHAR}, password = #{password,jdbcType=VARCHAR}, email = #{email,jdbcType=VARCHAR}, phone = #{phone,jdbcType=VARCHAR}, question = #{question,jdbcType=VARCHAR}, answer = #{answer,jdbcType=VARCHAR}, role = #{role,jdbcType=INTEGER}, create_time = #{createTime,jdbcType=TIMESTAMP}, update_time = now() where id = #{id,jdbcType=INTEGER} </update> <select id="checkUsername" resultType="int" parameterType="string"> select count(1) from mmall_user where username=#{username} </select> <select id="selectLogin" resultMap="BaseResultMap" parameterType="map"> SELECT -- *???//這樣真的好麽?答案就是,這樣不好. <include refid="Base_Column_List" /> from mmall_user where username = #{username} and password = #{password} </select> <select id="checkEmail" resultType="int" parameterType="string" > select count(1) from mmall_user where email = #{email} </select> </mapper>
package com.mmall.controller.portal; import com.mmall.common.Const; import com.mmall.common.ServerResponse; import com.mmall.pojo.User; import com.mmall.service.IUserService; 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.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpSession; /** * Created by think on 2018/1/8. */ @Controller @RequestMapping("/user/") public class UserController { @Autowired private IUserService iUserService; /** * @param username * @param password * @param session * @return */ @RequestMapping(value = "login.do", method = RequestMethod.POST) @ResponseBody public ServerResponse<User> login(String username, String password, HttpSession session) { ServerResponse<User> response = iUserService.login(username, password); if (response.isSuccess()) { session.setAttribute(Const.CURRENT_USER, response.getData()); } return response; } @RequestMapping(value = "logout.do", method = RequestMethod.POST) @ResponseBody public ServerResponse<String> logout(HttpSession session) { session.removeAttribute(Const.CURRENT_USER); return ServerResponse.createBySuccess(); } @RequestMapping(value = "register.do", method = RequestMethod.POST) @ResponseBody public ServerResponse<String> register(User user) { return iUserService.register(user); } @RequestMapping(value = "check_valid.do", method = RequestMethod.POST) @ResponseBody public ServerResponse<String> checkValid(String str, String type) { return iUserService.checkValid(str, type); } }
package com.mmall.service; import com.mmall.common.ServerResponse; import com.mmall.pojo.User; /** * Created by think on 2018/1/8. */ public interface IUserService { ServerResponse<User> login(String username, String password); ServerResponse<String> register(User user); ServerResponse<String> checkValid(String str,String type) }
package com.mmall.service.impl; import com.mmall.common.Const; import com.mmall.common.ServerResponse; import com.mmall.dao.UserMapper; import com.mmall.pojo.User; import com.mmall.service.IUserService; import com.mmall.util.MD5Util; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * Created by think on 2018/1/8. */ @Service("iUserService") public class UserServiceImpl implements IUserService { @Autowired private UserMapper userMapper; @Override public ServerResponse<User> login(String username, String password) { int resultCount = userMapper.checkUsername(username); if(resultCount == 0 ){ return ServerResponse.createByErrorMessage("用戶名不存在"); } String md5Password = MD5Util.MD5EncodeUtf8(password); User user = userMapper.selectLogin(username,md5Password); if(user == null){ return ServerResponse.createByErrorMessage("密碼錯誤"); } user.setPassword(org.apache.commons.lang3.StringUtils.EMPTY); return ServerResponse.createBySuccess("登錄成功",user); } public ServerResponse<String> register(User user){ ServerResponse validResponse = this.checkValid(user.getUsername(),Const.USERNAME); if(!validResponse.isSuccess()){ return validResponse; } validResponse = this.checkValid(user.getEmail(),Const.EMAIL); if(!validResponse.isSuccess()){ return validResponse; } user.setRole(Const.Role.ROLE_CUSTOMER); //MD5加密 user.setPassword(MD5Util.MD5EncodeUtf8(user.getPassword())); int resultCount = userMapper.insert(user); if(resultCount == 0){ return ServerResponse.createByErrorMessage("註冊失敗"); } return ServerResponse.createBySuccessMessage("註冊成功"); } public ServerResponse<String> checkValid(String str,String type){ if(org.apache.commons.lang3.StringUtils.isNotBlank(type)){ //開始校驗 if(Const.USERNAME.equals(type)){ int resultCount = userMapper.checkUsername(str); if(resultCount > 0 ){ return ServerResponse.createByErrorMessage("用戶名已存在"); } } if(Const.EMAIL.equals(type)){ int resultCount = userMapper.checkEmail(str); if(resultCount > 0 ){ return ServerResponse.createByErrorMessage("email已存在"); } } }else{ return ServerResponse.createByErrorMessage("參數錯誤"); } return ServerResponse.createBySuccessMessage("校驗成功"); } }
package com.mmall.util; import org.springframework.util.StringUtils; import java.security.MessageDigest; /** * Created by geely */ public class MD5Util { private static String byteArrayToHexString(byte b[]) { StringBuffer resultSb = new StringBuffer(); for (int i = 0; i < b.length; i++) resultSb.append(byteToHexString(b[i])); return resultSb.toString(); } private static String byteToHexString(byte b) { int n = b; if (n < 0) n += 256; int d1 = n / 16; int d2 = n % 16; return hexDigits[d1] + hexDigits[d2]; } /** * 返回大寫MD5 * * @param origin * @param charsetname * @return */ private static String MD5Encode(String origin, String charsetname) { String resultString = null; try { resultString = new String(origin); MessageDigest md = MessageDigest.getInstance("MD5"); if (charsetname == null || "".equals(charsetname)) resultString = byteArrayToHexString(md.digest(resultString.getBytes())); else resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname))); } catch (Exception exception) { } return resultString.toUpperCase(); } public static String MD5EncodeUtf8(String origin) { return MD5Encode(origin, "utf-8"); } private static final String hexDigits[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"}; }
package com.mmall.common; /** * Created by geely */ public class Const { public static final String CURRENT_USER = "currentUser"; public static final String EMAIL = "email"; public static final String USERNAME = "username"; public interface Role{ int ROLE_CUSTOMER = 0; //普通用戶 int ROLE_ADMIN = 1;//管理員 } }
package com.mmall.dao; import com.mmall.pojo.User; import org.apache.ibatis.annotations.Param; public interface UserMapper { int deleteByPrimaryKey(Integer id); int insert(User record); int insertSelective(User record); User selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record); int checkUsername(String username); User selectLogin(@Param("username") String username, @Param("password")String password); int checkEmail(String email); }
登出,註冊,校驗功能開發