上篇写到对JWT的理解,这篇写一个小的demo来实践下
###简介 本次的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));
}
}
逻辑实现
- 通过用户名和密码来授权中心获得token
- 将token保存在cookie中,返回到客户端
- 下次请求携带cookie发送到服务器,服务器解析出用户信息
在授权中心调取用户查询服务是通过feignClient实现的,两个微服务之间的相互调用