上篇写到对JWT的理解,这篇写一个小的demo来实践下

Github:https://github.com/wuhen152033/token/tree/dev

简介

本次的demo是基于SpringCloud微服务来实现的

  • 用户服务
  • 授权中心


用户服务

写了一个接口,实现用户名和密码来查询用户的功能,在此展现controller层

UserController

package com.wuhen.jwt.user.controller;

import com.wuhen.jwt.user.entity.User;
import com.wuhen.jwt.user.service.UserService; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; /**
* @Author: 王筱哲
* @Date: 2019/6/13
* @Time: 13:34
*/
@RestController
public class UserController { @Autowired
private UserService userService; @PostMapping("query")
public ResponseEntity<User> queryByUsernameAndPassword(
@RequestParam("username")String username,
@RequestParam("password")String password
){
return ResponseEntity.ok(userService.queryByUsernameAndPassword(username,password));
}
}

授权中心

主要是token的生成以及存储到cookie的功能

token的生成是利用JWT+RSA非对称加密来实现的

写了一个Common(公共类)来实现token的生成(具体可以参考下源码)

授权服务的实现

业务代码

AuthController层

package com.wuhen.jwt.auth.controller;

import com.wuhen.jwt.auth.config.JwtProperties;
import com.wuhen.jwt.auth.entity.UserInfo;
import com.wuhen.jwt.auth.service.AuthService;
import com.wuhen.jwt.common.utils.CookieUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* @Author: 王筱哲
* @Date: 2019/6/13
* @Time: 17:32
*/
@RestController
@EnableConfigurationProperties(JwtProperties.class)
public class AuthController { @Autowired
private AuthService authService;
@Autowired
private JwtProperties properties; @PostMapping("accredit")
public ResponseEntity<Void> authentication(
@RequestParam("username") String username,
@RequestParam("password") String password,
HttpServletRequest request,
HttpServletResponse response
) {
//1.登录校验
String token = this.authService.authentication(username, password);
//2.将token写入cookie,并指定httpOnly为true,防止通过js获取和修改
CookieUtils.setCookie(request, response, properties.getCookieName(), token, properties.getCookieMaxAge(), true);
return ResponseEntity.ok().build();
} /**
* 用户验证
*
* @param token
* @return
*/
@GetMapping("verify")
public ResponseEntity<UserInfo> verifyUser(@CookieValue("j-cookie") String token,
HttpServletRequest request,
HttpServletResponse response) {
String token1 = authService.verifyUser(token).get(2).toString();
//3.更新Cookie中的token CookieUtils.setCookie(request, response, this.properties.getCookieName(), token1, this.properties.getCookieMaxAge());
return ResponseEntity.ok((UserInfo) authService.verifyUser(token).get(1)); } }

逻辑实现

  1. 通过用户名和密码来授权中心获得token
  2. 将token保存在cookie中,返回到客户端
  3. 下次请求携带cookie发送到服务器,服务器解析出用户信息

在授权中心调取用户查询服务是通过feignClient实现的,两个微服务之间的相互调用

参考:https://blog.csdn.net/lyj2018gyq

最新文章

  1. 深入理解CSS Media媒体查询
  2. python 爬取乌云所有厂商名字,url,漏洞总数 并存入数据库
  3. jQuery3的新特性
  4. 初探Lambda表达式
  5. java.sql.SQLException: 无效的列索引
  6. Git是如何存储对象的
  7. Eclipse的详细安装步骤
  8. 加密算法使用(五):RSA使用全过程
  9. Ubuntu下tftp服务搭建
  10. MVC 全站开启缓存,缓解服务器的请求压力
  11. 存储过程 分页【NOT IN】和【&gt;】效率大PK 千万级别数据测试结果
  12. 自己写shell命令pwd
  13. 制作简易计算器处理过程Servlet
  14. 基于 Koa平台Node.js开发的KoaHub.js的控制器,模型,帮助方法自动加载
  15. Mongodb 认证鉴权那点事
  16. Jacobi symbol(裸雅可比符号)
  17. PHP实现html字符实体转汉字
  18. 解决初次安装PyCharm后 No Python interpreter selected的问题
  19. 快乐python 零基础也能P图 —— PIL库
  20. Solrcloud(Solr集群)

热门文章

  1. linux常用工具
  2. LeetCode Count and Say 数数字
  3. linux 命令——56 ss(转)
  4. win8.1和wp8.1共用代码,需要注意的一些问题
  5. hdu-2256 Problem of Precision---矩阵快速幂+数学技巧
  6. Android(java)学习笔记82:利用SpannableString设置复合文本
  7. java算法面试题:递归算法题1
  8. JDBC中 mysql数据库的连接工具类 Java登录 及增删改查 整理 附带:Navicat Premium 11.0.12中文破解版.zip(下载)mysql数据库工具
  9. 创建一个 Dynamic Web Project
  10. 转载:jsonp详解