角色
- 后台系统管理员:登录后台管理系统,拥有后台系统中的所有的操作权限
- 后台系统普通员工:登录后台管理系统,对菜品、套餐、订单等进行管理
- c端用户:登录移动端应用,可以浏览菜品、添加购物车、设置地址、在线下单等
先创建一个springboot项目 配置application.yml或者application.properties
server:
port: 8080
spring:
datasource:
druid:
url: jdbc:mysql://localhost:3306/ruiji?characterEncoding=UTF-8
username: root
driver-class-name: com.mysql.jdbc.Driver
password: han123456
mybatis:
configuration:
map-underscore-to-camel-case: true #自动驼峰映射
ruiji:
path: F:ruiji_takeimg
mybatis-plus:
configuration:
# 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
logic-delete-field: isDelete
logic-delete-value: 1
logic-not-delete-value: 0
stat-view-servlet: login-password: 用这个报错
直接访问8080端口会被拦截
导入静态资源映射 不会被拦截
添加资源过滤 以及路径
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/backend
// 获取密码
String password = employee.getPassword();
//MD5加密工具类
//然后经过md5加密
password = DigestUtils.md5DigestAsHex(password.getBytes());
//创建一个查询条件
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
//获取用户
queryWrapper.eq(Employee::getUsername, employee.getUsername());
//因为数据库中的username是唯一约束 getOne
Employee emp = employeeService.getOne(queryWrapper);
//判断
if (emp == null) {
return R.error("登录失败");
}
if (!emp.getPassword().equals(password)) {
return R.error("1111111");
}
if (emp.getStatus() == 0) {
return R.error("账号禁用");
}
//放到session中
session.setAttribute("employee", emp.getId());
return R.success(emp);
}
退出
请求方式是post 请求地址是logout
@PostMapping("logout")
public R logout(HttpSession session) {
//把session里边的id给移除掉就好了
session.removeAttribute("employee");
return R.success("退出成功");
}
添加员工
@PostMapping public Radd(@RequestBody Employee employee,HttpSession session){ //因为没有添加密码 就直接把密码写死 在经过MD5加密 employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes())); //设置创建时间 employee.setCreateTime(LocalDateTime.now()); employee.setUpdateTime(LocalDateTime.now()); //登录以后 就是当前用户的id //需要强行转换 getAttribute是Object employee的id是Long型 Long empid = (Long) session.getAttribute("employee"); employee.setCreateUser(empid); employee.setUpdateUser(empid); employeeService.save(employee); return R.success("新增成功"); }
优化
当我们没有登录的时候能直接跳到后台页面 怎么也说不过去
所以我们需要添加一个过滤器 创建一个过滤器类
//这个名字随便起 拦截所有请求
@WebFilter(filterName = "MyFilter", urlPatterns = "
public boolean check(String[] urls, String requestURI) {
for (String url : urls) {
//backend
@ExceptionHandler(SQLIntegrityConstraintViolationException.class)
public R exception(SQLIntegrityConstraintViolationException e) {
log.error(e.getMessage());
//当再次创建时 就会Duplicate entry异常 名字重复
if (e.getMessage().contains("Duplicate entry")){
String[] s = e.getMessage().split(" ");
String s1 = s[2] + "已存在";
return R.error(s1);
}
return R.error("未知错误");
}
}



