# TSMCP **Repository Path**: opera123/TSMCP ## Basic Information - **Project Name**: TSMCP - **Description**: 腾讯智能制造云平台 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 2 - **Created**: 2020-02-14 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 腾讯智能制造云平台 ## 项目介绍 组员:李海金,严波,宋皓楠   基于SpringBoot+Mybatis架构,实现前后端分离,提供整套后台生产模块:订单管理、产品管理、用户管理(第三方),订单管理,订单追踪, 微信平台、存储系统、配置中心、日志分析、任务和通知等 , 努力为制造业企业打造全方位J2EE企业级开发解决方案。 ### 技术选型 | 技术 | 名称 | 官网 | | ----------------- | ------------------- | ------------------------------------------------------------ | | Spring Framework | 容器 | http://projects.spring.io/spring-framework/ | | Git | 版本控制工具 | https://git-scm.com/ | | Gitee | 代码托管 | https://gitee.com/ | | SpringMVC | MVC框架 | http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc | | SpringBoot | 应用框架 | https://spring.io/projects/spring-boot/ | | Spring Security | 安全框架 | https://spring.io/projects/spring-security | | MyBatis | ORM框架 | http://www.mybatis.org/mybatis-3/zh/index.html | | MyBatis Generator | 代码生成 | http://www.mybatis.org/generator/index.html | | PageHelper | MyBatis物理分页插件 | http://git.oschina.net/free/Mybatis_PageHelpe | | Redis | 分布式缓存数据库 | https://redis.io/ | | Log4J | 日志组件 | http://logging.apache.org/log4j/1.2/ | | Maven | 项目构建管理 | http://maven.apache.org/ | ### 前端技术 | 技术 | 名称 | 官网 | | --------- | --------------------------- | ------------------------------------- | | jQuery | 函式库 | http://jquery.com/ | | Bootstrap | 前端框架 | http://getbootstrap.com/ | | datatable | 数据表格 | http://www.datatables.club/ | | Ajax | 异步JavaScript和XML 或 HTML | https://api.jquery.com/category/ajax/ | ### 登陆注册模块(宋皓楠) #### 基于spring security实现的登陆功能 一般权限控制有三层,即:`用户`<–>`角色`<–>`权限`,用户与角色是多对多,角色和权限也是多对多。登陆暂时不考虑权限,只考虑`用户`<–>`角色`的关系,实现简单的管理员和普通用户登陆的权限区分 tsmcp_user_role 权限表中,name字段储存了ROLE_ADMIN和ROLE_USER权限,这里权限格式"ROLE_XXX"是spring security规定的。 tsmcp_role_permit中关联了用户和权限。 ![image text](https://wx4.sinaimg.cn/mw690/bf6adc66gy1g8xxpioxbwj208q03i0sj.jpg) 用户的登陆认证是由Spring Security进行处理的,请求路径默认为`/login`,用户名字段默认为`username`,密码字段默认为`password` 。input标签name需设置为对应的默认字段 ![image text](https://wx4.sinaimg.cn/mw690/bf6adc66ly1g8yh0b8e3zj20fl0gmaaj.jpg) ”/admin“对应权限为admin的用户,“/user”对应用户为user的用户 ![image text](https://wx3.sinaimg.cn/mw690/bf6adc66ly1g8yhdlqds1j20a502g3yc.jpg) 也可以在控制层使用注解 @PreAuthorize("hasRole('ROLE_XXX')") 来指定这个请求的权限 `@PreAuthorize` 用于判断用户是否有指定权限,没有就不能访问 修改下要访问的接口,`@PreAuthorize("hasPermission('/admin','r')")` 参数1指明了**访问该接口需要的url**,参数2指明了**访问该接口需要的权限**。 即**访问 url 和权限**。 思路如下: 1. 通过 Authentication 取出登录用户的所有 Role 2. 遍历每一个 Role,获取到每个Role的所有 Permission 3. 遍历每一个 Permission,只要有一个 Permission 的 url 和传入的url相同,且该 Permission 中包含传入的权限,返回 true 4. 如果遍历都结束,还没有找到,返回false #### 自定义 UserDetailsService类 CustomUserDetailsService类 继承 UserDetailsService 重写 loadUserByUsername 方法,参数是用户输入的用户名。返回值是UserDetails,这是一个接口,一般使用它的子类org.springframework.security.core.userdetails.User,它有三个参数,分别是用户名、密码和权限集。 ```java TsmcpUser user = userService.selectByName(username); // 从数据库中取出用户信息 // 判断用户是否存在 //返回UserDetails实现类 ``` 将 DAO 中的 User 类继承 org.springframework.security.core.userdetails.User 返回。 #### 自定义WebSecurityConfig类 继承WebSecurityConfigurerAdapter ,将用户信息和权限注入进来。 该类是 Spring Security 的配置类,该类的三个注解分别是标识该类是配置类、开启 Security 服务、开启全局 Securtiy 注解。 首先将我们自定义的 userDetailsService 注入进来,在 configure() 方法中使用 auth.userDetailsService() 方法替换掉默认的 userDetailsService。 #### 配置application.properties ```properties #开启Mybatis下划线命名转驼峰命名 mybatis.configuration.map-underscore-to-camel-case=true ``` #### 密码加密 ```java @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService) .passwordEncoder(new BCryptPasswordEncoder()); } ``` #### Session 共享 一般情况下,一个程序为了保证稳定至少要部署两个,构成集群。那么就牵扯到了 Session 共享的问题,不然用户在 8080 登录成功后,后续访问了 8060 服务器,结果又提示没有登录。 简单实现 Session 共享,采用 Redis 来存储 1.导入redis依赖 2. `application.xml` 中新增配置指定 redis 地址以及 session 的存储方式 3. 为主类添加 `@EnableRedisHttpSession` 注解 4. 完成了基于 Redis 的 Session 共享 #### 退出登录 在WebSecurityConfig 的 configure() 方法中,配置 ```java http.logout(); ``` Spring Security 的默认退出配置,Spring Security 在退出时候做了这样几件事: 1. 使当前的 session 失效 2. 清除与当前用户有关的 remember-me 记录 3. 清空当前的 SecurityContext 4. 重定向到登录页 ### 权限控制模块 ### 图形界面模块 ### 产品管理模块 ### 开发环境 1. jdk 1.8 2. mysql 5.0 + 3. 阿里云服务器 4. apache-tomcat-9.0.12 5. win10/7 ### 开发工具 1. IDEA 2019 2. navicat 11 3. PowerDesigner 15 4. Axure RP 8 5. Xshell 6 6. Postman 8. RedisDesktopManager ### 数据库模型 ![image text](https://wx2.sinaimg.cn/mw690/bf6adc66gy1g8xvxsaiebj212q0lvq6f.jpg) ### 编译流程 maven编译安装pom.xml文件即可 设置SpringBoot配置文件里,redis数据库路径和mysql数据库路径 ### 常见问题 #### 运行时redis错误 [redis]Connection failure occurred. Restarting subscription task after 5000 ms 1.查看SpringBoot配置文件确认本地redis配置是否正确 2.确保redis正确运行在后台 3.缓存已经超出redis服务所规定的订阅缓存限制值,查看redis.conf配置文件:redis.windows.conf。 这一句:client-output-buffer-limit pubsub 32mb 8mb 60 Redis订阅客户端订阅buffer超过32M或持续60秒超过8M,订阅立即被关闭!解决改问题把限制值调大即可解决! 这里设置为:client-output-buffer-limit pubsub 256mb 64mb 60 4.用 redis-server.exe redis.windows.conf 命令启动redis 指定配文件(如果关掉,会出现问题)