10-前端显示当前用户-jwt查询接口
2.2.1 需求分析认证服务对外提供jwt查询接口,流程如下:1、客户端携带cookie中的身份令牌请求认证服务获取jwt2、认证服务根据身份令牌从redis中查询jwt令牌并返回给客户端。2.2.2 API在认证模块定义 jwt查询接口:@Api(value = "jwt查询接口",description = "客户端查询jwt令牌内容")public interface AuthControl
2.2.1 需求分析
认证服务对外提供jwt查询接口,流程如下:
1、客户端携带cookie中的身份令牌请求认证服务获取jwt
2、认证服务根据身份令牌从redis中查询jwt令牌并返回给客户端。
2.2.2 API
在认证模块定义 jwt查询接口:
@Api(value = "jwt查询接口",description = "客户端查询jwt令牌内容")
public interface AuthControllerApi {
@ApiOperation("查询userjwt令牌")
public JwtResult userjwt();
....
2.2.3 Dao
无
2.2.4 Service
在AuthService中定义方法如下:
//从redis查询令牌
public AuthToken getUserToken(String token){
String userToken = "user_token:"+token;
String userTokenString = stringRedisTemplate.opsForValue().get(userToken);
if(userToken!=null){
AuthToken authToken = null;
try {
authToken = JSON.parseObject(userTokenString, AuthToken.class);
} catch (Exception e) {
LOGGER.error("getUserToken from redis and execute JSON.parseObject error
{}",e.getMessage());
e.printStackTrace();
}
return authToken;
}
return null;
}
2.2.5 Controller
@Override
@GetMapping("/userjwt")
public JwtResult userjwt() {
//获取cookie中的令牌
String access_token = getTokenFormCookie();
//根据令牌从redis查询jwt
AuthToken authToken = authService.getUserToken(access_token);
if(authToken == null){
return new JwtResult(CommonCode.FAIL,null);
}
return new JwtResult(CommonCode.SUCCESS,authToken.getJwt_token());
}
//从cookie中读取访问令牌
private String getTokenFormCookie(){
Map<String, String> cookieMap = CookieUtil.readCookie(request, "uid");
String access_token = cookieMap.get("uid");
return access_token;
}
启动服务
启动Nginx
2.2.6 测试
使用postman测试
1、请求 /auth/userlogin
观察cookie是否已存入用户身份令牌。
2、get请求jwt
前端页面
启动前端
修改nginx
#认证
location ^~ /openapi/auth/ {
proxy_pass http://auth_server_pool/auth/;
}
Nginx最新代码
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#cms页面预览
upstream cms_server_pool{
server 127.0.0.1:31001 weight=10;
}
#静态资源服务
upstream static_server_pool{
server 127.0.0.1:91 weight=10;
}
#前端动态门户
upstream dynamic_portal_server_pool{
server 127.0.0.1:10000 weight=10;
}
#后台搜索(公开api)
upstream search_server_pool{
server 127.0.0.1:40100 weight=10;
}
#媒体服务
upstream video_server_pool{
server 127.0.0.1:90 weight=10;
}
#前端ucenter
upstream ucenter_server_pool{
#server 127.0.0.1:7081 weight=10;
server 127.0.0.1:13000 weight=10;
}
#学习服务
upstream learning_server_pool{
server 127.0.0.1:40600 weight=10;
}
#认证服务
upstream auth_server_pool{
server 127.0.0.1:40400 weight=10;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#门户的顶级域名,门户的主站
server {
listen 80;
server_name www.xuecheng.com;
ssi on;
ssi_silent_errors on;
location / {
alias E:/java_www/xcEduUI05/;
index index.html index.htm;
}
location /static/img/ {
alias E:/java_www/xcEduUI05/img/;
}
location /static/css/ {
alias E:/java_www/xcEduUI05/css/;
}
location /static/js/ {
alias E:/java_www/xcEduUI05/js/;
}
location /static/plugins/ {
alias E:/java_www/xcEduUI05/plugins/;
add_header Access-Control‐Allow‐Origin http://ucenter.xuecheng.com;
#add_header Access‐Control‐Allow‐Credentials true;
#add_header Access‐Control‐Allow‐Methods GET;
}
location /plugins/ {
alias E:/java_www/xcEduUI05/plugins/;
add_header Access-Control‐Allow‐Origin http://ucenter.xuecheng.com;
#add_header Access‐Control‐Allow‐Credentials true;
#add_header Access‐Control‐Allow‐Methods GET;
}
#页面预览
location /cms/preview/ {
proxy_pass http://cms_server_pool/cms/preview/;
}
location /static/company/ {
proxy_pass http://static_server_pool;
}
location /static/teacher/ {
proxy_pass http://static_server_pool;
}
location /static/stat/ {
proxy_pass http://static_server_pool;
}
location /course/detail/ {
proxy_pass http://static_server_pool;
}
#前端门户课程搜索
location ^~ /course/search {
proxy_pass http://dynamic_portal_server_pool;
}
#后端搜索服务
location /openapi/search/ {
proxy_pass http://search_server_pool/search/;
}
#分类信息
location /static/category/ {
proxy_pass http://static_server_pool;
}
#开发环境webpack定时加载此文件
location ^~ /__webpack_hmr {
proxy_pass http://dynamic_portal_server_pool/__webpack_hmr;
}
#开发环境nuxt访问_nuxt
location ^~ /_nuxt/ {
proxy_pass http://dynamic_portal_server_pool/_nuxt/;
}
#认证
location ^~ /openapi/auth/ {
proxy_pass http://auth_server_pool/auth/;
}
}
#学成网媒体服务
server {
listen 90;
server_name localhost;
#视频目录
location /video/ {
alias E:/java_www/video/;
}
}
#学成网静态资源
server {
listen 91;
server_name localhost;
#公司信息
location /static/company/ {
alias E:/java_www/xcEduUI05/static/company/;
}
#老师信息
location /static/teacher/ {
alias E:/java_www/xcEduUI05/static/teacher/;
}
#统计信息
location /static/stat/ {
alias E:/java_www/xcEduUI05/static/stat/;
}
location /course/detail/ {
alias E:/java_www/xcEduUI05/course/detail/;
}
location /static/category/ {
alias E:/java_www/xcEduUI05/static/category/;
}
}
#学成网媒体服务代理
map $http_origin $origin_list{
default http://www.xuecheng.com;
"~http://www.xuecheng.com" http://www.xuecheng.com;
"~http://ucenter.xuecheng.com" http://ucenter.xuecheng.com;
}
#学成网媒体服务代理
server {
listen 80;
server_name video.xuecheng.com;
location /video {
proxy_pass http://video_server_pool;
#add_header Access-Control‐Allow‐Origin http://www.xuecheng.com;
add_header Access‐Control‐Allow‐Origin $origin_list;
#add_header Access‐Control‐Allow‐Origin *;
#add_header Access‐Control‐Allow‐Credentials true;
#add_header Access‐Control‐Allow‐Methods GET;
}
}
#学成网用户中心
server {
listen 80;
server_name ucenter.xuecheng.com;
#个人中心
location / {
proxy_pass http://ucenter_server_pool;
}
#后端搜索服务
location /openapi/search/ {
proxy_pass http://search_server_pool/search/;
}
#学习服务
location ^~ /api/learning/ {
proxy_pass http://learning_server_pool/learning/;
}
#认证
location ^~ /openapi/auth/ {
proxy_pass http://auth_server_pool/auth/;
}
}
}
登录后
如果其他网站也要接入这个登录,并显示用户名
只要在相关域名下添加以下代码
#认证
location ^~ /openapi/auth/ {
proxy_pass http://auth_server_pool/auth/;
}
整个流程
用户名和密码登录,调用下发jwt接口。将jwt的所有信息保存到redis,键为短令牌。同时把短令牌保存到cookie
跳转到首页时,从cookie里拿到短令牌,然后通过短令牌获取redis里的长令牌信息。并把长令牌进行反向解析数据拿到用户信息。因为长令牌就是base64进行加密,前端可以进行base64解密。然后把用户信息保存到sessionStorage
localStorage 和 sessionStorage
localStorage 和 sessionStorage 属性允许在浏览器中存储 key/value 对的数据。
sessionStorage 用于临时保存同一窗口(或标签页)的数据,在关闭窗口或标签页之后将会删除这些数据。
提示: 如果你想在浏览器窗口关闭后还保留数据,可以使用 localStorage 属性, 该数据对象没有过期时间,今天、下周、明年都能用,除非你手动去删除。
更多推荐
所有评论(0)