+ * 数据初始化,实现 CommandLineRunner 接口,启动 springboot 后自动执行,如果有多个这个类可以根据 @Order 来指定执行顺序 + *
+ * + * @package: com.xkcoding.springbootdemorabcshiromybatis + * @description: 数据初始化 + * @author: yangkai.shen + * @date: Created in 2017/11/29 下午4:32 + * @copyright: Copyright (c) 2017 + * @version: 0.0.1 + * @modified: yangkai.shen + */ +@Component +@Slf4j +public class DBInitConfig implements CommandLineRunner { + + @Override + public void run(String... strings) throws Exception { + log.info("正在初始化数据。。。"); + log.info("数据初始化完成。。。"); + } + +} \ No newline at end of file diff --git a/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/config/ShiroConfig.java b/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/config/ShiroConfig.java new file mode 100644 index 0000000..8ad9d4d --- /dev/null +++ b/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/config/ShiroConfig.java @@ -0,0 +1,78 @@ +package com.xkcoding.springbootdemorabcshiromybatis.config; + +import com.google.common.collect.Maps; +import com.xkcoding.springbootdemorabcshiromybatis.shiro.MyShiroRealm; +import lombok.extern.slf4j.Slf4j; +import org.apache.shiro.mgt.DefaultSecurityManager; +import org.apache.shiro.mgt.SecurityManager; +import org.apache.shiro.spring.web.ShiroFilterFactoryBean; +import org.apache.shiro.web.mgt.DefaultWebSecurityManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.Map; + +/** + * Shiro 配置 + * + * @package: com.xkcoding.springbootdemorabcshiromybatis.config + * @description: Shiro 配置 + * @author: yangkai.shen + * @date: Created in 2017/11/29 下午3:24 + * @copyright: Copyright (c) 2017 + * @version: 0.0.1 + * @modified: yangkai.shen + */ +@Configuration +@Slf4j +public class ShiroConfig { + + @Bean + public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) { + ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); + // 必须设置 securityManager + shiroFilterFactoryBean.setSecurityManager(securityManager); + + // 设置登录页面,默认是 Web 工程目录下的"/login.jsp" + shiroFilterFactoryBean.setLoginUrl("/login"); + // 设置登陆成功后的页面 + shiroFilterFactoryBean.setSuccessUrl("/index"); + // 设置未授权页面 + shiroFilterFactoryBean.setUnauthorizedUrl("/403"); + + // 配置拦截器链,注意配置的顺序,很关键 + // 过滤链定义,从上向下顺序执行,一般将 /**放在最为下边 + // authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问 + Map+ * 常量生产工厂的接口 + *
+ * + * @package: com.xkcoding.springbootdemorabcshiromybatis.constrant.factory + * @description: 常量生产工厂的接口 + * @author: yangkai.shen + * @date: Created in 2017/12/6 下午3:51 + * @copyright: Copyright (c) 2017 + * @version: 0.0.1 + * @modified: yangkai.shen + */ +public interface Constant { + + String ADMIN_NAME = "超级管理员"; + + String COMMA = ","; + String SEMI = ";"; + String SEPARATE = "|"; + String PATH_SEPARATE = "/"; + String PERCENT = "%"; + + /** + * 根据用户id获取用户名称 + * + * @param userId 用户id + * @return 用户名称 + */ + String getRealNameById(Integer userId); + + /** + * 根据用户id获取用户账号 + * + * @param userId 用户id + * @return 用户账号 + */ + String getUsernameById(Integer userId); + + /** + * 根据角色id获取角色名称 + * + * @param roleId 角色id + * @return 角色名称 + */ + String getRoleName(Integer roleId); + + /** + * 根据角色id列表获取角色名称列表 + * + * @param roleIds 角色id列表 + * @return 角色名称列表 + */ + List+ * 常量的生产工厂 + *
+ * + * @package: com.xkcoding.springbootdemorabcshiromybatis.constrant.factory + * @description: 常量的生产工厂 + * @author: yangkai.shen + * @date: Created in 2017/12/6 下午4:01 + * @copyright: Copyright (c) 2017 + * @version: 0.0.1 + * @modified: yangkai.shen + */ +@Component +@DependsOn("springContextHolder") +public class ConstantFactory implements Constant { + + private MybatisShiroAclMapper aclMapper = SpringContextHolder.getBean(MybatisShiroAclMapper.class); + private MybatisShiroDeptMapper deptMapper = SpringContextHolder.getBean(MybatisShiroDeptMapper.class); + private MybatisShiroUserMapper userMapper = SpringContextHolder.getBean(MybatisShiroUserMapper.class); + private MybatisShiroRoleMapper roleMapper = SpringContextHolder.getBean(MybatisShiroRoleMapper.class); + private MybatisShiroRoleUserMapper roleUserMapper = SpringContextHolder.getBean(MybatisShiroRoleUserMapper.class); + + + public static Constant me() { + return SpringContextHolder.getBean("constantFactory"); + } + + @Override + public String getRealNameById(Integer userId) { + MybatisShiroUser user = userMapper.selectByPrimaryKey(userId); + if (user != null) { + return user.getRealname(); + } + return null; + } + + @Override + public String getUsernameById(Integer userId) { + MybatisShiroUser user = userMapper.selectByPrimaryKey(userId); + if (user != null && StrUtil.isNotEmpty(user.getUsername())) { + return user.getUsername(); + } + return null; + } + + @Override + public String getRoleName(Integer roleId) { + MybatisShiroRole role = roleMapper.selectByPrimaryKey(roleId); + if (role != null && StrUtil.isNotEmpty(role.getName())) { + return role.getName(); + } + return null; + } + + @Override + public List+ * 权限状态的枚举类 + *
+ * + * @package: com.xkcoding.springbootdemorabcshiromybatis.enums + * @description: 权限状态的枚举类 + * @author: yangkai.shen + * @date: Created in 2017/12/6 下午3:38 + * @copyright: Copyright (c) 2017 + * @version: 0.0.1 + * @modified: yangkai.shen + */ +@Getter +public enum AclStatusEnum { + DISABLE(0, "禁用"), ENABLE(1, "启用"); + + private Integer code; + private String message; + + AclStatusEnum(Integer code, String message) { + this.code = code; + this.message = message; + } + + /** + * 根据状态码返回状态 + * + * @param code 状态码 + * @return 状态 + */ + public static String valueOf(Integer code) { + if (code == null) { + return null; + } else { + for (AclStatusEnum statusEnum : AclStatusEnum.values()) { + if (statusEnum.getCode().equals(code)) { + return statusEnum.getMessage(); + } + } + return null; + } + } +} diff --git a/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/enums/UserStatusEnum.java b/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/enums/UserStatusEnum.java new file mode 100644 index 0000000..27f1a28 --- /dev/null +++ b/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/enums/UserStatusEnum.java @@ -0,0 +1,48 @@ +package com.xkcoding.springbootdemorabcshiromybatis.enums; + +import lombok.Getter; + +/** + *+ * 用户状态的枚举类 + *
+ * + * @package: com.xkcoding.springbootdemorabcshiromybatis.enums + * @description: 用户状态的枚举类 + * @author: yangkai.shen + * @date: Created in 2017/12/6 下午3:38 + * @copyright: Copyright (c) 2017 + * @version: 0.0.1 + * @modified: yangkai.shen + */ +@Getter +public enum UserStatusEnum { + DELETED(-1, "已删除"), DISABLE(0, "禁用"), ENABLE(1, "启用"); + + private Integer code; + private String message; + + UserStatusEnum(Integer code, String message) { + this.code = code; + this.message = message; + } + + /** + * 根据状态码返回状态 + * + * @param code 状态码 + * @return 状态 + */ + public static String valueOf(Integer code) { + if (code == null) { + return null; + } else { + for (UserStatusEnum statusEnum : UserStatusEnum.values()) { + if (statusEnum.getCode().equals(code)) { + return statusEnum.getMessage(); + } + } + return null; + } + } +} diff --git a/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/model/MybatisShiroAcl.java b/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/model/MybatisShiroAcl.java new file mode 100644 index 0000000..70d2a5c --- /dev/null +++ b/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/model/MybatisShiroAcl.java @@ -0,0 +1,334 @@ +package com.xkcoding.springbootdemorabcshiromybatis.model; + +import java.util.Date; +import javax.persistence.*; + +@Table(name = "mybatis_shiro_acl") +public class MybatisShiroAcl { + /** + * 权限id + */ + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + /** + * 权限编号 + */ + private String code; + + /** + * 权限名称 + */ + private String name; + + /** + * 权限图标 + */ + private String icon; + + /** + * 上级权限id + */ + @Column(name = "parent_id") + private Integer parentId; + + /** + * 权限层级,默认为0,层级直接用逗号(半角)隔开 + */ + private String level; + + /** + * 权限的url, 可以填正则表达式 + */ + private String url; + + /** + * 类型,1:菜单,2:按钮,3:其他 + */ + private Integer type; + + /** + * 状态,0:冻结,1:正常 + */ + private Integer status; + + /** + * 权限在当前层级下的顺序,由小到大 + */ + private Integer seq; + + /** + * 备注 + */ + private String remark; + + /** + * 操作者 + */ + private String operator; + + /** + * 最后一次更新时间 + */ + @Column(name = "operate_time") + private Date operateTime; + + /** + * 最后一次更新操作者的ip地址 + */ + @Column(name = "operate_ip") + private String operateIp; + + /** + * 获取权限id + * + * @return id - 权限id + */ + public Integer getId() { + return id; + } + + /** + * 设置权限id + * + * @param id 权限id + */ + public void setId(Integer id) { + this.id = id; + } + + /** + * 获取权限编号 + * + * @return code - 权限编号 + */ + public String getCode() { + return code; + } + + /** + * 设置权限编号 + * + * @param code 权限编号 + */ + public void setCode(String code) { + this.code = code; + } + + /** + * 获取权限名称 + * + * @return name - 权限名称 + */ + public String getName() { + return name; + } + + /** + * 设置权限名称 + * + * @param name 权限名称 + */ + public void setName(String name) { + this.name = name; + } + + /** + * 获取权限图标 + * + * @return icon - 权限图标 + */ + public String getIcon() { + return icon; + } + + /** + * 设置权限图标 + * + * @param icon 权限图标 + */ + public void setIcon(String icon) { + this.icon = icon; + } + + /** + * 获取上级权限id + * + * @return parent_id - 上级权限id + */ + public Integer getParentId() { + return parentId; + } + + /** + * 设置上级权限id + * + * @param parentId 上级权限id + */ + public void setParentId(Integer parentId) { + this.parentId = parentId; + } + + /** + * 获取权限层级,默认为0,层级直接用逗号(半角)隔开 + * + * @return level - 权限层级,默认为0,层级直接用逗号(半角)隔开 + */ + public String getLevel() { + return level; + } + + /** + * 设置权限层级,默认为0,层级直接用逗号(半角)隔开 + * + * @param level 权限层级,默认为0,层级直接用逗号(半角)隔开 + */ + public void setLevel(String level) { + this.level = level; + } + + /** + * 获取权限的url, 可以填正则表达式 + * + * @return url - 权限的url, 可以填正则表达式 + */ + public String getUrl() { + return url; + } + + /** + * 设置权限的url, 可以填正则表达式 + * + * @param url 权限的url, 可以填正则表达式 + */ + public void setUrl(String url) { + this.url = url; + } + + /** + * 获取类型,1:菜单,2:按钮,3:其他 + * + * @return type - 类型,1:菜单,2:按钮,3:其他 + */ + public Integer getType() { + return type; + } + + /** + * 设置类型,1:菜单,2:按钮,3:其他 + * + * @param type 类型,1:菜单,2:按钮,3:其他 + */ + public void setType(Integer type) { + this.type = type; + } + + /** + * 获取状态,0:冻结,1:正常 + * + * @return status - 状态,0:冻结,1:正常 + */ + public Integer getStatus() { + return status; + } + + /** + * 设置状态,0:冻结,1:正常 + * + * @param status 状态,0:冻结,1:正常 + */ + public void setStatus(Integer status) { + this.status = status; + } + + /** + * 获取权限在当前层级下的顺序,由小到大 + * + * @return seq - 权限在当前层级下的顺序,由小到大 + */ + public Integer getSeq() { + return seq; + } + + /** + * 设置权限在当前层级下的顺序,由小到大 + * + * @param seq 权限在当前层级下的顺序,由小到大 + */ + public void setSeq(Integer seq) { + this.seq = seq; + } + + /** + * 获取备注 + * + * @return remark - 备注 + */ + public String getRemark() { + return remark; + } + + /** + * 设置备注 + * + * @param remark 备注 + */ + public void setRemark(String remark) { + this.remark = remark; + } + + /** + * 获取操作者 + * + * @return operator - 操作者 + */ + public String getOperator() { + return operator; + } + + /** + * 设置操作者 + * + * @param operator 操作者 + */ + public void setOperator(String operator) { + this.operator = operator; + } + + /** + * 获取最后一次更新时间 + * + * @return operate_time - 最后一次更新时间 + */ + public Date getOperateTime() { + return operateTime; + } + + /** + * 设置最后一次更新时间 + * + * @param operateTime 最后一次更新时间 + */ + public void setOperateTime(Date operateTime) { + this.operateTime = operateTime; + } + + /** + * 获取最后一次更新操作者的ip地址 + * + * @return operate_ip - 最后一次更新操作者的ip地址 + */ + public String getOperateIp() { + return operateIp; + } + + /** + * 设置最后一次更新操作者的ip地址 + * + * @param operateIp 最后一次更新操作者的ip地址 + */ + public void setOperateIp(String operateIp) { + this.operateIp = operateIp; + } +} \ No newline at end of file diff --git a/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/model/MybatisShiroDept.java b/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/model/MybatisShiroDept.java new file mode 100644 index 0000000..27c9c34 --- /dev/null +++ b/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/model/MybatisShiroDept.java @@ -0,0 +1,219 @@ +package com.xkcoding.springbootdemorabcshiromybatis.model; + +import java.util.Date; +import javax.persistence.*; + +@Table(name = "mybatis_shiro_dept") +public class MybatisShiroDept { + /** + * 部门id + */ + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + /** + * 部门名称 + */ + private String name; + + /** + * 上级部门id,顶层是0 + */ + @Column(name = "parent_id") + private Integer parentId; + + /** + * 部门层级,默认为0,层级直接用逗号(半角)隔开 + */ + private String level; + + /** + * 部门在当前层级下的顺序,由小到大 + */ + private Integer seq; + + /** + * 备注 + */ + private String remark; + + /** + * 操作者 + */ + private String operator; + + /** + * 最后一次更新时间 + */ + @Column(name = "operate_time") + private Date operateTime; + + /** + * 最后一次更新操作者的ip地址 + */ + @Column(name = "operate_ip") + private String operateIp; + + /** + * 获取部门id + * + * @return id - 部门id + */ + public Integer getId() { + return id; + } + + /** + * 设置部门id + * + * @param id 部门id + */ + public void setId(Integer id) { + this.id = id; + } + + /** + * 获取部门名称 + * + * @return name - 部门名称 + */ + public String getName() { + return name; + } + + /** + * 设置部门名称 + * + * @param name 部门名称 + */ + public void setName(String name) { + this.name = name; + } + + /** + * 获取上级部门id,顶层是0 + * + * @return parent_id - 上级部门id,顶层是0 + */ + public Integer getParentId() { + return parentId; + } + + /** + * 设置上级部门id,顶层是0 + * + * @param parentId 上级部门id,顶层是0 + */ + public void setParentId(Integer parentId) { + this.parentId = parentId; + } + + /** + * 获取部门层级,默认为0,层级直接用逗号(半角)隔开 + * + * @return level - 部门层级,默认为0,层级直接用逗号(半角)隔开 + */ + public String getLevel() { + return level; + } + + /** + * 设置部门层级,默认为0,层级直接用逗号(半角)隔开 + * + * @param level 部门层级,默认为0,层级直接用逗号(半角)隔开 + */ + public void setLevel(String level) { + this.level = level; + } + + /** + * 获取部门在当前层级下的顺序,由小到大 + * + * @return seq - 部门在当前层级下的顺序,由小到大 + */ + public Integer getSeq() { + return seq; + } + + /** + * 设置部门在当前层级下的顺序,由小到大 + * + * @param seq 部门在当前层级下的顺序,由小到大 + */ + public void setSeq(Integer seq) { + this.seq = seq; + } + + /** + * 获取备注 + * + * @return remark - 备注 + */ + public String getRemark() { + return remark; + } + + /** + * 设置备注 + * + * @param remark 备注 + */ + public void setRemark(String remark) { + this.remark = remark; + } + + /** + * 获取操作者 + * + * @return operator - 操作者 + */ + public String getOperator() { + return operator; + } + + /** + * 设置操作者 + * + * @param operator 操作者 + */ + public void setOperator(String operator) { + this.operator = operator; + } + + /** + * 获取最后一次更新时间 + * + * @return operate_time - 最后一次更新时间 + */ + public Date getOperateTime() { + return operateTime; + } + + /** + * 设置最后一次更新时间 + * + * @param operateTime 最后一次更新时间 + */ + public void setOperateTime(Date operateTime) { + this.operateTime = operateTime; + } + + /** + * 获取最后一次更新操作者的ip地址 + * + * @return operate_ip - 最后一次更新操作者的ip地址 + */ + public String getOperateIp() { + return operateIp; + } + + /** + * 设置最后一次更新操作者的ip地址 + * + * @param operateIp 最后一次更新操作者的ip地址 + */ + public void setOperateIp(String operateIp) { + this.operateIp = operateIp; + } +} \ No newline at end of file diff --git a/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/model/MybatisShiroLog.java b/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/model/MybatisShiroLog.java new file mode 100644 index 0000000..e0826e3 --- /dev/null +++ b/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/model/MybatisShiroLog.java @@ -0,0 +1,221 @@ +package com.xkcoding.springbootdemorabcshiromybatis.model; + +import java.util.Date; +import javax.persistence.*; + +@Table(name = "mybatis_shiro_log") +public class MybatisShiroLog { + /** + * 日志记录id + */ + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + /** + * 权限更新的类型,1:部门,2:用户,3:权限模块,4:权限,5:角色,6:角色用户关系,7:角色权限关系 + */ + private Integer type; + + /** + * 基于type后指定的对象id,比如用户、权限、角色等表的主键 + */ + @Column(name = "target_id") + private Integer targetId; + + /** + * 操作者 + */ + private String operator; + + /** + * 最后一次更新时间 + */ + @Column(name = "operate_time") + private Date operateTime; + + /** + * 最后一次更新操作者的ip地址 + */ + @Column(name = "operate_ip") + private String operateIp; + + /** + * 当前是否复原过,0:没有复原过,1:复原过 + */ + private Integer status; + + /** + * 旧值 + */ + @Column(name = "old_value") + private String oldValue; + + /** + * 新值 + */ + @Column(name = "new_value") + private String newValue; + + /** + * 获取日志记录id + * + * @return id - 日志记录id + */ + public Integer getId() { + return id; + } + + /** + * 设置日志记录id + * + * @param id 日志记录id + */ + public void setId(Integer id) { + this.id = id; + } + + /** + * 获取权限更新的类型,1:部门,2:用户,3:权限模块,4:权限,5:角色,6:角色用户关系,7:角色权限关系 + * + * @return type - 权限更新的类型,1:部门,2:用户,3:权限模块,4:权限,5:角色,6:角色用户关系,7:角色权限关系 + */ + public Integer getType() { + return type; + } + + /** + * 设置权限更新的类型,1:部门,2:用户,3:权限模块,4:权限,5:角色,6:角色用户关系,7:角色权限关系 + * + * @param type 权限更新的类型,1:部门,2:用户,3:权限模块,4:权限,5:角色,6:角色用户关系,7:角色权限关系 + */ + public void setType(Integer type) { + this.type = type; + } + + /** + * 获取基于type后指定的对象id,比如用户、权限、角色等表的主键 + * + * @return target_id - 基于type后指定的对象id,比如用户、权限、角色等表的主键 + */ + public Integer getTargetId() { + return targetId; + } + + /** + * 设置基于type后指定的对象id,比如用户、权限、角色等表的主键 + * + * @param targetId 基于type后指定的对象id,比如用户、权限、角色等表的主键 + */ + public void setTargetId(Integer targetId) { + this.targetId = targetId; + } + + /** + * 获取操作者 + * + * @return operator - 操作者 + */ + public String getOperator() { + return operator; + } + + /** + * 设置操作者 + * + * @param operator 操作者 + */ + public void setOperator(String operator) { + this.operator = operator; + } + + /** + * 获取最后一次更新时间 + * + * @return operate_time - 最后一次更新时间 + */ + public Date getOperateTime() { + return operateTime; + } + + /** + * 设置最后一次更新时间 + * + * @param operateTime 最后一次更新时间 + */ + public void setOperateTime(Date operateTime) { + this.operateTime = operateTime; + } + + /** + * 获取最后一次更新操作者的ip地址 + * + * @return operate_ip - 最后一次更新操作者的ip地址 + */ + public String getOperateIp() { + return operateIp; + } + + /** + * 设置最后一次更新操作者的ip地址 + * + * @param operateIp 最后一次更新操作者的ip地址 + */ + public void setOperateIp(String operateIp) { + this.operateIp = operateIp; + } + + /** + * 获取当前是否复原过,0:没有复原过,1:复原过 + * + * @return status - 当前是否复原过,0:没有复原过,1:复原过 + */ + public Integer getStatus() { + return status; + } + + /** + * 设置当前是否复原过,0:没有复原过,1:复原过 + * + * @param status 当前是否复原过,0:没有复原过,1:复原过 + */ + public void setStatus(Integer status) { + this.status = status; + } + + /** + * 获取旧值 + * + * @return old_value - 旧值 + */ + public String getOldValue() { + return oldValue; + } + + /** + * 设置旧值 + * + * @param oldValue 旧值 + */ + public void setOldValue(String oldValue) { + this.oldValue = oldValue; + } + + /** + * 获取新值 + * + * @return new_value - 新值 + */ + public String getNewValue() { + return newValue; + } + + /** + * 设置新值 + * + * @param newValue 新值 + */ + public void setNewValue(String newValue) { + this.newValue = newValue; + } +} \ No newline at end of file diff --git a/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/model/MybatisShiroRole.java b/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/model/MybatisShiroRole.java new file mode 100644 index 0000000..0358321 --- /dev/null +++ b/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/model/MybatisShiroRole.java @@ -0,0 +1,195 @@ +package com.xkcoding.springbootdemorabcshiromybatis.model; + +import java.util.Date; +import javax.persistence.*; + +@Table(name = "mybatis_shiro_role") +public class MybatisShiroRole { + /** + * 角色id + */ + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + /** + * 角色名称 + */ + private String name; + + /** + * 角色的类型,1:管理员角色,2:其他 + */ + private Integer type; + + /** + * 状态,0:冻结,1:可用 + */ + private Integer status; + + /** + * 备注 + */ + private String remark; + + /** + * 操作者 + */ + private String operator; + + /** + * 最后一次更新时间 + */ + @Column(name = "operate_time") + private Date operateTime; + + /** + * 最后一次更新操作者的ip地址 + */ + @Column(name = "operate_ip") + private String operateIp; + + /** + * 获取角色id + * + * @return id - 角色id + */ + public Integer getId() { + return id; + } + + /** + * 设置角色id + * + * @param id 角色id + */ + public void setId(Integer id) { + this.id = id; + } + + /** + * 获取角色名称 + * + * @return name - 角色名称 + */ + public String getName() { + return name; + } + + /** + * 设置角色名称 + * + * @param name 角色名称 + */ + public void setName(String name) { + this.name = name; + } + + /** + * 获取角色的类型,1:管理员角色,2:其他 + * + * @return type - 角色的类型,1:管理员角色,2:其他 + */ + public Integer getType() { + return type; + } + + /** + * 设置角色的类型,1:管理员角色,2:其他 + * + * @param type 角色的类型,1:管理员角色,2:其他 + */ + public void setType(Integer type) { + this.type = type; + } + + /** + * 获取状态,0:冻结,1:可用 + * + * @return status - 状态,0:冻结,1:可用 + */ + public Integer getStatus() { + return status; + } + + /** + * 设置状态,0:冻结,1:可用 + * + * @param status 状态,0:冻结,1:可用 + */ + public void setStatus(Integer status) { + this.status = status; + } + + /** + * 获取备注 + * + * @return remark - 备注 + */ + public String getRemark() { + return remark; + } + + /** + * 设置备注 + * + * @param remark 备注 + */ + public void setRemark(String remark) { + this.remark = remark; + } + + /** + * 获取操作者 + * + * @return operator - 操作者 + */ + public String getOperator() { + return operator; + } + + /** + * 设置操作者 + * + * @param operator 操作者 + */ + public void setOperator(String operator) { + this.operator = operator; + } + + /** + * 获取最后一次更新时间 + * + * @return operate_time - 最后一次更新时间 + */ + public Date getOperateTime() { + return operateTime; + } + + /** + * 设置最后一次更新时间 + * + * @param operateTime 最后一次更新时间 + */ + public void setOperateTime(Date operateTime) { + this.operateTime = operateTime; + } + + /** + * 获取最后一次更新操作者的ip地址 + * + * @return operate_ip - 最后一次更新操作者的ip地址 + */ + public String getOperateIp() { + return operateIp; + } + + /** + * 设置最后一次更新操作者的ip地址 + * + * @param operateIp 最后一次更新操作者的ip地址 + */ + public void setOperateIp(String operateIp) { + this.operateIp = operateIp; + } +} \ No newline at end of file diff --git a/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/model/MybatisShiroRoleAcl.java b/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/model/MybatisShiroRoleAcl.java new file mode 100644 index 0000000..aaeab53 --- /dev/null +++ b/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/model/MybatisShiroRoleAcl.java @@ -0,0 +1,151 @@ +package com.xkcoding.springbootdemorabcshiromybatis.model; + +import java.util.Date; +import javax.persistence.*; + +@Table(name = "mybatis_shiro_role_acl") +public class MybatisShiroRoleAcl { + /** + * 角色-权限id + */ + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + /** + * 角色id + */ + @Column(name = "role_id") + private Integer roleId; + + /** + * 权限id + */ + @Column(name = "acl_id") + private Integer aclId; + + /** + * 操作者 + */ + private String operator; + + /** + * 最后一次更新时间 + */ + @Column(name = "operate_time") + private Date operateTime; + + /** + * 最后一次更新操作者的ip地址 + */ + @Column(name = "operate_ip") + private String operateIp; + + /** + * 获取角色-权限id + * + * @return id - 角色-权限id + */ + public Integer getId() { + return id; + } + + /** + * 设置角色-权限id + * + * @param id 角色-权限id + */ + public void setId(Integer id) { + this.id = id; + } + + /** + * 获取角色id + * + * @return role_id - 角色id + */ + public Integer getRoleId() { + return roleId; + } + + /** + * 设置角色id + * + * @param roleId 角色id + */ + public void setRoleId(Integer roleId) { + this.roleId = roleId; + } + + /** + * 获取权限id + * + * @return acl_id - 权限id + */ + public Integer getAclId() { + return aclId; + } + + /** + * 设置权限id + * + * @param aclId 权限id + */ + public void setAclId(Integer aclId) { + this.aclId = aclId; + } + + /** + * 获取操作者 + * + * @return operator - 操作者 + */ + public String getOperator() { + return operator; + } + + /** + * 设置操作者 + * + * @param operator 操作者 + */ + public void setOperator(String operator) { + this.operator = operator; + } + + /** + * 获取最后一次更新时间 + * + * @return operate_time - 最后一次更新时间 + */ + public Date getOperateTime() { + return operateTime; + } + + /** + * 设置最后一次更新时间 + * + * @param operateTime 最后一次更新时间 + */ + public void setOperateTime(Date operateTime) { + this.operateTime = operateTime; + } + + /** + * 获取最后一次更新操作者的ip地址 + * + * @return operate_ip - 最后一次更新操作者的ip地址 + */ + public String getOperateIp() { + return operateIp; + } + + /** + * 设置最后一次更新操作者的ip地址 + * + * @param operateIp 最后一次更新操作者的ip地址 + */ + public void setOperateIp(String operateIp) { + this.operateIp = operateIp; + } +} \ No newline at end of file diff --git a/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/model/MybatisShiroRoleUser.java b/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/model/MybatisShiroRoleUser.java new file mode 100644 index 0000000..1ea2885 --- /dev/null +++ b/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/model/MybatisShiroRoleUser.java @@ -0,0 +1,151 @@ +package com.xkcoding.springbootdemorabcshiromybatis.model; + +import java.util.Date; +import javax.persistence.*; + +@Table(name = "mybatis_shiro_role_user") +public class MybatisShiroRoleUser { + /** + * 角色-用户id + */ + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + /** + * 角色id + */ + @Column(name = "role_id") + private Integer roleId; + + /** + * 用户id + */ + @Column(name = "user_id") + private Integer userId; + + /** + * 操作者 + */ + private String operator; + + /** + * 最后一次更新时间 + */ + @Column(name = "operate_time") + private Date operateTime; + + /** + * 最后一次更新操作者的ip地址 + */ + @Column(name = "operate_ip") + private String operateIp; + + /** + * 获取角色-用户id + * + * @return id - 角色-用户id + */ + public Integer getId() { + return id; + } + + /** + * 设置角色-用户id + * + * @param id 角色-用户id + */ + public void setId(Integer id) { + this.id = id; + } + + /** + * 获取角色id + * + * @return role_id - 角色id + */ + public Integer getRoleId() { + return roleId; + } + + /** + * 设置角色id + * + * @param roleId 角色id + */ + public void setRoleId(Integer roleId) { + this.roleId = roleId; + } + + /** + * 获取用户id + * + * @return user_id - 用户id + */ + public Integer getUserId() { + return userId; + } + + /** + * 设置用户id + * + * @param userId 用户id + */ + public void setUserId(Integer userId) { + this.userId = userId; + } + + /** + * 获取操作者 + * + * @return operator - 操作者 + */ + public String getOperator() { + return operator; + } + + /** + * 设置操作者 + * + * @param operator 操作者 + */ + public void setOperator(String operator) { + this.operator = operator; + } + + /** + * 获取最后一次更新时间 + * + * @return operate_time - 最后一次更新时间 + */ + public Date getOperateTime() { + return operateTime; + } + + /** + * 设置最后一次更新时间 + * + * @param operateTime 最后一次更新时间 + */ + public void setOperateTime(Date operateTime) { + this.operateTime = operateTime; + } + + /** + * 获取最后一次更新操作者的ip地址 + * + * @return operate_ip - 最后一次更新操作者的ip地址 + */ + public String getOperateIp() { + return operateIp; + } + + /** + * 设置最后一次更新操作者的ip地址 + * + * @param operateIp 最后一次更新操作者的ip地址 + */ + public void setOperateIp(String operateIp) { + this.operateIp = operateIp; + } +} \ No newline at end of file diff --git a/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/model/MybatisShiroUser.java b/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/model/MybatisShiroUser.java new file mode 100644 index 0000000..4317b07 --- /dev/null +++ b/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/model/MybatisShiroUser.java @@ -0,0 +1,382 @@ +package com.xkcoding.springbootdemorabcshiromybatis.model; + +import java.util.Date; +import javax.persistence.*; + +@Table(name = "mybatis_shiro_user") +public class MybatisShiroUser { + /** + * 用户id + */ + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + /** + * 用户名 + */ + private String username; + + /** + * 加密后的密码 + */ + private String password; + + /** + * 密码加密的盐值 + */ + private String salt; + + /** + * 头像 + */ + private String avatar; + + /** + * 真实姓名 + */ + private String realname; + + /** + * 手机号 + */ + private String telephone; + + /** + * 邮箱 + */ + private String mail; + + /** + * 用户所在部门的id + */ + @Column(name = "dept_id") + private Integer deptId; + + /** + * 状态,-1:删除,0:冻结状态,1:正常 + */ + private Integer status; + + /** + * 备注 + */ + private String remark; + + /** + * 最后一次登录的时间 + */ + @Column(name = "last_time") + private Date lastTime; + + /** + * 最后一次登录的ip地址 + */ + @Column(name = "last_ip") + private String lastIp; + + /** + * 操作者 + */ + private String operator; + + /** + * 最后一次更新时间 + */ + @Column(name = "operate_time") + private Date operateTime; + + /** + * 最后一次更新操作者的ip地址 + */ + @Column(name = "operate_ip") + private String operateIp; + + /** + * 获取用户id + * + * @return id - 用户id + */ + public Integer getId() { + return id; + } + + /** + * 设置用户id + * + * @param id 用户id + */ + public void setId(Integer id) { + this.id = id; + } + + /** + * 获取用户名 + * + * @return username - 用户名 + */ + public String getUsername() { + return username; + } + + /** + * 设置用户名 + * + * @param username 用户名 + */ + public void setUsername(String username) { + this.username = username; + } + + /** + * 获取加密后的密码 + * + * @return password - 加密后的密码 + */ + public String getPassword() { + return password; + } + + /** + * 设置加密后的密码 + * + * @param password 加密后的密码 + */ + public void setPassword(String password) { + this.password = password; + } + + /** + * 获取密码加密的盐值 + * + * @return salt - 密码加密的盐值 + */ + public String getSalt() { + return salt; + } + + /** + * 设置密码加密的盐值 + * + * @param salt 密码加密的盐值 + */ + public void setSalt(String salt) { + this.salt = salt; + } + + /** + * 获取头像 + * + * @return avatar - 头像 + */ + public String getAvatar() { + return avatar; + } + + /** + * 设置头像 + * + * @param avatar 头像 + */ + public void setAvatar(String avatar) { + this.avatar = avatar; + } + + /** + * 获取真实姓名 + * + * @return realname - 真实姓名 + */ + public String getRealname() { + return realname; + } + + /** + * 设置真实姓名 + * + * @param realname 真实姓名 + */ + public void setRealname(String realname) { + this.realname = realname; + } + + /** + * 获取手机号 + * + * @return telephone - 手机号 + */ + public String getTelephone() { + return telephone; + } + + /** + * 设置手机号 + * + * @param telephone 手机号 + */ + public void setTelephone(String telephone) { + this.telephone = telephone; + } + + /** + * 获取邮箱 + * + * @return mail - 邮箱 + */ + public String getMail() { + return mail; + } + + /** + * 设置邮箱 + * + * @param mail 邮箱 + */ + public void setMail(String mail) { + this.mail = mail; + } + + /** + * 获取用户所在部门的id + * + * @return dept_id - 用户所在部门的id + */ + public Integer getDeptId() { + return deptId; + } + + /** + * 设置用户所在部门的id + * + * @param deptId 用户所在部门的id + */ + public void setDeptId(Integer deptId) { + this.deptId = deptId; + } + + /** + * 获取状态,-1:删除,0:冻结状态,1:正常 + * + * @return status - 状态,-1:删除,0:冻结状态,1:正常 + */ + public Integer getStatus() { + return status; + } + + /** + * 设置状态,-1:删除,0:冻结状态,1:正常 + * + * @param status 状态,-1:删除,0:冻结状态,1:正常 + */ + public void setStatus(Integer status) { + this.status = status; + } + + /** + * 获取备注 + * + * @return remark - 备注 + */ + public String getRemark() { + return remark; + } + + /** + * 设置备注 + * + * @param remark 备注 + */ + public void setRemark(String remark) { + this.remark = remark; + } + + /** + * 获取最后一次登录的时间 + * + * @return last_time - 最后一次登录的时间 + */ + public Date getLastTime() { + return lastTime; + } + + /** + * 设置最后一次登录的时间 + * + * @param lastTime 最后一次登录的时间 + */ + public void setLastTime(Date lastTime) { + this.lastTime = lastTime; + } + + /** + * 获取最后一次登录的ip地址 + * + * @return last_ip - 最后一次登录的ip地址 + */ + public String getLastIp() { + return lastIp; + } + + /** + * 设置最后一次登录的ip地址 + * + * @param lastIp 最后一次登录的ip地址 + */ + public void setLastIp(String lastIp) { + this.lastIp = lastIp; + } + + /** + * 获取操作者 + * + * @return operator - 操作者 + */ + public String getOperator() { + return operator; + } + + /** + * 设置操作者 + * + * @param operator 操作者 + */ + public void setOperator(String operator) { + this.operator = operator; + } + + /** + * 获取最后一次更新时间 + * + * @return operate_time - 最后一次更新时间 + */ + public Date getOperateTime() { + return operateTime; + } + + /** + * 设置最后一次更新时间 + * + * @param operateTime 最后一次更新时间 + */ + public void setOperateTime(Date operateTime) { + this.operateTime = operateTime; + } + + /** + * 获取最后一次更新操作者的ip地址 + * + * @return operate_ip - 最后一次更新操作者的ip地址 + */ + public String getOperateIp() { + return operateIp; + } + + /** + * 设置最后一次更新操作者的ip地址 + * + * @param operateIp 最后一次更新操作者的ip地址 + */ + public void setOperateIp(String operateIp) { + this.operateIp = operateIp; + } +} \ No newline at end of file diff --git a/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/shiro/MyShiroRealm.java b/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/shiro/MyShiroRealm.java new file mode 100644 index 0000000..8ffbb63 --- /dev/null +++ b/spring-boot-demo-rabc-shiro-mybatis/src/main/java/com/xkcoding/springbootdemorabcshiromybatis/shiro/MyShiroRealm.java @@ -0,0 +1,96 @@ +package com.xkcoding.springbootdemorabcshiromybatis.shiro; + +import com.google.common.collect.Sets; +import com.xiaoleilu.hutool.util.StrUtil; +import com.xkcoding.springbootdemorabcshiromybatis.dao.MybatisShiroUserMapper; +import com.xkcoding.springbootdemorabcshiromybatis.model.MybatisShiroUser; +import com.xkcoding.springbootdemorabcshiromybatis.shiro.factory.Shiro; +import com.xkcoding.springbootdemorabcshiromybatis.shiro.factory.ShiroFactroy; +import lombok.extern.slf4j.Slf4j; +import org.apache.shiro.authc.*; +import org.apache.shiro.authc.credential.CredentialsMatcher; +import org.apache.shiro.authc.credential.HashedCredentialsMatcher; +import org.apache.shiro.authz.AuthorizationInfo; +import org.apache.shiro.authz.SimpleAuthorizationInfo; +import org.apache.shiro.realm.AuthorizingRealm; +import org.apache.shiro.subject.PrincipalCollection; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.List; +import java.util.Set; + +/** + * shiro 身份校验 + * + * @package: com.xkcoding.springbootdemorabcshiromybatis.shiro + * @description: shiro 身份校验 + * @author: yangkai.shen + * @date: Created in 2017/11/29 下午3:39 + * @copyright: Copyright (c) 2017 + * @version: 0.0.1 + * @modified: yangkai.shen + */ +@Slf4j +public class MyShiroRealm extends AuthorizingRealm { + @Autowired + private MybatisShiroUserMapper mybatisShiroUserMapper; + + /** + * 身份认证: Authentication 用来验证用户信息 + * + * @param authenticationToken + * @return + * @throws AuthenticationException + */ + @Override + protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { + log.info("【身份认证】:进入doGetAuthenticationInfo()"); + + Shiro shiroFactory = ShiroFactroy.me(); + UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken; + MybatisShiroUser user = shiroFactory.user(token.getUsername()); + ShiroUser shiroUser = shiroFactory.shiroUser(user); + SimpleAuthenticationInfo info = shiroFactory.info(shiroUser, user, super.getName()); + return info; + + } + + /** + * 授权验证 + * + * @param principalCollection + * @return + */ + @Override + protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { + log.info("【授权验证】:进入doGetAuthorizationInfo()"); + Shiro shiroFactory = ShiroFactroy.me(); + ShiroUser shiroUser = (ShiroUser) principalCollection.getPrimaryPrincipal(); + List+ * 自定义Authentication对象,使得Subject除了携带用户的登录名外还可以携带更多信息 + *
+ * + * @package: com.xkcoding.springbootdemorabcshiromybatis.shiro + * @description: 自定义Authentication对象,使得Subject除了携带用户的登录名外还可以携带更多信息 + * @author: yangkai.shen + * @date: Created in 2017/12/6 下午3:26 + * @copyright: Copyright (c) 2017 + * @version: 0.0.1 + * @modified: yangkai.shen + */ +@Data +public class ShiroUser implements Serializable { + + private static final long serialVersionUID = 1L; + + private Integer id; + private String username; + private String realname; + private Integer deptId; + private String deptName; + private List+ * shiro 工具类 + *
+ * + * @package: com.xkcoding.springbootdemorabcshiromybatis.shiro + * @description: shiro 工具类 + * @author: yangkai.shen + * @date: Created in 2017/12/1 下午6:02 + * @copyright: Copyright (c) 2017 + * @version: 0.0.1 + * @modified: yangkai.shen + */ +public class ShiroUtil { + + /** + * 加盐参数 + */ + public final static String HASH_ALGORITHM_NAME = "MD5"; + + /** + * 循环次数 + */ + public final static int HASH_ITERATIONS = 1; + + /** + * shiro密码加密工具类 + * + * @param credentials 密码 + * @param saltSource 密码盐 + * @return 加密后的字符串 + */ + public static String md5(String credentials, String saltSource) { + ByteSource salt = new Md5Hash(saltSource); + return new SimpleHash(HASH_ALGORITHM_NAME, credentials, salt, HASH_ITERATIONS).toString(); + } + + /** + * 获取随机盐值 + * + * @return 获取随机盐值 + */ + public static String getRandomSalt() { + return T.UUID(); + } + + /** + * 获取当前 Subject + * + * @return Subject + */ + public static Subject getSubject() { + return SecurityUtils.getSubject(); + } + + /** + * 获取封装的 ShiroUser + * + * @return ShiroUser + */ + public static ShiroUser getUser() { + if (isGuest()) { + return null; + } else { + return (ShiroUser) getSubject().getPrincipals().getPrimaryPrincipal(); + } + } + + /** + * 从shiro获取session + */ + public static Session getSession() { + return getSubject().getSession(); + } + + /** + * 获取shiro指定的sessionKey + */ + @SuppressWarnings("unchecked") + public static+ * 定义 shiro realm 所需数据的接口 + *
+ * + * @package: com.xkcoding.springbootdemorabcshiromybatis.shiro.factory + * @description: 定义 shiro realm 所需数据的接口 + * @author: yangkai.shen + * @date: Created in 2017/12/6 下午3:24 + * @copyright: Copyright (c) 2017 + * @version: 0.0.1 + * @modified: yangkai.shen + */ +public interface Shiro { + + /** + * 根据用户名数据库存储的用户信息 + * + * @param username 用户名 + * @return 数据库存储的用户信息 + */ + MybatisShiroUser user(String username); + + /** + * 根据系统用户获取 shiro 的用户 + * + * @param user 数据库保存的用户 + * @return 自定义的用户对象 + */ + ShiroUser shiroUser(MybatisShiroUser user); + + /** + * 根据角色id获取权限列表 + * + * @param roleId 角色id + * @return 权限列表 + */ + List+ * Spring 的 ApplicationContext 的持有者,可以用静态方法的方式获取 spring 容器中的 bean + *
+ * + * @package: com.xkcoding.springbootdemorabcshiromybatis.util + * @description: Spring 的 ApplicationContext 的持有者,可以用静态方法的方式获取 spring 容器中的 bean + * @author: yangkai.shen + * @date: Created in 2017/12/1 下午3:47 + * @copyright: Copyright (c) 2017 + * @version: 0.0.1 + * @modified: yangkai.shen + */ +@Component +public class SpringContextHolder implements ApplicationContextAware { + private static ApplicationContext applicationContext; + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + SpringContextHolder.applicationContext = applicationContext; + } + + public static ApplicationContext getApplicationContext() { + assertApplicationContext(); + return applicationContext; + } + + public static+ * 得到中文首字母 + *
+ * + * @package: com.xkcoding.util + * @description: 得到中文首字母 + * @author: yangkai.shen + * @date: Created in 2017/12/1 下午3:52 + * @copyright: Copyright (c) 2017 + * @version: 0.0.1 + * @modified: yangkai.shen + */ +public class PingyinUtil { + public static void main(String[] args) { + String str = "我是一串中文"; + System.out.println("我是一串中文:" + getPYIndexStr(str, true)); + } + + /** + * 返回首字母 + * + * @param strChinese 中文字符串 + * @param bUpCase 是否为大写 + * @return 中文字符串的首字母 + */ + public static String getPYIndexStr(String strChinese, boolean bUpCase) { + try { + StringBuffer buffer = new StringBuffer(); + // 把中文转化成byte数组 + byte b[] = strChinese.getBytes("GBK"); + for (int i = 0; i < b.length; i++) { + if ((b[i] & 255) > 128) { + int char1 = b[i++] & 255; + // 左移运算符用“<<”表示,是将运算符左边的对象,向左移动运算符右边指定的位数,并且在低位补零。其实,向左移n位,就相当于乘上2的n次方 + char1 <<= 8; + int chart = char1 + (b[i] & 255); + buffer.append(getPYIndexChar((char) chart, bUpCase)); + continue; + } + char c = (char) b[i]; + // 确定指定字符是否可以是 Java + if (!Character.isJavaIdentifierPart(c)) { + // 标识符中首字符以外的部分。 + c = 'A'; + } + buffer.append(c); + } + return buffer.toString(); + } catch (Exception e) { + System.out.println((new StringBuilder()).append("\u53D6\u4E2D\u6587\u62FC\u97F3\u6709\u9519").append(e.getMessage()).toString()); + } + return null; + } + + /** + * 得到首字母 + * + * @param strChinese 中文字符 + * @param bUpCase 是否大写 + * @return 中文字符的首字母 + */ + private static char getPYIndexChar(char strChinese, boolean bUpCase) { + + int charGBK = strChinese; + + char result; + + if (charGBK >= 45217 && charGBK <= 45252) { + result = 'A'; + } else if (charGBK >= 45253 && charGBK <= 45760) { + result = 'B'; + } else if (charGBK >= 45761 && charGBK <= 46317) { + result = 'C'; + } else if (charGBK >= 46318 && charGBK <= 46825) { + result = 'D'; + } else if (charGBK >= 46826 && charGBK <= 47009) { + result = 'E'; + } else if (charGBK >= 47010 && charGBK <= 47296) { + result = 'F'; + } else if (charGBK >= 47297 && charGBK <= 47613) { + result = 'G'; + } else if (charGBK >= 47614 && charGBK <= 48118) { + result = 'H'; + } else if (charGBK >= 48119 && charGBK <= 49061) { + result = 'J'; + } else if (charGBK >= 49062 && charGBK <= 49323) { + result = 'K'; + } else if (charGBK >= 49324 && charGBK <= 49895) { + result = 'L'; + } else if (charGBK >= 49896 && charGBK <= 50370) { + result = 'M'; + } else if (charGBK >= 50371 && charGBK <= 50613) { + result = 'N'; + } else if (charGBK >= 50614 && charGBK <= 50621) { + result = 'O'; + } else if (charGBK >= 50622 && charGBK <= 50905) { + result = 'P'; + } else if (charGBK >= 50906 && charGBK <= 51386) { + result = 'Q'; + } else if (charGBK >= 51387 && charGBK <= 51445) { + result = 'R'; + } else if (charGBK >= 51446 && charGBK <= 52217) { + result = 'S'; + } else if (charGBK >= 52218 && charGBK <= 52697) { + result = 'T'; + } else if (charGBK >= 52698 && charGBK <= 52979) { + result = 'W'; + } else if (charGBK >= 52980 && charGBK <= 53688) { + result = 'X'; + } else if (charGBK >= 53689 && charGBK <= 54480) { + result = 'Y'; + } else if (charGBK >= 54481 && charGBK <= 55289) { + result = 'Z'; + } else { + result = (char) (65 + (new Random()).nextInt(25)); + } + if (!bUpCase) { + result = Character.toLowerCase(result); + } + return result; + } +} diff --git a/spring-boot-demo-util/src/main/java/com/xkcoding/util/T.java b/spring-boot-demo-util/src/main/java/com/xkcoding/util/T.java new file mode 100644 index 0000000..95d783b --- /dev/null +++ b/spring-boot-demo-util/src/main/java/com/xkcoding/util/T.java @@ -0,0 +1,63 @@ +package com.xkcoding.util; + +import com.google.common.collect.Maps; +import com.sun.istack.internal.NotNull; +import com.xiaoleilu.hutool.http.HttpUtil; +import com.xiaoleilu.hutool.json.JSONObject; +import com.xiaoleilu.hutool.json.JSONUtil; +import com.xiaoleilu.hutool.util.RandomUtil; +import com.xiaoleilu.hutool.util.StrUtil; +import com.xkcoding.util.domain.ShortUrlRet; + +import java.util.Map; + +/** + *+ * 高频工具类 + *
+ * + * @package: com.xkcoding.util + * @description: 高频工具类 + * @author: yangkai.shen + * @date: Created in 2017/12/1 下午4:24 + * @copyright: Copyright (c) 2017 + * @version: 0.0.1 + * @modified: yangkai.shen + */ +public class T { + private static final String URL_SEPARATOR = "//"; + private static final String URL_PROTOCOL = "http"; + + /** + * 获取 UUID + * + * @return 返回没有“-”的 UUID + */ + public static String UUID() { + return RandomUtil.randomUUID().replace("-", ""); + } + + /** + * 长地址转短地址 + * + * @param longUrl 长地址 + * @return 长地址转化后的短地址 + */ + public static String shortURL(@NotNull String longUrl) { + Map+ * 短地址请求返回类型 + *
+ * + * @package: com.xkcoding.util.domain + * @description: 短地址请求返回类型 + * @author: yangkai.shen + * @date: Created in 2017/12/4 上午11:29 + * @copyright: Copyright (c) 2017 + * @version: 0.0.1 + * @modified: yangkai.shen + */ +@Data +public class ShortUrlRet { + private String short_url; + private String long_encoded; + private String long_decoded; + private String status; +}