Skip to content

Latest commit

 

History

History
117 lines (89 loc) · 3.19 KB

UserDev.md

File metadata and controls

117 lines (89 loc) · 3.19 KB

用户模块开发

构建

在项目中复制一份guns-gateway模块并重命名为guns-user(修改子模块和主模块的pom.xml文件),并在application.yml中关闭其鉴权机制(仅网关需要开启),修改端口避免冲突:

rest:
  auth-open: false #jwt鉴权机制是否开启(true或者false)
  sign-open: false #签名机制是否开启(true或false)
  
server:
  port: 8083

在添加 Dubbo 协议和端口(UserApplication 需要向外部暴露接口,先向注册中心注册,然后通过 Dubbo 协议被远程调用)

spring:
  application:
    name: cinema-user
  dubbo:
    server: true
    registry: zookeeper://localhost:2181
    protocol:
      name: dubbo
      port: 20881

开发

guns-user中对guns-apiUserAPI编写其实现类,并通过@Service向外部暴露:

@Component
@Service(interfaceClass = UserAPI.class)
public class UserServiceImpl implements UserAPI {
...
}

guns-gatewayUserController中远程调用接口:

@RequestMapping("/user/")
@RestController
public class UserController {
    @Reference(interfaceClass = UserAPI.class)
    private UserAPI userAPI;
	...
}

配置guns-gateway模块中的application.yml,在jwt下添加ignore-url,用逗号分隔,这些接口将不通过jwt的验证过滤器:

jwt:
  header: Authorization   #http请求头所需要的字段
  secret: mySecret        #jwt秘钥
  expiration: 604800      #7天 单位:秒
  auth-path: auth         #认证请求的路径
  md5-key: randomKey      #md5加密混淆key
  ignore-url: /user/register,/user/check # 忽略列表

在jwt配置类中添加ignoreUrl,@ConfigurationProperties(prefix = JwtProperties.JWT_PREFIX) 表示用于从application.yml中获取jwt开头的配置属性:

@Configuration
@ConfigurationProperties(prefix = JwtProperties.JWT_PREFIX)
public class JwtProperties {
	...
    private String ignoreUrl = "";
    ...
}

AuthFilterdoFilterInternal()中添加忽略配置列表的代码:

// 配置忽略列表
String ignoreUrl = jwtProperties.getIgnoreUrl();
String[] ignoreUrls = ignoreUrl.split(",");
if (ignoreUrls.length != 0) {
    for (String url : ignoreUrls) {
        if (request.getServletPath().startsWith(url)) {
            chain.doFilter(request, response);
            return;
        }
    }
}

使用Threadlocal保存用户信息,在guns-gatewaycommon下创建 CurrentUser类保存userId(不直接保存userInfoModel,因为更省空间,避免用户量大时出现内存溢出):

public class CurrentUser {
    
    // 仅存储userId 防止存储整个对象占用过多的内存空间 OOM
    private static final ThreadLocal<String> THREAD_LOCAL = new ThreadLocal<>();

    public static void saveUserId(String userId) {
        THREAD_LOCAL.set(userId);
    }

    public static String getCurrentUser() {
        return THREAD_LOCAL.get();
    }
    
}

在每个模块的test目录下,都有generator.EntityGenerator用于根据数据库表生成实体类,根据需要稍微修改一下参数即可。在guns-user模块中生成:UserT, UserTMapper, UserTMapper.xml。