谷粒商城-高级-67 -商城业务-订单服务-登录拦截
一、环境搭建
订单环境创建细节请参考购物车环境搭建方法,谷粒商城-高级-61 -商城业务-购物车-环境搭建,引入相关的依赖和修改配置。
需要引入的依赖:
- 1、引入 thymeleaf
- 2、引入redis
- 3、引入SpringSession
- 4、引入 devtools
gulimall-order/pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 整合SpringSession完成session共享问题,使用redis作为session存储 -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
修改配置:gulimall-order/src/main/resources/application.yml
spring:
datasource:
username: root
password: root
url: jdbc:mysql://192.168.10.10:3306/gulimall_oms
driver-class-name: com.mysql.cj.jdbc.Driver
# 配置nacos注册中心
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
application:
name: gulimall-order
thymeleaf:
cache: false # 测试期间关掉缓存
redis:
host: 192.168.10.10
prot: 6379
session:
store-type: redis # Session store type,SpringSession整合,使用redis作为session存储
mybatis-plus:
mapper-location: classpath:/mapper/**/*.xml
global-config:
db-config:
id-type: auto # id主键自增
server:
port: 9000
servlet:
session:
timeout: 30m # Session timeout,SpringSession整合
创建session配置:com/atguigu/gulimall/order/config/GulimallSessionConfig.java
package com.atguigu.gulimall.order.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;
/**
* @author: kaiyi
* @create: 2020-09-09 17:05
*/
@EnableRedisHttpSession //除了在启动类开启,这里也可以开启
@Configuration
public class GulimallSessionConfig {
/**
* 子域名共享设置及session名自定义
* @return
*/
@Bean
public CookieSerializer cookieSerializer(){
DefaultCookieSerializer defaultCookieSerializer = new DefaultCookieSerializer();
defaultCookieSerializer.setDomainName("gulimall.com"); // 设置作用域
defaultCookieSerializer.setCookieName("GULISESSION"); // 设置session名
return defaultCookieSerializer;
}
/**
* 默认序列化转为JSON存储
*
* @return
*/
@Bean
public RedisSerializer<Object> springSessionDefaultRedisSerializer(){
return new GenericJackson2JsonRedisSerializer();
}
}
二、登录拦截
参照 gulimall-cart
购物车中的拦截验证,未登录的用户直接跳转到登录页面。
gulimall-order/xxx/order/interceptor/LoginUserInterceptor.java
package com.atguigu.gulimall.order.interceptor;
import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.vo.MemberResponseVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
/**
* @Description: 登录拦截器
* @author: kaiyi
* @createTime: 2020-09-12 19:21
**/
@Component
public class LoginUserInterceptor implements HandlerInterceptor {
public static ThreadLocal<MemberResponseVo> loginUser = new ThreadLocal<>();
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String uri = request.getRequestURI();
AntPathMatcher antPathMatcher = new AntPathMatcher();
boolean match = antPathMatcher.match("/order/order/status/**", uri);
boolean match1 = antPathMatcher.match("/payed/notify", uri);
if (match || match1) {
return true;
}
//获取登录的用户信息
MemberResponseVo attribute = (MemberResponseVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
if (attribute != null) {
//把登录后用户的信息放在ThreadLocal里面进行保存
loginUser.set(attribute);
return true;
} else {
//未登录,返回登录页面
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<script>alert('请先进行登录,再进行后续操作!');location.href='http://auth.gulimall.com/login.html'</script>");
// session.setAttribute("msg", "请先进行登录");
// response.sendRedirect("http://auth.gulimall.com/login.html");
return false;
}
}
@Override
public void postHandle(
HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
相关文章:
谷粒商城-高级-62 -商城业务-购物车-拦截器及 ThreadLocal 用户身份鉴别
为者常成,行者常至
自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)