一、创建jl-cart项目
购物车为提供者,需定义公共接口,和实现类
new -> Moudule> maven 创建子项目
jl-cart
继承、添加jl-common工具类依赖和跳过测试类打包的插件
将
jl-sso
的resources
复制到jl-cart
修改配置文件application.yml 设置端口为
8094
修改dubbo配置: 将
application.name
改为provider-cart
; 将protocol.port
改为20881
将
UserMapper.xml
改成cartMapper.xml
, 以及修改里面的映射路径在
jl-common
中定义DubboCartService
接口在
jl-cart
定义接口实现类DubboCartServiceImpl
创建SpringBootRun启动类 ,加上@MapperScan(“com.jl.mapper”)注解
在
jl-common
中添加Cart
的pojo对象
1.1 接口实现类
@Service
public class DubboCartServiceImpl implements DubboCartService {
@Autowired
private CartMapper cartMapper;
}
1.2 cart项目的启动类
package com.jl;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.jl.mapper")
public class SpringBootRun {
public static void main(String[] args) {
SpringApplication.run(SpringBootRun.class, args);
}
}
1.3 cart的pojo对象
package com.jl.pojo;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
@TableName("tb_cart")
@Data
@Accessors(chain = true)
public class Cart extends BasePojo {
@TableId
private Long id;
private Long userId;
private Long itemId;
private String itemTitle;
private String itemImage;
private Long itemPrice;
private Integer num;
}
二、拦截器实现权限控制
2.1 功能用法
如果用户不经登录直接访问后台服务器.则必须跳转到登录页面.
如果用户已经登陆.则可以直接访问服务器
2.2 拦截器实现原理
根据业务规则,定义拦截器不同的使用方法
Perhandler 控制程序执不执行
Posthandler 控制程序执行以后的结果
Afterhandler 控制视图渲染回显
2.3 编辑拦截器
在
jl-web
的创建UserInterceptor
拦截器如果不登录,就不让访问购物车,所以选择重写Perhadnler拦截方法
boolean: true 放行;false 拦截, 必须配合重定向使用
业务思路:
1.获取用户Cookie获取ticket数据
2.判断token中是否有数据
false 表示没有登陆,则重定向到用户登陆页面
true :表示用户之前登陆过
从redis中根据ticket获取userJSON,
再次判断数据是否有数据
false: 没有数据,则重定向到用户登陆页面
true : 表示有数据,则程序予以放行.
package com.jl;
import com.jl.pojo.User;
import com.jl.util.CookieUtil;
import com.jl.util.IPUtil;
import com.jl.util.ObjectMapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import redis.clients.jedis.JedisCluster;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component //表示交给Spring容器管理
public class UserInterceptor implements HandlerInterceptor {
@Autowired
private JedisCluster jedisCluster;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//1.获取Cookie数据
Cookie cookie = CookieUtil.getCookie(request, "JL_TICKET");
if (cookie != null) {
String ticket = cookie.getValue();
//2.判断token是否有数据
if (!StringUtils.isEmpty(ticket)) {
//2.1判断redis集群中是否有数据
if (jedisCluster.exists(ticket)) {
//2.2 校验IP
String nowIP = IPUtil.getIpAddr(request);
String realIP = jedisCluster.hget(ticket, "JL_USER_IP");
if (nowIP.equals(realIP)) {
return true;//表示放行
}
}
}
}
response.sendRedirect("/user/login.html");