# 用户认证demo **Repository Path**: yjp245/user-authentication-demo ## Basic Information - **Project Name**: 用户认证demo - **Description**: 使用oauth2+spring security实现用户认证授权服务 - **Primary Language**: Java - **License**: Artistic-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 0 - **Created**: 2020-12-09 - **Last Updated**: 2023-12-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 用户认证流程分析 ![输入图片说明](https://images.gitee.com/uploads/images/2020/1210/144327_83c55abd_1892581.png "屏幕截图.png") 业务流程说明如下: 1、客户端请求认证服务进行认证。 2、认证服务认证通过向浏览器cookie写入token(身份令牌) 认证服务请求用户中心查询用户信息。 认证服务请求Spring Security申请令牌。 认证服务将token(身份令牌)和jwt令牌存储至redis中。 认证服务向cookie写入 token(身份令牌)。 3、前端携带token请求认证服务获取jwt令牌 前端获取到jwt令牌并存储在sessionStorage。 前端从jwt令牌中解析中用户信息并显示在页面。 4、前端携带cookie中的token身份令牌及jwt令牌访问资源服务 前端请求资源服务需要携带两个token,一个是cookie中的身份令牌,一个是http header中的jwt令牌 前端请求资源服务前在http header上添加jwt请求资源 5、网关校验token的合法性 用户请求必须携带token身份令牌和jwt令牌 网关校验redis中token是否合法,已过期则要求用户重新登录 6、资源服务校验jwt的合法性并完成授权 资源服务校验jwt令牌,完成授权,拥有权限的方法正常执行,没有权限的方法将拒绝访问。 # 用户认证demo-first基础版 #### 介绍 使用oauth2+spring security实现用户认证授权服务,这里主要实现的是密码认证模式 #### 软件架构 软件架构说明 uauth-center用户认证中心 eureka-server:注册中心 数据库:xc_user #### 安装教程 1. 首先启动Eureka服务 2. 启动uauth-center中心,注意yml文件的数据库配置和redis链接配置 查看数据库连接账户是否正确、redis是否启动。 3. 启动成功查看Eureka服务启动成功, http://localhost:8761 4. 注册中心是否有uauth-center的注册信息 ![Image text](https://gitee.com/yjp245/user-authentication-demo/raw/master/README_IMAGE/1607568451.jpg) #### 使用说明 ##### 1.使用postan测试认证接口: 首先填写httpBasic认证 username:XcWebApp password:XcWebApp 说明:这里httpBasic填写的是客户端ID和客户端密码,存储于oauth_details表 ![Image text](https://gitee.com/yjp245/user-authentication-demo/raw/master/README_IMAGE/httpBasic%E5%A1%AB%E5%86%99.jpg) http://localhost:40401/auth/oauth/token 然后再填写请求参数 ![Image text](https://gitee.com/yjp245/user-authentication-demo/raw/master/README_IMAGE/1607567324(1).jpg) grant_type: password username:itcast password:123 发起请求测试,验证成功则返回token信息 ![Image text](https://gitee.com/yjp245/user-authentication-demo/raw/master/README_IMAGE/1607568559.jpg) ##### 2.使用postan测试登录接口: ![输入图片说明](https://images.gitee.com/uploads/images/2020/1210/105613_aaf2b1c4_1892581.png "屏幕截图.png")