# 网关服务 **Repository Path**: ms-platform/gateway ## Basic Information - **Project Name**: 网关服务 - **Description**: 项目包含网关转发功能及登录认证功能。 业务功能: 1、同一设备一账号登录数量限制 2、多账号方式登录,例如:支持账号登录和微信登录 3、验证码校验 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 11 - **Forks**: 7 - **Created**: 2018-07-18 - **Last Updated**: 2025-10-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### 网关配置项: application.yml ``` security: #免过滤配置项,请使用 - ""语法。 ignored: - "*.bundle.*" server: session: #session超时时间(单位:分) timeout: 2 service: authentication: #关闭验证码校验(true/false) captcha: false #同一设备同一账号最大登录数,默认为1个 maximumSessions: 1 #根据登录账号验证用户信息类,支持多个(例如:账号,微信),请使用 - ""语法。 authenticateAction: - "com.klxedu.ms.gateway.security.authentication.impl.DefaultAuthenticateAction" - "com.klxedu.ms.gateway.security.authentication.impl.WXAuthenticateAction" #资源服务类,获取系统中所有资源及关联的角色,用于访问权限判断 resourceService: com.klxedu.ms.gateway.security.access.impl.ResourceService #角色服务类,根据用户标识获取当前用户所拥有的角色权限 roleService: com.klxedu.ms.gateway.security.authentication.impl.AuthenticateRolesImpl #可设置验证码长度 captchaSize: 4 #可设置验证码超时时间(单位:秒) captchaTimeout: 5000 ``` ### 登录方式: > 登录地址:http://localhost:{port}/user #### 200:登录成功 ``` { "authenticated": true } ``` 需要在访问时,将用户名和密码Base64后放入Header中传递。 例如: ``` gr:gr Base64后为 Z3I6Z3I= header("Authorization", "Basic Z3I6Z3I=") ``` **注:记得一定要加上“Basic”** ### 登出方式: 登出地址:http://localhost:{port}/logout ``` # 200:登出成功 { "data": null, "code": "2000", "message": "logout success" } ``` ### CRSF防攻击处理 csrf又称跨域请求伪造,攻击方通过伪造用户请求访问受信任站点。 在所有GET请求后会返回一个csrftoken,该token放在Header中,并在 **除GET, HEAD, TRACE, OPTIONS外** 的所有请求中需要将该token回传 Get请求Header中Cookie的key为:XSRF-TOKEN 提交请求有两种方式 1、通过Header方式token回传,需要在Header中使用 X-XSRF-TOKEN 为Key,Value为Cookie中XSRF-TOKEN的值,同时Cookie也需要回传。 2、通过parameter方式token回传,需要在请求后使用 _csrf 为Key,Value为Cookie中XSRF-TOKEN的值,同时Cookie也需要回传。 ### 验证码: 验证码图片获取方式:http://localhost:{port}/generateCaptchaImage 验证码获取方式:http://localhost:{port}/generateCaptchaNum 提交验证码时的参数名称:http://localhost:{port}/user?captcha={num} ### 扩展点: authenticateAction、resourceService、roleService 三种扩展方式,参考默认实现类,实现相应的接口,并在配置项中替换默认配置 ### 微服务获取用户信息方式: 由于网关在前面做了代理作用,所以微服务需要通过网关转发请求过来,在转发的过程中,网关会将用户的信息保存在Header中传递给微服务应用,因此微服务不再需要从Session中获取信息。 request.getHeader("authService.USERID") //用户ID request.getHeader("authService.DEPARTID") //用户部门ID request.getHeader("authService.LOGINID") //用户登录账号 request.getHeader("authService.SCOPECODE") //用户当前管理范围 ### 微服务配置项: 微服务所有请求不加权限控制,要求部署在内网环境下,所有访问必须通过网关转发,出于安全性考虑,微服务地址必须配置为127.0.0.1 server.address: 127.0.0.1 ### Http Status 错误编码: #### 401:未登录、登录超时 访问授权资源 ``` { "timestamp": 1531967339307, "status": 401, "error": "Unauthorized", "message": "Full authentication is required to access this resource", "path": "/user" } ``` #### 401:用户名或密码错误 ``` { "timestamp": 1531967907786, "status": 401, "error": "Unauthorized", "message": "用户不存在: gr", "path": "/user" } ``` #### 403访问未授权请求 ``` { "timestamp": 1531967793534, "status": 403, "error": "Forbidden", "message": " 没有权限访问! ", "path": "/user_admin" } ``` #### 405不支持的访问方式 > 该情况出现在资源访问方式未被配置在资源管理中,参考com.klxedu.ms.gateway.security.access.impl.ResourceService类 ``` { "timestamp": 1533180654768, "status": 405, "error": "Method Not Allowed", "exception": "org.springframework.web.HttpRequestMethodNotSupportedException", "message": "Request method 'POST' not supported", "path": "/user_admin" } ``` #### 500:验证码验证失败或账号被另一账号代替 ``` { "timestamp": 1531967040294, "status": 10001, "error": "Http Status 10001", "message": "验证码校验失败。", "path": "/user" } { "timestamp": 1531968332807, "status": 10002, "error": "Http Status 10002", "message": "相同的账号已在其它设备上登录,请重新登录。", "path": "/user" } { "code":"10003", "message":"请求/api-ouser/workbench/organization重复提交" } -- 10001 验证码校验失败 -- 10002 账号冲突 -- 10003 重复提交 ``` ### 管理范围接口 #### 查询当前用户管理范围 - 请求地址:http://{ip}:{port}/user/listScope - 请求方式:GET - 返回格式 ``` {"data":[{"SCOPE_CODE":"-1/5","ORGANIZATION_NAME":"测试机构1"},{"SCOPE_CODE":"-1","ORGANIZATION_NAME":"组织机构树"}],"code":"2000","message":null} ``` #### 设置用户管理范围 - 请求地址:http://{ip}:{port}/user/setScope - 请求方式:GET - 参数:scopeCode=xx