Browse Source

tenant 租户切换逻辑修改

huangzhiyong 8 years ago
parent
commit
edbe187a09

+ 4 - 3
src/main/java/com/yihu/hos/common/constants/ContextAttributes.java

@ -4,10 +4,11 @@ package com.yihu.hos.common.constants;
 * @created Airhead 2016/11/14.
 */
public interface ContextAttributes {
    String SCHEMA = "schema";
    String TENANT_NAME = "tenant_name";
    String SCHEMA = "schema";//schema数据库名称
    String SCHEMA_TEMP = "schema_temp";//schema副本,用于在租户库和中心库之前切换的中间件
    String TENANT_NAME = "tenant_name";//租户tenant名称
    String USER_INFO = "userInfo";
    String TENANT_SESSION = "tenantSession";
    String TENANT_SESSION = "tenantSession";//session缓存的的schema 和 tenant
    String GLOBAL_DB = "global_db";//平台管理中心库
    String SHELL_RESPONSE = "shell_repsonse.";

+ 17 - 2
src/main/java/com/yihu/hos/interceptor/WebMvcInterceptor.java

@ -16,14 +16,29 @@ import javax.servlet.http.HttpSession;
 */
public class WebMvcInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        HttpSession session = request.getSession();
        String servletPath = request.getServletPath();
        TenantSession tenantSession = (TenantSession) session.getAttribute(ContextAttributes.TENANT_SESSION);
        if (tenantSession!=null) {
            LocalContext.getContext().setAttachment(ContextAttributes.SCHEMA, tenantSession.getSchema());
            LocalContext.getContext().setAttachment(ContextAttributes.TENANT_NAME, tenantSession.getTenant());
            if (servletPath.contains("/tenant/")){
                LocalContext.getContext().setAttachment(ContextAttributes.SCHEMA, ContextAttributes.GLOBAL_DB);
            }else {
                if (ContextAttributes.GLOBAL_DB.equals(LocalContext.getContext().getAttachment(ContextAttributes.SCHEMA))){
                    //切换到租户对应数据库
                    String userSchema =  LocalContext.getContext().getAttachment(ContextAttributes.SCHEMA_TEMP);
                    LocalContext.getContext().setAttachment(ContextAttributes.SCHEMA, userSchema);
                }else {
                    LocalContext.getContext().setAttachment(ContextAttributes.SCHEMA, tenantSession.getSchema());
                    LocalContext.getContext().setAttachment(ContextAttributes.TENANT_NAME, tenantSession.getTenant());
                    LocalContext.getContext().setAttachment(ContextAttributes.SCHEMA_TEMP, tenantSession.getSchemaTemp());
                }
            }
        }
        return true;
    }

+ 13 - 0
src/main/java/com/yihu/hos/tenant/model/TenantSession.java

@ -11,6 +11,8 @@ public class TenantSession {
    private String token;
    private String schema;
    private String tenant;
    private String schemaTemp;
    private String role;//角色类型;admin:管理员,null :普通租户
@ -20,15 +22,26 @@ public class TenantSession {
    public TenantSession(String tenant, String schema) {
        this.tenant = tenant;
        this.schema = schema;
        this.schemaTemp = schema;
        token = UUID.randomUUID().toString();
    }
    public TenantSession(String userCode, String schema,String token) {
        this.userCode = userCode;
        this.schema = schema;
        this.schemaTemp = schema;
        this.token = token;
    }
    public String getSchemaTemp() {
        return schemaTemp;
    }
    public void setSchemaTemp(String schemaTemp) {
        this.schemaTemp = schemaTemp;
    }
    public String getRole() {
        return role;
    }

+ 3 - 1
src/main/java/com/yihu/hos/tenant/service/AuthenticateService.java

@ -53,6 +53,7 @@ public class AuthenticateService {
        }
        //切换Schema到对应租户
        LocalContext.getContext().setAttachment(ContextAttributes.SCHEMA, tenantModel.getSchema());
        LocalContext.getContext().setAttachment(ContextAttributes.SCHEMA_TEMP, tenantModel.getSchema());
//        UserModel userModel = userDao.findOne(user);
//        //TODO:现在只是简单逻辑,需要修改成安全逻辑
//        if (userModel == null || !password.equals(userModel.getPassword())) {
@ -70,8 +71,9 @@ public class AuthenticateService {
     * @throws Exception
     */
    public String logout(HttpSession httpSession) throws Exception {
        //移除Schema
        //移除Schema及缓存
        LocalContext.getContext().removeAttachment(ContextAttributes.SCHEMA);
        LocalContext.getContext().removeAttachment(ContextAttributes.SCHEMA_TEMP);
        httpSession.removeAttribute(ContextAttributes.TENANT_SESSION);
        return "Tenan Logout Success";
    }

+ 1 - 1
src/main/resources/application.yml

@ -24,7 +24,7 @@ spring:
  #SAAS管理员账号,暂时配置在此处
  datasource:
      driverClassName: com.mysql.jdbc.Driver
      url: jdbc:mysql://192.168.1.220:8066/global_db?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true
      url: jdbc:mysql://172.19.103.57:8066/global_db?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true
      username: hos
      password: 123456
      test-on-borrow: true

+ 5 - 20
src/main/resources/banner.txt

@ -1,22 +1,7 @@
                                            _ooOoo_
                                           o8888888o
                                           88" . "88
                                           (| -_- |)
                                            O\ = /O
                                        ____/`---'\____
                                      .   ' \\| |// `.
                                       / \\||| : |||// \
                                     / _||||| -:- |||||- \
                                       | | \\\ - /// | |
                                     | \_| ''\---/'' | |
                                      \ .-\__ `-` ___/-. /
                                   ___`. .' /--.--\ `. . __
                                ."" '< `.___\_<|>_/___.' >'"".
                               | | : `- \`.;`\ _ /`;.`/ - ` : | |
                                 \ \ `-. \_ __\ /__ _/ .-` / /
                         ======`-.____`-.___\_____/___.-`____.-'======
                                            `=---='
                         .............................................
                         佛祖保佑                               永无BUG
                ███████╗    ███████╗    ██████╗
                ██╔════╝    ██╔════╝    ██╔══██╗
                █████╗        ███████╗    ██████╔╝
                ██╔══╝        ╚════██║    ██╔══██╗
                ███████╗    ███████║    ██████╔
**************************spring-boot.version: ${spring-boot.version}*****************************