第10章-单点登录.md 39 KB

第10章-单点登录

学习目标:

  • 能够说出主流的认证方案-JWT/Token
  • 搭建用户微服务模块
  • 完成基于Token+Redis实现认证
  • 完成网关统一鉴权

1、单点登录业务介绍

img

早期单一服务器,用户认证。

img

缺点:

  • 单点性能压力,无法扩展

分布式,SSO(single sign on)模式

img

解决 :

  • 用户身份信息独立管理,更好的分布式管理。

  • 可以自己扩展安全策略

  • 跨域不是问题

缺点:

  • 认证服务器访问压力较大。

业务流程图 {用户访问业务时,必须登录的流程}{单点登录的过程}

img

2、用户模块

2.1 实现思路

1、 用接收的用户名密码核对后台数据库

2、 核对通过,用uuid生成token

3、 将用户id加载到写入redis,redis的key为token,value为用户id。

4、 登录成功返回token与用户信息,将token与用户信息记录到cookie里面

5、 重定向用户到之前的来源地址。

数据库表:user_info,并添加一条数据!密码应该是加密的!

2.2 搭建认证中心模块service-user

2.2.1 搭建service-user服务

gmall-service模块下新增子模块:service-user。搭建方式如service-item

image-20221206174842858

2.2.2 启动类

package com.atguigu.gmall;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class UserApp {
   public static void main(String[] args) {
      SpringApplication.run(UserApp.class, args);
   }
}

2.2.3 配置pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>gmall-service</artifactId>
        <groupId>com.atguigu.gmall</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>service-user</artifactId>


    <build>
        <finalName>service-user</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2.2.4 添加配置文件

在resources目录下新增 bootstrap.properties 文件

spring.application.name=service-user
spring.profiles.active=dev
spring.cloud.nacos.discovery.server-addr=192.168.200.128:8848
spring.cloud.nacos.config.server-addr=192.168.200.128:8848
spring.cloud.nacos.config.prefix=${spring.application.name}
spring.cloud.nacos.config.file-extension=yaml
spring.cloud.nacos.config.shared-configs[0].data-id=common.yaml

修改Nacos配置中心:service-user-dev.yaml

image-20230306223213894

2.2.5 生成基础代码

mybatis-plus-code中执行代码生成器代码,将gmall_user数据库中的 user_info,user_address 生成基础代码。

package com.atguigu.mybatispluscode;

import com.atguigu.gmall.base.model.BaseEntity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.*;

/**
 * 可参考:https://blog.csdn.net/weixin_44541136/article/details/121202871
 */

public class CodeGenerator {


    public static void main(String[] args) {
        // 设置代码生成的位置
        // TODO String projectPath = System.getProperty("user.dir");
        String projectPath = "D:\\code\\workspace2023\\sph\\0825-sph\\gmall-parent\\gmall-service";
        // 设置父模块名称
        String parentModuleName = "com.atguigu.gmall";
        //TODO 设置子模块名称
        String moduleName = "user";
        String subPath = "/service-" + moduleName;
        // 设置数据库连接
        String databaseUrl = "jdbc:mysql://192.168.200.128:3306/gmall_" + moduleName + "?useUnicode=true&useSSL=false&characterEncoding=utf8";
        // 数据库用户名
        String username = "root";
        //TODO 数据库密码
        String password = "123456";
        // 设置表名前缀,例如表为tb_UserInfo,这里设置表前缀为"tb_",生成实体类的时候会自动去除前缀,最终生成UserInfo
        String tablePrefix = "";
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        // 最终输出目录
        gc.setOutputDir(projectPath + subPath + "/src/main/java");
        gc.setAuthor("atguigu"); //作者
        gc.setOpen(false); //是否打开输出目录
        gc.setSwagger2(true); //实体属性 Swagger2 注解
        gc.setDateType(DateType.ONLY_DATE); //时间类型为 Date LocalDateTime
        // 设置主键类型  ASSIGN_ID为分布式全局唯一ID  AUTO:数据库自增
        gc.setIdType(IdType.AUTO);
        //gc.setIdType(IdType.ASSIGN_ID);
        // 是否覆盖已有文件
        gc.setFileOverride(false);
        //去掉Service接口的首字母I
        gc.setServiceName("%sService");
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(databaseUrl);
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername(username);
        dsc.setPassword(password);
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
//        pc.setModuleName(scanner("模块名"));
        //设置实体类包名
        pc.setEntity("model");
        pc.setParent(parentModuleName);
        pc.setModuleName(moduleName);

        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            //自定义属性注入
            //在.ftl(或者是.vm)模板中,通过${cfg.abc}获取属性
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<>();
                map.put("parentName", parentModuleName);
                map.put("moduleName", moduleName);
                this.setMap(map);
            }
        };
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setEntity("templates/entity2.java");
        //指定自定义模板路径, 位置:/resources/templates/entity2.java.ftl(或者是.vm)
        templateConfig.setMapper("templates/mapper2.java");
        templateConfig.setService("templates/service2.java");
        templateConfig.setServiceImpl("templates/serviceImpl2.java");
        templateConfig.setController("templates/controller2.java");
        mpg.setTemplate(templateConfig);

        //TODO 根据需要来设置!!!例如:禁用模版的方式禁止生成实体类
        templateConfig.disable(TemplateType.ENTITY, TemplateType.CONTROLLER);
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setSuperEntityClass(BaseEntity.class);
        //写于父类中的公共字段
        strategy.setSuperEntityColumns("id", "create_time", "update_time", "is_deleted");
        strategy.setNaming(NamingStrategy.underline_to_camel);  //数据库表映射到实体的命名策略
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);    //数据库表字段映射到实体的命名策略
        strategy.setEntityLombokModel(true);    //【实体】是否为lombok模型(默认 false)
        strategy.setRestControllerStyle(true);
        strategy.setEntityTableFieldAnnotationEnable(true); //是否生成实体时,生成字段注解


        strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix(tablePrefix);
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + ":");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "!");
    }

}

2.2.4 登录/退出接口

YAPI接口地址:

2.2.4.1 控制器

package com.atguigu.gmall.user.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.user.model.UserInfo;
import com.atguigu.gmall.user.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-03-07 11:30
 */
@RestController
@RequestMapping("/api/user")
public class PassportController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 用户登录
     *
     * @param userInfo
     * @return
     */
    @PostMapping("/passport/login")
    public Result login(@RequestBody UserInfo userInfo, HttpServletRequest httpServletRequest) {
        Map loginMap = userInfoService.login(userInfo, httpServletRequest);
        return Result.ok(loginMap);
    }


    /**
     * 用户退出
     * @return
     */
    @GetMapping("/passport/logout")
    public Result logout(@RequestHeader("token") String token) {
        userInfoService.logout(token);
        return Result.ok();
    }
}

2.2.4.2 业务层

业务接口:UserInfoService

package com.atguigu.gmall.user.service;

import com.atguigu.gmall.user.model.UserInfo;
import com.baomidou.mybatisplus.extension.service.IService;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 用户表 业务接口类
 * @author atguigu
 * @since 2023-05-04
 */
public interface UserInfoService extends IService<UserInfo> {

    /**
     * 用户登录
     *
     * @param userInfo
     * @param httpServletRequest
     * @return
     */
    Map login(UserInfo userInfo, HttpServletRequest httpServletRequest);

    /**
     * 用户退出
     * @param token 用户令牌
     */
    void logout(String token);
}

业务实现类:

package com.atguigu.gmall.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.user.mapper.UserInfoMapper;
import com.atguigu.gmall.user.model.UserInfo;
import com.atguigu.gmall.user.service.UserInfoService;
import com.atguigu.gmall.user.util.AreaInfo;
import com.atguigu.gmall.user.util.BaiDuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 用户表 业务实现类
 *
 * @author atguigu
 * @since 2023-05-04
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 用户登录
     * 1.根据账号(登录名,手机号,邮箱号)查询用户记录
     * 2.将提交密码进行加密(md5)
     * 3.判断密码是否一致
     * 4.验证通过
     * 生成令牌UUID
     * 将用户信息存入Redis  key:uuid val:用户信息
     * 5.将生成令牌跟用户信息响应给客户端浏览器
     *
     * @param userInfo
     * @param httpServletRequest
     * @return
     */
    @Override
    public Map login(UserInfo userInfo, HttpServletRequest httpServletRequest) {
        //1.根据账号(登录名,手机号,邮箱号)查询用户记录
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getLoginName, userInfo.getLoginName());
        queryWrapper.or().eq(UserInfo::getEmail, userInfo.getLoginName());
        queryWrapper.or().eq(UserInfo::getPhoneNum, userInfo.getLoginName());
        ////2.将提交密码进行加密(md5)
        UserInfo loginUser = this.getOne(queryWrapper);

        //3.判断密码是否一致
        String userEnterPwd = DigestUtils.md5DigestAsHex(userInfo.getPasswd().getBytes());
        if (loginUser.getPasswd().equals(userEnterPwd)) {
            //4.验证通过
            // 4.1生成令牌UUID  user:uuid
            String token = UUID.randomUUID().toString().replaceAll("-", "").toString();
            String userKey = RedisConst.USER_KEY_PREFIX + token;
            // 4.2 将用户信息存入Redis  key:uuid val:用户信息
            JSONObject userInfoJsonObj = new JSONObject();
            userInfoJsonObj.put("id", loginUser.getId());
            //todo 通过第三方服务 根据IP获取城市
            //String ipAddress = IpUtil.getIpAddress(httpServletRequest);
            String ipAddress = "221.220.109.97";
            AreaInfo area = BaiDuService.getArea(ipAddress);
            if (area != null) {
                //todo 存储设备唯一标识,需要手机端获取手机硬件信息并提交服务端
                userInfoJsonObj.put("ipCity", area.getCity());
            }
            redisTemplate.opsForValue().set(userKey, userInfoJsonObj, 30, TimeUnit.MINUTES);

            // 5.将生成令牌跟用户信息响应给客户端浏览器
            HashMap<String, Object> mapResult = new HashMap<>();
            mapResult.put("token", token);
            mapResult.put("nickName", loginUser.getNickName());
            return mapResult;
        } else {
            throw new RuntimeException("用户名密码错误!");
        }
    }


    /**
     * 用户退出:将当前用户登录时存入Redis中令牌删除
     */
    @Override
    public void logout(String token) {
        //1.构建用户登录存入token Key
        String tokenKey = RedisConst.USER_KEY_PREFIX + token;
        //2.删除即可
        redisTemplate.delete(tokenKey);
    }
}

2.2.4.3 持久层

package com.atguigu.gmall.user.mapper;

import com.atguigu.gmall.model.user.UserInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

public interface UserInfoMapper extends BaseMapper<UserInfo> {
}

2.3 配置网关路由

在Nacos配置列表中,server-gateway-dev.yaml进行编辑增加动态路由

- id: service-user
  uri: lb://service-user
  predicates:
  - Path=/*/user/**
- id: web-passport
  uri: lb://web-all
  predicates:
  - Host=passport.gmall.com

2.4 在web-all模块添加实现

YAPI接口地址:http://192.168.200.128:3000/project/11/interface/api/803

2.4.1 在web-all 项目中跳转页面

package com.atguigu.gmall.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author: atguigu
 * @create: 2023-05-04 11:33
 */

@Controller
public class LoginContrller {

    /**
     * 渲染登录页面
     * @param model
     * @param originUrl 登录前访问页面地址
     * @return
     */
    @GetMapping("/login.html")
    public String loginHtml(Model model, @RequestParam("originUrl") String originUrl) {
        model.addAttribute("originUrl", originUrl);
        return "/login";
    }
}

2.4.2 登录页面

页面资源: \templates\login1.html

login1.html 没有公共头部信息

login.html 有公共头部信息

img

Html关键代码

<form class="sui-form">
   <div class="input-prepend"><span class="add-on loginname"></span>
      <input id="inputName" type="text" v-model="user.loginName" placeholder="邮箱/用户名/手机号" class="span2 input-xfat">
   </div>
   <div class="input-prepend"><span class="add-on loginpwd"></span>
      <input id="inputPassword" type="password" v-model="user.passwd" placeholder="请输入密码" class="span2 input-xfat">
   </div>
   <div class="setting">
      <label class="checkbox inline">
         <input name="m1" type="checkbox" value="2" checked="">
         自动登录
      </label>
      <span class="forget">忘记密码?</span>
   </div>
   <div class="logined">
      <a class="sui-btn btn-block btn-xlarge btn-danger" href="javascript:" @click="submitLogin()">登&nbsp;&nbsp;录</a>
   </div>
</form>

<script src="/js/api/login.js"></script>
<script th:inline="javascript">
   var item = new Vue({
      el: '#profile',

      data: {
            originUrl: [[${originUrl}]],
            user: {
                loginName: '',
                passwd: ''
            }
      },

      created() {
      },

      methods: {
            submitLogin() {
                login.login(this.user).then(response => {
                    
                    if (response.data.code == 200) {
                        //把token存在cookie中、也可以放在localStorage中
                        auth.setToken(response.data.data.token)
                        auth.setUserInfo(JSON.stringify(response.data.data))

                        console.log("originUrl:"+this.originUrl);
                        if(this.originUrl == ''){
                            window.location.href="http://www.gmall.com/index.html"
                            return ;
                        } else {
                            window.location.href = decodeURIComponent(this.originUrl)
                  }
                    } else {
                  alert(response.data.data.message)
               }

                })
            }
        }
   })
</script>

2.5 头部信息处理

web-all项目:common/header.html,common/head.html

功能:头部信息为公共信息,所有页面都具有相关的头部,所以我们可以单独提取出来,头部页面显示登录状态与关键字搜索等信息

2.5.1 提取头部信息

提取头部信息我们会用到thymeleaf 两个标签:

th:fragment:定义代码块

th:include:将代码块片段包含的内容插入到使用了th:include的HTML标签中

1,定义头部代码块(/common/header.html),关键代码

<div id="nav-bottom" th:fragment="header">
    <!--顶部-->
	<div class="nav-top" id="header">
		...
	</div>
</div>

2,在其他页面引用头部代码块

<div th:include="common/header :: header"></div>

2.5.2 头部登录状态处理

思路:登录成功后我们将用户信息写入了cookie,所以我们判断cookie中是否有用户信息,如果有则显示登录用户信息和退出按钮,我们采取vue的渲染方式

关键代码

Header.html 中

<ul class="fl">
    <li class="f-item">尚品汇欢迎您!</li>
    <li  v-if="userInfo.nickName == ''" class="f-item">请<span><a href="javascript:" @click="login()">登录</a></span> <span><a href="#">免费注册</a></span></li>
    <li  v-if="userInfo.nickName != ''" class="f-item"><span>{{userInfo.nickName}}</span> <span><a href="javascript:" @click="logout()">退出</a></span></li>
</ul>

<script type="text/javascript" src="/js/plugins/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/js/plugins/jquery.cookie.js"></script>
<script src="/js/plugins/vue.js"></script>
<script src="/js/plugins/axios.js"></script>
<script src="/js/auth.js"></script>
<script src="/js/request.js"></script>
<script src="/js/api/login.js"></script>
<script th:inline="javascript">
    var item = new Vue({
        el: '#header',

        data: {
            userInfo: {
                nickName: '',
                name: ''
            }
        },

        created() {
            this.showInfo()
        },

        methods: {
            showInfo() {
                // debugger
                if(auth.getUserInfo()) {
                    this.userInfo = auth.getUserInfo()
                    console.log("--------"+this.userInfo.nickName)
                }
            },

            
            logout() {
                //debugger
                login.logout().then(response => {
                    console.log("已退出")
                    auth.removeToken()
                    auth.removeUserInfo()

                    //跳转页面
                    window.location.href = "/"
                })
            }
        }
    })
</script>

2.4.3 头部关键字搜索

<div class="input-append">
    <input id="keyword" type="text" v-model="keyword" class="input-error input-xxlarge" />
    <button class="sui-btn btn-xlarge btn-danger" @click="search()" type="button">搜索</button>
</div>
<script th:inline="javascript">
    var item = new Vue({
        el: '#header',

        data: {
            keyword: [[${searchParam?.keyword}]],
            userInfo: {
                nickName: '',
                name: ''
            }
        },

        created() {
            this.showInfo()
        },

        methods: {
            showInfo() {
                // debugger
                if(auth.getUserInfo()) {
                    this.userInfo = auth.getUserInfo()
                    console.log("--------"+this.userInfo.nickName)
                }
            },

            search() {
                if(this.keyword == null) this.keyword = ''
                window.location.href = 'http://list.gmall.com/search.html?keyword=' + this.keyword
            },

            login() {
                window.location.href = 'http://passport.gmall.com/login.html?originUrl='+window.location.href
            },

            logout() {
                //debugger
                login.logout().then(response => {
                    console.log("已退出")
                    auth.removeToken()
                    auth.removeUserInfo()

                    //跳转页面
                    window.location.href = "/"
                })
            }
        }
    })
</script>

说明:[[${searchParam?.keyword}]],searchParam为搜索列表的搜索对象,如果存在searchParam对象,显示关键字的值

2.5.4 头部公共js

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>

<div th:fragment="head">
    <script type="text/javascript" src="/js/plugins/jquery/jquery.min.js"></script>
    <script type="text/javascript" src="/js/plugins/jquery.cookie.js"></script>
    <script src="/js/plugins/vue.js"></script>
    <script src="/js/plugins/axios.js"></script>
    <script src="/js/auth.js"></script>
    <script src="/js/request.js"></script>
</div>
</body>
</html>

引用

img

3、用户认证与服务网关整合

3.1 实现思路

  1. 所有请求都会经过服务网关,服务网关对外暴露服务,不管是api异步请求还是web同步请求都走网关,在网关进行统一用户认证

  2. 既然要在网关进行用户认证,网关得知道对哪些url进行认证,所以我们得对url制定规则

  3. Web页面同步请求(如:*.html),我采取配置白名单的形式,凡是配置在白名单里面的请求都是需要用户认证的(注:也可以采取域名的形式,方式多多)

  4. Api接口异步请求的,我们采取url规则匹配,如:/api/** 、/auth /**,"/inner/"如凡是满足该规则的都必须用户认证

所以在Nacos配置列表,修改server-gateway-dev.yaml增加需要校验的html访问路径

authUrls:
  url: trade.html,myOrder.html #,list.html, addCart.html # 用户访问该控制器的时候,会被拦截跳转到登录!

3.2 在服务网关添加fillter

  1. gmall-gateway模块的pom.xml中增加redis的依赖

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
    </dependency>
    
  2. gmall-gateway模块中配置RedisTemplate采用自定义序列化器

    package com.atguigu.gmall.gateway.config;
       
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.data.redis.connection.RedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
       
    @Configuration
    @EnableCaching
    public class RedisConfig {
       
       @Primary
       @Bean
       public RedisTemplate<Object, Object> RedisTemplate(RedisConnectionFactory RedisConnectionFactory) {
           RedisTemplate<Object, Object> RedisTemplate = new RedisTemplate<>();
           RedisTemplate.setConnectionFactory(RedisConnectionFactory);
       
           //使用Jackson2JsonRedisSerialize 替换默认序列化(默认采用的是JDK序列化) 对存储的对象进行JSON序列化
           Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
           ObjectMapper objectMapper = new ObjectMapper();
           objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
           objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
           jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
       
           // 序列化key value
           RedisTemplate.setKeySerializer(new StringRedisSerializer());
           RedisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
           RedisTemplate.setHashKeySerializer(new StringRedisSerializer());
           RedisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
       
           RedisTemplate.afterPropertiesSet();
           return RedisTemplate;
       }
       
    }
    
  3. gmall-gateway 项目中添加一个全局过滤器

    package com.atguigu.gmall.gateway.filter;
       
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import com.atguigu.gmall.common.result.Result;
    import com.atguigu.gmall.common.result.ResultCodeEnum;
    import org.apache.commons.lang.StringUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.gateway.filter.GatewayFilterChain;
    import org.springframework.cloud.gateway.filter.GlobalFilter;
    import org.springframework.core.Ordered;
    import org.springframework.core.io.buffer.DataBuffer;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.http.HttpCookie;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.server.reactive.ServerHttpRequest;
    import org.springframework.http.server.reactive.ServerHttpResponse;
    import org.springframework.stereotype.Component;
    import org.springframework.util.AntPathMatcher;
    import org.springframework.web.server.ServerWebExchange;
    import reactor.core.publisher.Mono;
       
    import java.util.List;
       
    /**
    * @author: atguigu
    * @create: 2023-05-04 15:13
    */
    @Component
    public class AuthFilter implements GlobalFilter, Ordered {
       
       @Autowired
       private RedisTemplate redisTemplate;
       
       
       private AntPathMatcher antPathMatcher = new AntPathMatcher();
       
       /**
        * 需要登录才可以访问静态页面地址
        */
       @Value("${authUrls.url}")
       private List<String> authUrl;
       
       /**
        * 统一验证用户身份请求
        *
        * @param exchange 交换器对象获取请求,响应对象
        * @param chain    过滤器链
        * @return
        */
       @Override
       public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
           ServerHttpRequest request = exchange.getRequest();
           ServerHttpResponse response = exchange.getResponse();
           //1.对于静态资源 js css img 直接放行
           //1.1 获取请求路径地址  getURI()  www.list.gmall.com/ab/cd   getURI().getPath() /ab/cd
           String urlPath = request.getURI().getPath();
           //1.2 采用地址匹配器进行判断
           if (antPathMatcher.match("**/js/**", urlPath) ||
                   antPathMatcher.match("**/img/**", urlPath) ||
                   antPathMatcher.match("**/css/**", urlPath)) {
               return chain.filter(exchange);
           }
           //2.对于请求地址中包含"inner"说明该接口用于服务间调用,无论登录与否都禁止直接通过网关访问
           if (antPathMatcher.match("/**/inner/**", urlPath)) {
               return outError(response, ResultCodeEnum.ILLEGAL_REQUEST);
           }
       
           //3.尝试从Redis中获取用户信息
           String userId = this.getUserId(request);
           if (StringUtils.isBlank(userId)) {
               //3.1 对于某些ajax请求包含"auth"只有登录后才可以访问,如果用户没登录响应401没有权限访问
               if (antPathMatcher.match("/**/auth/**", urlPath)) {
                   return outError(response, ResultCodeEnum.LOGIN_AUTH);
               }
               //3.2 对于前端部分页面访问 例如:我的订单页面 我的购物车页面 等这些页面,只有登录后才可以访问,引导用户登录
               for (String url : authUrl) {
                   if (antPathMatcher.match("/" + url, urlPath)) {
                       //设置响应http状态码
                       response.setStatusCode(HttpStatus.SEE_OTHER);
                       //设置重定向地址 浏览器会解析响应头地址 进行浏览器重定向
                       response.getHeaders().set(HttpHeaders.LOCATION, "http://passport.gmall.com/login.html?originUrl=" + request.getURI());
                       return response.setComplete();
                   }
               }
           }
           //4.动态路由同时将获取到用户ID设置到请求头中,将用户ID透传到目标微服务中
           if (StringUtils.isNotBlank(userId)) {
               request.mutate().header("userId", userId);
           }
           return chain.filter(exchange);
       }
       
       /**
        * 过滤器执行顺序,值越小,优先级越高
        *
        * @return
        */
       @Override
       public int getOrder() {
           return 1;
       }
       
       public static void main(String[] args) {
           AntPathMatcher antPathMatcher = new AntPathMatcher();
           boolean match = antPathMatcher.match("/**/inner/**", "/api/product/inner/getSkuInfo/24");
           System.out.println(match);
       }
       
       
       /**
        * TODO 在网关模块使用Redis模板对象,使用默认的序列化器
        * 前端
        * 提交ajax请求,将token放在请求头token 中提交
        * 浏览器地址栏同步请求,将token放在cookie中提交 cookieName=token
        * 获取当前用户ID
        *
        * @return
        */
       private String getUserId(ServerHttpRequest request) {
           //1.尝试从请求头或者cookie中获取用户令牌
           String token = request.getHeaders().getFirst("token");
           if (StringUtils.isBlank(token)) {
               HttpCookie cookie = request.getCookies().getFirst("token");
               if (cookie != null) {
                   token = cookie.getValue();
               }
           }
           if (StringUtils.isBlank(token)) {
               return null;
           }
           //2.从redis中获取用户信息
           String tokenKey = "user:" + token;
           JSONObject userJsonObject = (JSONObject) redisTemplate.opsForValue().get(tokenKey);
           if (userJsonObject != null) {
               return userJsonObject.get("id").toString();
           }
           return null;
       }
       
       /**
        * 用来给前端响应错误提示信息
        *
        * @param response
        * @param resultCodeEnum
        * @return
        */
       private Mono<Void> outError(ServerHttpResponse response, ResultCodeEnum resultCodeEnum) {
           //1.准备响应结果对象,转为JSON对象
           Result<Object> result = Result.build(null, resultCodeEnum);
           String resultString = JSON.toJSONString(result);
       
           //2.响应结果给客户端
           //2.1 设置http状态码
           response.setStatusCode(HttpStatus.UNAUTHORIZED);
           //2.2 通过响应头设置响应数据格式-json
           response.getHeaders().add("content-type", "application/json;charset=utf-8");
           DataBuffer wrap = response.bufferFactory().wrap(resultString.getBytes());
           //2.3 网关结束响应-不再路由转发
           //return response.setComplete();
           //2.4 网关将响应数据返回给客户端
           return response.writeWith(Mono.just(wrap));
       }
    }
       
    

3.3 在服务网关中判断用户登录状态

在网关中如何获取用户信息:

1、从cookie中获取(如:web同步请求)

2、从header头信息中获取(如:异步请求)

如何判断用户信息合法:

登录时我们返回用户token,在服务网关中获取到token后,我在到redis中去查看用户id,如果用户id存在,则token合法,否则不合法,同时校验ip,防止token被盗用。

3.3.1 取用户信息

/**
 * 尝试从Redis中获取用户ID
 *
 * @param request
 * @param response
 * @return
 */
private String getUserId(ServerHttpRequest request, ServerHttpResponse response) {
    //1.先从请求头中获取Token
    String token = "";
    token = request.getHeaders().getFirst("token");
    if (StringUtils.isBlank(token)) {
        //2.在尝试从请求对象中Cookie中获取
        HttpCookie httpCookie = request.getCookies().getFirst("token");
        if (httpCookie != null) {
            token = httpCookie.getValue();
        }
    }

    //3.根据Token查询Redis中用户信息-判断用户是否为异地登录 todo 是否更换设备
    if (StringUtils.isNotBlank(token)) {
        String redisKey = "user:login:" + token;
        HashMap<String, String> userInfoMap = (HashMap<String, String>) redisTemplate.opsForValue().get(redisKey);
        if (userInfoMap != null) {
            //用户登录时所在城市
            String loginCity = userInfoMap.get("city");
            //获取本地访问所在城市
            String ipAddress = IpUtil.getGatwayIpAddress(request);
            String nowCity = BaiduMapUtil.getAddress("114.240.253.58");
            if (!nowCity.equals(loginCity)) {
                this.outError(response, ResultCodeEnum.ILLEGAL_REQUEST);
            }
            //4.返回用户ID
            String userId = userInfoMap.get("userId");
            return userId;
        }
    }
    return null;
}

3.3.2 输出信息out 方法

/**
 * 用来给前端响应错误提示信息
 *
 * @param response
 * @param resultCodeEnum
 * @return
 */
private Mono<Void> outError(ServerHttpResponse response, ResultCodeEnum resultCodeEnum) {
    //1.准备响应结果对象,转为JSON对象
    Result<Object> result = Result.build(null, resultCodeEnum);
    String resultString = JSON.toJSONString(result);

    //2.响应结果给客户端
    //2.1 设置http状态码
    response.setStatusCode(HttpStatus.UNAUTHORIZED);
    //2.2 通过响应头设置响应数据格式-json
    response.getHeaders().add("content-type", "application/json;charset=utf-8");
    DataBuffer wrap = response.bufferFactory().wrap(resultString.getBytes());
    //2.3 网关结束响应-不再路由转发
    //return response.setComplete();
    //2.4 网关将响应数据返回给客户端
    return response.writeWith(Mono.just(wrap));
}

3.3.3 测试

  1. 通过网关访问内部接口,则不能访问!

http://localhost/api/product/inner/getSkuInfo/17

img

  1. 测试登录权限

测试一:

未登录 :http://localhost/api/product/auth/hello

img

登录完成之后继续测试!

登录:http://localhost/api/product/auth/hello

img

使用localhost访问,你登录或者不登录,都会提示未登录!

测试二:

用户在未登录情况下测试:

http://item.gmall.com/api/product/auth/hello

img

在上面的访问链接的时候,如果用户登录了,那么还会继续提示未登录!

img

404 表示资源没有!没有提示未登录!

原因:

测试一:访问资源的时候,没有获取到userId

测试二:访问资源的时候,获取到了userId

因为:我们登录成功的时候,将token放入了cookie中。在放入cookie的时候,我们给cookie 设置了一个作用域。

return $.cookie('token', token, {domain: 'gmall.com', expires: 7, path: '/'})

测试一:使用的域名是localhost

测试二:使用item.gmall.com 包含gmall.com

所以测试二是正确的!以后我们访问的时候,不会通过localhost访问,都是通过域名访问的!

  1. 验证Url 访问的是控制器

未登录直接访问:会弹出登录页面

http://list.gmall.com/list.html

  1. 登录之后,然后在访问

会显示查询结果!

http://list.gmall.com/list.html