【附源码】SpringBoot + MyBatis + Shiro 搭建简易权限系统

0、写在前面的话

一直想能仿公司框架的形式,着手做一个简单的脚手架,一来是带着目标性能更好地学习,接触新的技术,另外自己如果有什么想要实现的简单需求,就可以进行快速开发,主要还是希望能在权限上有所控spring是什么意思制,所以最花时间的还是在Shiro上。

其实目标在github已架构师面试题及答案经有不少大佬的参考物了:

  • zspringcloudheng(https://gitee.com/shuzheng/zheng) 基于Spring+SpringMVC+Mybatis分布式敏捷开发系统架构,提供整套公共微服务服务模块
  • ES(https://githubspring.com/zhangkaitao/es) JavaEE企业级架构师面试题及答案项目的快速开发的脚手架,linux重启命令提供了底层抽象和通用功能,拿来即用
  • renren-security(https://www.renlinux重启命令ren.io/) 轻量级权限管理系统
  • lenos(https://gitee.com/bweird/lenosp) 快速开发模块化脚手架

我自己也试着搭建了最简linux命令单的包含权限的后端,主要是为了走通整个流程,之后也会慢慢linux试着参考大佬们做一款自己的架子。在整个集成过程中,当然不免遇到了各系统运维是干嘛的种奇奇怪怪的问题,这里做一些简单的经验记录,linux虚拟机避免旧坑重踩。

1、技术框架整合

1.1 Maven多模块项目的搭建

参考链接:

  • 为什么linux要搭建多模块项目?
  • Maven最佳实践linux:划分模块(http://juvenshun.iteye.comspring/blog/305865)
  • maven构建企业级多模块项目(最好的划分模块方式)
  • 多模块项目如何搭建?
  • Maven的多模块 Spring MVC + Spring + Mybatis 项目的搭建(http://www.leeyspringbootom.top/2017/08/01/tech-malinux命令ven-multi-module-ssm/)
  • SpringBoot多模块项目实践(Multi-Modlinux常用命令ule)(https://segmentfaulspringboott.com/a/1190000011367492)

1.2 SpringBoot-MyBatis集成

参考链接:

  • Spring Boot 集成MyBatis
  • mybatis-spring-boot-autoconflinux常用命令igure(http://www.mybatis.org/spring-boot-starter/linux是什么操作系统myb架构师和程序员的区别at架构师工资一般多少is-spring-boot-autoconfigure/)

1.3 SpringBoot-Shiro集成

参考链接:

    Shiro用starter方式优雅整合到SpringBoot中(https://segmentfault.com/a/1190000014479154#a架构师是做什么的rticleHeader0)
  • Spring Boot 集成-Shiro
  • Sh架构师是干嘛的iro下简单的RBAC(Realm及相关对象)(httlinux是什么操作系统p://jinnialinux常用命令nshil架构师是做什么的ongnian.iteye.com/blog/2022468)

2、踩坑警告

  • SpringBoot 版本:2.0.3.REL架构师面试题EASE
  • JUnspring是什么意思it 版本:4.12

  • SpringBoot-MyBatis 版本:1.3.2
  • SpringBoot-Shiro 版本:1.4.0-RC2

2springboot面试题.1 多模块带来的注意事项

SpringBoot 多模块的单元测试需要指定注解 @Sprlinux系统安装ingBootTest(classes = {Application.class}),这里的 Application.class 即你的Spring系统/运维Boot启动类,这也就意味着linux是什么操作系统你其他模块的测试也只能在 Application.class 所在的模块中进行,否则编译无法通过因为其他模块找不到 Alinux系统安装pplication.class,当然这是因为其他模块中的依赖问题导致的。linux

另外需要注意的是,SpringBoot中 的 Bean 扫描默认为 Application.java 所在包及子包,所以哪怕是多模块,也请注意包名的问题,并调整 Application.java 的位置,否则很容易出现找不到 Bean 注入的情况。

如果你还linux虚拟机使用了 MyBatis-generator,同样其对于数据源的配置文件,因为多模块的缘故,你可能也无法直接使用 SpringBoot 中 application.propertiesspring漏洞 的配置,需要单独写一个配置文件在 MyBatislinux系统安装-genespring翻译rator架构师面试题 使用的那个模块下。

2.2 SpringBoot+MyBatis与单元测试

如果在单元springboot面试题测试时发现 xxxMapper 或 xxxDao 的 Bean 无法注入,那么请注意springmvc的工作原理你使用的注解了。在持久层接口上注解使用 @Map架构师是干嘛的per,而不是仅仅使用拼多多模块 @Repos架构师和程序员的区别itory。实际上哪怕不使用 @Reposispringboot面试题tory 也可以注入持久层的 Bean,但是IDE会在Service类中报红提醒 xxxDao 没有注册 Bea架构师需要掌握哪些知识n,所以最好还是加上 @Repository,尽管去掉也没有什么架构师一般做到多少岁影响。

@Repository
@Mapper
public interface RoleDao {
int deleteByPrimaryKey(Long id);
int insert(Role record);
int insertSelective(Role record);
Role selectByPrimaryKey(Long id);
int updateByPrimaryKeySelective(Role record);
int updateByPrimaryKey(Role record);
Set<Role> findAll();
Set<Role> findByUserId(Long userId);
}

2.3 Shiro中自定义Realm的Bean注册

在 SpringBoot 和 Shiro 的集成中,Shiro的配置通常是使用一个自定义配置类,通过在方法上使用 @Bean 注解来将配spring漏洞置注册成 Bean,如下:多模块

@Configuration
public class ShiroConfig {
@Bean
public Realm realm() {
return new MyRealm();
}

@Bean
public ShiroFilterChainDefinition shiroFilterChainDefinition() {
DefaultShiroFilterChainDefinition chain = new DefaultShiroFilterChainDefinition();
//todo "/anon" not useful
chain.addPathDefinition("/anon/*", "anon");
chain.addPathDefinition("/authc/*", "authc");
return chain;
}
}

那么在自定义的Realm中还需要单独的注架构师解(如 @Component)标记吗?答案是不需要。如下,哪怕它之中还需要用到其他的 Bean 组件,也不需要再单独做组件注解了(加上反而spring因为和 @Bean 的方式冲突报错):

//无需 @Component
public class MyRealm extends AuthorizingRealm {

@Autowired
private UserService userService;

@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

return null;
}

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

//...
return null;
}
}

另外需要注意的是,在配置url访问权限时,如下两种写法请注意:

  • chain架构师面试题.addPathDefinition("/anon", "anon"); //无效
  • chain.addPathDefinition("/anon/*"springmvc的工作原理, "anospring面试题n"); //有效

3、Demo源spring漏洞linux重启命令

  • https:架构师是做什么的//github.com/deng-cc/baseMan-demo-init

【附源码】SpringBoot + MyBatis + Shiro 搭建简易权限系统