淘淘商城实战高并发分布式项目(2016.8月新版)

来源:互联网 发布:战地1数据查询 编辑:程序博客网 时间:2024/06/01 07:13

CK003-淘淘商城实战高并发分布式项目(有源码2016.8月新版)

学习要趁早,点滴记录,学习就是进步!

随笔背景:在很多时候,很多入门不久的朋友都会问我:我是从其他语言转到程序开发的,有没有一些基础性的资料给我们学习学习呢,你的框架感觉一下太大了,希望有个循序渐进的教程或者视频来学习就好了。对于学习有困难不知道如何提升自己可以加扣:1225462853进行交流得到帮助,获取学习资料.

CK003-淘淘商城实战高并发分布式项目(有源码2016.8月新版)

下载地址:http://pan.baidu.com/s/1jI05TPW


我们参考京东可以知道,京东在没有登录时就可以使用购物车,但是当要真正付款的时候,一定是要求登录的,也就是说由购物车列表页面直接跳转到登录页面去登录。这显然用到了拦截器的功能,本文我们便一起实现登录功能。 
下图便是购物车列表页面,我们点击”去结算”,如果当前用户还没登录,是必须要先登录的。也就是说在展示订单确认页面之前,需要对用户身份进行认证,要求用户必须登录。 

/** * 判断用户身份的拦截器 * <p>Title: LoginInterceptor</p> * <p>Description: </p> * <p>Company: www.itcast.cn</p>  * @version 1.0 */public class LoginInterceptor implements HandlerInterceptor {    @Value("${COOKIE_TOKEN_KEY}")    private String COOKIE_TOKEN_KEY;    @Value("${SSO_URL}")    private String SSO_URL;    @Autowired    private UserLoginService userLoginService;    @Override    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)            throws Exception {        // 先从cookie中取token        String token = CookieUtils.getCookieValue(request, COOKIE_TOKEN_KEY);        /*         * 如果没有token,直接跳转到sso系统的登录页面,而且还需要把当前请求的url         * 做为参数传递给sso系统,用户登录成功之后还要跳转回当前请求的页面,         * 因此要在请求url中添加一个回调地址。           */        if (StringUtils.isBlank(token)) {            // 跳转到http://localhost:8088/page/login            String url = SSO_URL + "/page/login?redirect=" + request.getRequestURL().toString();            // 如何进行跳转呢?            response.sendRedirect(url);            // 拦截            return false;        }        // 如果有token,需要调用sso系统的服务,根据token查询用户信息        TaotaoResult result = userLoginService.getUserByToken(token);        TbUser user = null;        if (result != null && result.getStatus() == 200) {            user = (TbUser) result.getData();        }        // 如果查询不到用户,跳转到sso系统的登录页面        else {            // 跳转到http://localhost:8088/page/login            String url = SSO_URL + "/page/login?redirect=" + request.getRequestURL().toString();            // 如何进行跳转呢?            response.sendRedirect(url);            // 拦截            return false;        }        /*         * 现在拦截器里面能够查询出用户,已经查了一次,如果我们在OrderController里面再取         * 一遍,那么就重复了,所以我们在拦截器里面取出用户之后,直接将其传递给OrderController。         */        // 把user对象放到request中,拦截器里面的request与OrderController里面的request是同一个        request.setAttribute("user", user);        // 如果查询到用户,则放行        return true;    }    @Override    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,            ModelAndView modelAndView) throws Exception {        // TODO Auto-generated method stub    }    @Override    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)            throws Exception {    }}

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"     xmlns:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">    <!-- 加载属性文件 -->    <context:property-placeholder location="classpath:resource/resource.properties" />    <context:component-scan base-package="com.taotao.order.controller" />    <mvc:annotation-driven />    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/jsp/" />        <property name="suffix" value=".jsp" />    </bean>    <!-- 拦截器配置 -->    <mvc:interceptors>        <mvc:interceptor>            <!-- 去结算超链接,http://localhost:8089/order/order-cart.html -->            <mvc:mapping path="/order/**"/>            <bean class="com.taotao.order.interceptor.LoginInterceptor"></bean>        </mvc:interceptor>    </mvc:interceptors>    <!-- 引用dubbo服务 -->    <dubbo:application name="taotao-order-web"/>    <dubbo:registry protocol="zookeeper" address="192.168.25.128:2181"/>        <dubbo:reference interface="com.taotao.sso.service.UserLoginService" id="userLoginService" /></beans>


原创粉丝点击