0、写在前面的话

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

其实目标在github已经有不少大佬的参考物了:
  • zheng  基于Spring+SpringMVC+Mybatis分布式敏捷开发系统架构,提供整套公共微服务服务模块
  • ES  JavaEE企业级项目的快速开发的脚手架,提供了底层抽象和通用功能,拿来即用
  • renren-security  轻量级权限管理系统
  • lenos  快速开发模块化脚手架

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

1、技术框架整合

1.1 Maven多模块项目的搭建

参考链接:

1.2 SpringBoot-MyBatis集成

参考链接:

1.3 SpringBoot-Shiro集成

参考链接:

2、踩坑警告

  • SpringBoot 版本:2.0.3.RELEASE
  • JUnit 版本:4.12
  • SpringBoot-MyBatis 版本:1.3.2
  • SpringBoot-Shiro 版本:1.4.0-RC2

2.1 多模块带来的注意事项

SpringBoot 多模块的单元测试需要指定注解 @SpringBootTest(classes = {Application.class}),这里的 Application.class 即你的SpringBoot启动类,这也就意味着你其他模块的测试也只能在 Application.class 所在的模块中进行,否则编译无法通过因为其他模块找不到 Application.class,当然这是因为其他模块中的依赖问题导致的。

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

如果你还使用了 MyBatis-generator,同样其对于数据源的配置文件,因为多模块的缘故,你可能也无法直接使用 SpringBoot 中 application.properties 的配置,需要单独写一个配置文件在 MyBatis-generator 使用的那个模块下。

2.2 SpringBoot+MyBatis与单元测试

如果在单元测试时发现 xxxMapper 或 xxxDao 的 Bean 无法注入,那么请注意你使用的注解了。在持久层接口上注解使用 @Mapper,而不是仅仅使用 @Repository。实际上哪怕不使用 @Repository 也可以注入持久层的 Bean,但是IDE会在Service类中报红提醒 xxxDao 没有注册 Bean,所以最好还是加上 @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);
}
x
20
 
1
@Repository
2
@Mapper
3
public interface RoleDao {
4

5
    int deleteByPrimaryKey(Long id);
6
    int insert(Role record);
7
    int insertSelective(Role record);
8
    Role selectByPrimaryKey(Long id);
9
    int updateByPrimaryKeySelective(Role record);
10
    int updateByPrimaryKey(Role record);
11
    Set<Role> findAll();
12
    Set<Role> findByUserId(Long userId);
13
}

2.3 Shiro中自定义Realm的Bean注册

在 SpringBoot 和 Shiro 的集成中,Shiro的配置通常是使用一个自定义配置类,通过在方法上使用 @Bean 注解来将配置注册成 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;
} }
x
 
1
@Configuration
2
public class ShiroConfig {
3

4
    @Bean
5
    public Realm realm() {
6
        return new MyRealm();
7
    }
8

9
    @Bean
10
    public ShiroFilterChainDefinition shiroFilterChainDefinition() {
11
        DefaultShiroFilterChainDefinition chain = new DefaultShiroFilterChainDefinition();
12
        //todo "/anon" not useful
13
        chain.addPathDefinition("/anon/*", "anon");
14
        chain.addPathDefinition("/authc/*", "authc");
15
        return chain;
16
    }
17

18
}

那么在自定义的Realm中还需要单独的注解(如 @Component)标记吗?答案是不需要。如下,哪怕它之中还需要用到其他的 Bean 组件,也不需要再单独做组件注解了(加上反而因为和 @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;
} }
x
 
1
//无需 @Component
2
public class MyRealm extends AuthorizingRealm {
3

4
    @Autowired
5
    private UserService userService;
6

7
    @Override
8
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
9
        //...
10
        return null;
11
    }
12

13
    @Override
14
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
15
        //...
16
        return null;
17
    }
18

19
}

另外需要注意的是,在配置url访问权限时,如下两种写法请注意:
  • chain.addPathDefinition("/anon", "anon");      //无效
  • chain.addPathDefinition("/anon/*", "anon");   //有效

3、Demo源码



[03] SpringBoot+MyBatis+Shiro搭建杂谈的更多相关文章

  1. springboot成神之——springboot+mybatis+mysql搭建项目简明demo

    springboot+mybatis+mysql搭建项目简明demo 项目所需目录结构 pom.xml文件配置 application.properties文件配置 MyApplication.jav ...

  2. SpringBoot+Mybatis集成搭建

    本博客介绍一下SpringBoot集成Mybatis,数据库连接池使用alibaba的druid,使用SpringBoot微框架虽然集成Mybatis之后可以不使用xml的方式来写sql,但是用惯了x ...

  3. springboot+mybatis+shiro项目中使用shiro实现登录用户的权限验证。权限表、角色表、用户表。从不同的表中收集用户的权限、

    要实现的目的:根据登录用户.查询出当前用户具有的所有权限.然后登录系统后.根据查询到的权限信息进行不同的操作. 以下的代码是在搭好的框架之下进行的编码. 文章目录 核心实现部分. 第一种是将用户表和角 ...

  4. springboot+mybatis+shiro——shiro简介

    转载:[一]shiro入门 之 Shiro简介 一.shiro介绍: 官方网址:http://shiro.apache.org/introduction.html,shiro的功能包括:认证.授权.加 ...

  5. springboot+mybatis+shiro——登录认证和权限控制

    转载:https://z77z.oschina.io/ 一.引入依赖 shiro-all包含shiro所有的包.shiro-core是核心包.shiro-web是与web整合.shiro-spring ...

  6. springboot + mybatis 前后端分离项目的搭建 适合在学习中的大学生

    人生如戏,戏子多半掉泪! 我是一名大四学生,刚进入一家软件件公司实习,虽说在大学中做过好多个实训项目,都是自己完成,没有组员的配合.但是在这一个月的实习中,我从以前别人教走到了现在的自学,成长很多. ...

  7. Mysql8.0主从复制搭建,shardingsphere+springboot+mybatis读写分离

    1.安装mysql8.0 首先需要在192.167.3.171上安装JDK. 下载mysql安装包,https://dev.mysql.com/downloads/,找到以下页面下载. 下载后放到li ...

  8. Shiro+springboot+mybatis(md5+salt+散列)认证与授权-01

    这个小项目包含了注册与登录,使用了springboot+mybatis+shiro的技术栈:当用户在浏览器登录时发起请求时,首先这一系列的请求会被拦截器进行拦截(ShiroFilter),然后拦截器根 ...

  9. Shiro+springboot+mybatis+EhCache(md5+salt+散列)认证与授权-03

    从上文:Shiro+springboot+mybatis(md5+salt+散列)认证与授权-02 当每次进行刷新时,都会从数据库重新查询数据进行授权操作,这样无疑给数据库造成很大的压力,所以需要引入 ...

随机推荐

  1. Windows 不能在本地计算机启动 OracleDBConsoleorcl的问题解决方法

    解决步骤如下: 1.开始->运行cmd 2.执行 emctl start dbconsole 输入:C:\Documents and Settings\xcl>emctl start db ...

  2. 5步告诉你QQ音乐的完美音质是怎么来的,播放器的秘密都在这里

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由QQ音乐技术团队发表于云+社区专栏 一.问题背景与分析 不久前,团队发现其Android平台App在播放MV视频<凤凰花开的路口 ...

  3. 2014年5月16至24日,杨学明老师为深圳创维RGB事业部提供两天的《软件测试管理》内训服务!

    2014年5月16日和24日,<在软件开发流程中构筑软件质量—软件测试管理>内训课程在深圳创维集团成功举办!来自创维研发.测试.生产等部门的管理人员和核心骨干等参加了此次培训,此次培训由研 ...

  4. [译]在Linux上的提高MySQL/MariaDB安全性的12条建议

    MySQL 是世界上最流行的开源数据库系统,而MariaDB(MySQL的一个分支)是世界上发展最快的开源数据库系统.安装MySQL服务器之后,它的默认配置是不安全的,保护它是一般数据库管理中的基本任 ...

  5. Python GUI编程实例

    import os from time import sleep from tkinter import * from tkinter.messagebox import showinfo class ...

  6. (python)数据结构---集合

    一.描述 set翻译为集合 set是可变的.无序的.不可重复的 set的元素要求可哈西(不可变的数据类型可哈西,可变的数据类型不可哈希) set是无序的,因此不可以索引,也不可以修改 线型结构的查询时 ...

  7. CentOS6.8下MySQL MHA架构搭建笔记

    转载请注明出处,本文地址:http://www.cnblogs.com/ajiangg/p/6552855.html 以下是CentOS6.8下MySQL MHA架构搭建笔记 IP资源规划: 192. ...

  8. PHP is much better than you think

    Rants about PHP are everywhere, and they even come from smart guys.When Jeff Atwood wrote yet anothe ...

  9. 【PAT】B1005 继续(3n+1)猜想

    没有婼姐写得好 将所有的输入放入mp,mp2 覆盖的数存入mp 一开始认为mp中只出现一次的元素就是,忘了可能只被覆盖一次的情况 所以添加了mp2保存输入 #include <iostream& ...

  10. 4.8Python数据处理篇之Matplotlib系列(八)---Figure的学习

    目录 目录 前言 (一)figure()方法的定义 (二)figure()方法的参数 (三)figure()方法的例子 1.多窗体绘图: 2.窗口得分别率 目录 前言 今天我们来学习一下plt.fig ...