上篇写到对JWT的理解,这篇写一个小的demo来实践下
###简介 本次的demo是基于SpringCloud微服务来实现的
- 用户服务
- 授权中心

####用户服务 写了一个接口,实现用户名和密码来查询用户的功能,在此展现controller层 UserController
1package com.wuhen.jwt.user.controller; 2 3import com.wuhen.jwt.user.entity.User; 4import com.wuhen.jwt.user.service.UserService; 5 6import org.springframework.beans.factory.annotation.Autowired; 7import org.springframework.http.ResponseEntity; 8 9import org.springframework.web.bind.annotation.PostMapping; 10import org.springframework.web.bind.annotation.RequestParam; 11import org.springframework.web.bind.annotation.RestController; 12 13/** 14 * @Author: 王筱哲 15 * @Date: 2019/6/13 16 * @Time: 13:34 17 */ 18@RestController 19public class UserController { 20 21 @Autowired 22 private UserService userService; 23 24 @PostMapping("query") 25 public ResponseEntity<User> queryByUsernameAndPassword( 26 @RequestParam("username")String username, 27 @RequestParam("password")String password 28 ){ 29 return ResponseEntity.ok(userService.queryByUsernameAndPassword(username,password)); 30 } 31} 32
####授权中心 主要是token的生成以及存储到cookie的功能 token的生成是利用JWT+RSA非对称加密来实现的 写了一个Common(公共类)来实现token的生成(具体可以参考下源码)

#####授权服务的实现 业务代码 AuthController层
1package com.wuhen.jwt.auth.controller; 2 3import com.wuhen.jwt.auth.config.JwtProperties; 4import com.wuhen.jwt.auth.entity.UserInfo; 5import com.wuhen.jwt.auth.service.AuthService; 6import com.wuhen.jwt.common.utils.CookieUtils; 7import org.springframework.beans.factory.annotation.Autowired; 8import org.springframework.boot.context.properties.EnableConfigurationProperties; 9import org.springframework.http.ResponseEntity; 10import org.springframework.web.bind.annotation.*; 11 12import javax.servlet.http.HttpServletRequest; 13import javax.servlet.http.HttpServletResponse; 14 15/** 16 * @Author: 王筱哲 17 * @Date: 2019/6/13 18 * @Time: 17:32 19 */ 20@RestController 21@EnableConfigurationProperties(JwtProperties.class) 22public class AuthController { 23 24 @Autowired 25 private AuthService authService; 26 @Autowired 27 private JwtProperties properties; 28 29 @PostMapping("accredit") 30 public ResponseEntity<Void> authentication( 31 @RequestParam("username") String username, 32 @RequestParam("password") String password, 33 HttpServletRequest request, 34 HttpServletResponse response 35 ) { 36 //1.登录校验 37 String token = this.authService.authentication(username, password); 38 //2.将token写入cookie,并指定httpOnly为true,防止通过js获取和修改 39 CookieUtils.setCookie(request, response, properties.getCookieName(), token, properties.getCookieMaxAge(), true); 40 return ResponseEntity.ok().build(); 41 } 42 43 /** 44 * 用户验证 45 * 46 * @param token 47 * @return 48 */ 49 @GetMapping("verify") 50 public ResponseEntity<UserInfo> verifyUser(@CookieValue("j-cookie") String token, 51 HttpServletRequest request, 52 HttpServletResponse response) { 53 String token1 = authService.verifyUser(token).get(2).toString(); 54 //3.更新Cookie中的token 55 56 CookieUtils.setCookie(request, response, this.properties.getCookieName(), token1, this.properties.getCookieMaxAge()); 57 return ResponseEntity.ok((UserInfo) authService.verifyUser(token).get(1)); 58 59 } 60 61} 62
逻辑实现
- 通过用户名和密码来授权中心获得token
- 将token保存在cookie中,返回到客户端
- 下次请求携带cookie发送到服务器,服务器解析出用户信息
在授权中心调取用户查询服务是通过feignClient实现的,两个微服务之间的相互调用