ssm当用户登录成功显示用户名_从零到企业级SSM电商项目实战教程(十八)用户登录功能开发...
用户模块功能介绍
1、登录
2、用户名验证
3、注册
4、忘记密码
5、提交问题答案
6、重置密码
7、获取用户信息
8、更新用户信息
9、退出登录
学习目标
1、理解横向越权、纵向越权安全漏洞
2、MD5明文加密机增加salt值
3、Guava缓存的使用
4、高复用服务响应对象的设计思想及抽象分装
5、session的使用
6、方法局部演进
横向越权、纵向越权安全漏洞
横向越权:攻击者尝试访问与他拥有相同权限的用户的资源
纵向越权:低级别攻击者尝试访问高级别用户的资源
完成用户登录功能
controller包下新建一个包portal,门户的意思,给前端用的。然后在里面创建一个类UserController
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;
/**
* @Author: Geekerstar(jikewenku.com)
* @Date: 2018/6/22 9:04
* @Description:
*/
@Controller
@RequestMapping("/user/")
public class UserController {
@Autowired
private IUserService iUserService;
/*
* @Description: 用户登录
*
* @auther: Geekerstar(jikewenku.com)
* @date: 2018/6/22 9:28
* @param: [username, password, session]
* @return: java.lang.Object
*/
@RequestMapping(value = "login.do",method = RequestMethod.POST)
@ResponseBody
public ServerResponselogin(String username, String password, HttpSession session){
//service-->mybatis-->dao
ServerResponseresponse = iUserService.login(username,password);
if(response.isSuccess()){
session.setAttribute(Const.CURRENT_USER,response.getData());
}
return response;
}
}
service包下创建IUserService,创建一个包impl存放它的实现类
IUserService代码如下:
package com.mmall.service;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
/**
* @Author: Geekerstar(jikewenku.com)
* @Date: 2018/6/22 9:32
* @Description:
*/
public interface IUserService {
ServerResponselogin(String username, String password);
}
实现类UserServiceImpl代码如下
package com.mmall.service.impl;
import com.mmall.common.ServerResponse;
import com.mmall.dao.UserMapper;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @Author: Geekerstar(jikewenku.com)
* @Date: 2018/6/22 9:34
* @Description:
*/
@Service("iUserService")
public class UserServiceImpl implements IUserService{
@Autowired
private UserMapper userMapper;
@Override
public ServerResponselogin(String username, String password){
int resultCount = userMapper.checkUsername(username);
if(resultCount == 0){
return ServerResponse.createByErrorMessgae("用户名不存在");
}
//密码登录MD5
User user = userMapper.selectLogin(username,password);
if(user == null){
return ServerResponse.createByErrorMessgae("密码错误");
}
user.setPassword(StringUtils.EMPTY);
return ServerResponse.createBySuccess("登录成功",user);
}
}
接下来完成一个通用的响应对象,common里新建一个类ServerResponse
package com.mmall.common;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import java.io.Serializable;
/**
* @Author: Geekerstar(jikewenku.com)
* @Date: 2018/6/22 9:36
* @Description:
*/
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
//保证序列化JSon的时候,如果是null的对象,可以也会消失
public class ServerResponseimplements Serializable{
private int status;
private String msg;
private T data;
private ServerResponse(int status){
this.status = status;
}
private ServerResponse(int status,T data){
this.status = status;
this.data = data;
}
private ServerResponse(int status,String msg,T data){
this.status = status;
this.msg = msg;
this.data = data;
}
private ServerResponse(int status,String msg){
this.status = status;
this.msg = msg;
}
@JsonIgnore
//使之不在json序列化结果当中
public boolean isSuccess(){
return this.status == ResponseCode.SUCCESS.getCode();
}
public int getStatus(){
return status;
}
public T getData(){
return data;
}
public String getMsg(){
return msg;
}
public static ServerResponsecreateBySuccess(){
return new ServerResponse(ResponseCode.SUCCESS.getCode());
}
public static ServerResponsecreateBySuccessMessage(String msg){
return new ServerResponse(ResponseCode.SUCCESS.getCode(),msg);
}
public static ServerResponsecreateBySuccess(T data){
return new ServerResponse(ResponseCode.SUCCESS.getCode(),data);
}
public static ServerResponsecreateBySuccess(String msg,T data){
return new ServerResponse(ResponseCode.SUCCESS.getCode(),msg,data);
}
public static ServerResponsecreateByError(){
return new ServerResponse(ResponseCode.ERROR.getCode(),ResponseCode.ERROR.getDesc());
}
public static ServerResponsecreateByErrorMessgae(String errorMesage){
return new ServerResponse(ResponseCode.ERROR.getCode(),errorMesage);
}
public static ServerResponsecreateByErrorCodeMessgae(int errorCode,String errorMesage){
return new ServerResponse(errorCode,errorMesage);
}
}
common包下新建一个ResponseCode
package com.mmall.common;
/**
* @Author: Geekerstar(jikewenku.com)
* @Date: 2018/6/22 9:43
* @Description:
*/
public enum ResponseCode {
SUCCESS(0,"SUCCESS"),
ERROR(1,"ERROR"),
NEED_LOGIN(10,"NEED_LOGIN"),
ILLEGAL_ARGUMENT(2,"ILLEGAL_ARGUMENT");
private final int code;
private final String desc;
ResponseCode(int code,String desc){
this.code = code;
this.desc = desc;
}
public int getCode(){
return code;
}
public String getDesc(){
return desc;
}
}
common包下新建一个Const
package com.mmall.common;
/**
* @Author: Geekerstar(jikewenku.com)
* @Date: 2018/6/22 10:33
* @Description:
*/
public class Const {
public static final String CURRENT_USER = "currentUser";
}
UserMapper下新增
int checkUsername(String username);
User selectLogin(@Param("username") String username, @Param("password") String password);
UserMapper.xml里编写SQL
select count(1) from mmall_user
where username = #{username}
SELECT
-- *???//这样真的好么?答案就是,这样不好.要什么查什么。
from mmall_user
where username = #{username}
and password = #{password}
说明
以上代码不是一蹴而就的,不是写完一个文件再写另一个,而是交叉开发的。
但是为了展示方便,所以按照文件来呈现代码,没有按照先后顺序来呈现,望周知。
每个章节的代码都放到GitHub上了,希望参考后顺便给个star。
整个教程完成之后我会再做一次优化,做出部分调整完善。
有任何疑问请在下面留言。
ssm当用户登录成功显示用户名_从零到企业级SSM电商项目实战教程(十八)用户登录功能开发...相关推荐
- 《果然新鲜》电商项目(36)-SSO单点登录(集成SSO认证服务)
文章目录 引言 1. 集成xxl-sso-core 2. 集成xxl-server 总结 引言 在上一篇博客<果然新鲜电商项目(35)-SSO单点登录(XXL-SSO案例)>,主要讲解了S ...
- 《果然新鲜》电商项目(25)- 会员唯一登录
文章目录 引言 1.什么是唯一登录? 2.会员唯一登录的实现思路 3. 功能实现 3.1 数据库设计 3.2 代码实现 3.2.1 用户登录 3.2.2 获取用户信息 4. 测试 4.1 三端唯一登录 ...
- 电商项目实战--用户相关
1. 项目分析 首先应该分析该项目中需要处理哪些种类的数据,在本项目中有:商品.商品类别.用户.收货地址.购物车.收藏.订单-- 然后,确定以上这些数据的开发顺序,原则上应该先开发基础数据和简单的数据 ...
- 尚硅谷2020微服务分布式电商项目《谷粒商城》-单点登录(jwt)
学习更多的知识,整理不易,拒绝白嫖,记得三连哦 关注公众号:java星星 获取全套课件资料 1. 用户管理提供数据接口 1.1. 数据验证功能 根据接口文档知: 请求方式:GET 请求路径:check ...
- 测试从零开始-电商项目实战-用例设计篇No.1-[后台-用户列表]
在之前的文章中,已经教大家搭建了电商网站,如果有没找到的,可以再私聊一下我.接下来,简单介绍一下,在公司中,一般我们怎么去开展测试. 这里我们抛开所有的理想状态,就只有这个项目的访问地址,数据库信息, ...
- django和mysql写注册_Django电商项目---完成注册页面和用户登录
完成基本的创建项目.用户注册.登录.注销功能创建Django项目,创建df_user的App 创建静态文件夹static(跟manage.py保持在同一级别下) 复制静态文件(css + images ...
- 【电商项目实战】用户登录(详细篇)
- 淘东电商项目(35) -SSO单点登录(登录功能完善)
引言 本文代码已提交至Github(版本号:725238a1d0c829ee28cdef0ffe49e5f1c0020a2b),有兴趣的同学可以下载来看看:https://github.com/ylw ...
- 实现用户的登录,并且登录后显示用户名
利用servlet,jsp实现用户的登录,并且登录后显示用户名(连接数据库) 显示用户名这里是通过session传递数据,登陆成功后点击退出时要清除页面session. 使用软件:tomcat-8.5 ...
最新文章
- 什么叫做“假学习”?
- 介绍Python的魔术方法 - Magic Method
- URL URI傻傻分不清楚,dart告诉你该怎么用
- 555定时器,你知道它的功能有多强大吗?
- bzoj [Usaco2009 Hol]Cattle Bruisers 杀手游戏
- matlab计算位温,大气物理学复习资料
- mysql5.7.10 二进制包_MYSQL5.7二进制包的安装
- Sgen.exe: Speed up XmlSerializer's Startup Performance [.NET 2.0, XML Serialization]
- kali linux子远程桌面,适用于kali linux的远程桌面开启方法(从windows xp 远程登录到kali linux )...
- Spring Cloud F Spring Boot 2.0 版本升级说明书
- php取数组里面数组的值,PHP获取数组的键与值方法小结
- 数据在本地文件的写入和读取
- 2k2实用球员_2KOL2王朝不知道用谁?五大位置低价实用球员大解析!
- Javawbe的实战案例
- python 中文乱码问题深入分析
- 某小说App返回数据 解密分析
- applecare多少钱?_AppleCare和AppleCare +有什么区别?
- Java 后端技术清单 2023版
- 【Matplotlib】(一)绘制图像函数
- Day09 - 面向对象进阶