<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          【附源碼】SpringBoot + MyBatis + Shiro 搭建簡(jiǎn)易權(quán)限系統(tǒng)

          共 4070字,需瀏覽 9分鐘

           ·

          2021-05-17 11:19

          關(guān)注我們,設(shè)為星標(biāo),每天7:30不見不散,架構(gòu)路上與您共享 

          回復(fù)"架構(gòu)師"獲取資源

          0、寫在前面的話

          一直想能仿公司框架的形式,著手做一個(gè)簡(jiǎn)單的腳手架,一來是帶著目標(biāo)性能更好地學(xué)習(xí),接觸新的技術(shù),另外自己如果有什么想要實(shí)現(xiàn)的簡(jiǎn)單需求,就可以進(jìn)行快速開發(fā),主要還是希望能在權(quán)限上有所控制,所以最花時(shí)間的還是在Shiro上。

          其實(shí)目標(biāo)在github已經(jīng)有不少大佬的參考物了:

          • zheng(https://gitee.com/shuzheng/zheng)  基于Spring+SpringMVC+Mybatis分布式敏捷開發(fā)系統(tǒng)架構(gòu),提供整套公共微服務(wù)服務(wù)模塊
          • ES(https://github.com/zhangkaitao/es)  JavaEE企業(yè)級(jí)項(xiàng)目的快速開發(fā)的腳手架,提供了底層抽象和通用功能,拿來即用
          • renren-security(https://www.renren.io/)  輕量級(jí)權(quán)限管理系統(tǒng)
          • lenos(https://gitee.com/bweird/lenosp)  快速開發(fā)模塊化腳手架

          我自己也試著搭建了最簡(jiǎn)單的包含權(quán)限的后端,主要是為了走通整個(gè)流程,之后也會(huì)慢慢試著參考大佬們做一款自己的架子。在整個(gè)集成過程中,當(dāng)然不免遇到了各種奇奇怪怪的問題,這里做一些簡(jiǎn)單的經(jīng)驗(yàn)記錄,避免舊坑重踩。

          1、技術(shù)框架整合

          1.1 Maven多模塊項(xiàng)目的搭建

          參考鏈接:

          • 為什么要搭建多模塊項(xiàng)目?

          • Maven最佳實(shí)踐:劃分模塊(http://juvenshun.iteye.com/blog/305865)

          • maven構(gòu)建企業(yè)級(jí)多模塊項(xiàng)目(最好的劃分模塊方式)(https://blog.csdn.net/program_guys/article/details/76407360)

          • 多模塊項(xiàng)目如何搭建?

          • Maven的多模塊 Spring MVC + Spring + Mybatis 項(xiàng)目的搭建(http://www.leeyom.top/2017/08/01/tech-maven-multi-module-ssm/)

          • SpringBoot多模塊項(xiàng)目實(shí)踐(Multi-Module)(https://segmentfault.com/a/1190000011367492)

          1.2 SpringBoot-MyBatis集成

          參考鏈接:

          • Spring Boot 集成MyBatis(https://blog.csdn.net/isea533/article/details/50359390)
          • mybatis-spring-boot-autoconfigure(http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/)

          1.3 SpringBoot-Shiro集成

          參考鏈接:

          • Shiro用starter方式優(yōu)雅整合到SpringBoot中(https://segmentfault.com/a/1190000014479154#articleHeader0)
          • Spring Boot 集成-Shiro
          • Shiro下簡(jiǎn)單的RBAC(Realm及相關(guān)對(duì)象)(http://jinnianshilongnian.iteye.com/blog/2022468)

          2、踩坑警告

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

          2.1 多模塊帶來的注意事項(xiàng)

          SpringBoot 多模塊的單元測(cè)試需要指定注解 @SpringBootTest(classes = {Application.class}),這里的 Application.class 即你的SpringBoot啟動(dòng)類,這也就意味著你其他模塊的測(cè)試也只能在 Application.class 所在的模塊中進(jìn)行,否則編譯無法通過因?yàn)槠渌K找不到 Application.class,當(dāng)然這是因?yàn)槠渌K中的依賴問題導(dǎo)致的。

          另外需要注意的是,SpringBoot中 的 Bean 掃描默認(rèn)為 Application.java 所在包及子包,所以哪怕是多模塊,也請(qǐng)注意包名的問題,并調(diào)整 Application.java 的位置,否則很容易出現(xiàn)找不到 Bean 注入的情況。

          如果你還使用了 MyBatis-generator,同樣其對(duì)于數(shù)據(jù)源的配置文件,因?yàn)槎嗄K的緣故,你可能也無法直接使用 SpringBoot 中 application.properties 的配置,需要單獨(dú)寫一個(gè)配置文件在 MyBatis-generator 使用的那個(gè)模塊下。

          2.2 SpringBoot+MyBatis與單元測(cè)試

          如果在單元測(cè)試時(shí)發(fā)現(xiàn) xxxMapper 或 xxxDao 的 Bean 無法注入,那么請(qǐng)注意你使用的注解了。在持久層接口上注解使用 @Mapper,而不是僅僅使用 @Repository。實(shí)際上哪怕不使用 @Repository 也可以注入持久層的 Bean,但是IDE會(huì)在Service類中報(bào)紅提醒 xxxDao 沒有注冊(cè) 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);
          }

          2.3 Shiro中自定義Realm的Bean注冊(cè)

          在 SpringBoot 和 Shiro 的集成中,Shiro的配置通常是使用一個(gè)自定義配置類,通過在方法上使用 @Bean 注解來將配置注冊(cè)成 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中還需要單獨(dú)的注解(如 @Component)標(biāo)記嗎?答案是不需要。如下,哪怕它之中還需要用到其他的 Bean 組件,也不需要再單獨(dú)做組件注解了(加上反而因?yàn)楹?@Bean 的方式?jīng)_突報(bào)錯(cuò)):

           //無需 @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訪問權(quán)限時(shí),如下兩種寫法請(qǐng)注意:

          • chain.addPathDefinition("/anon", "anon");      //無效
          • chain.addPathDefinition("/anon/*", "anon");   //有效

          3、Demo源碼

          • https://github.com/deng-cc/baseMan-demo-init



          到此文章就結(jié)束了。如果今天的文章對(duì)你在進(jìn)階架構(gòu)師的路上有新的啟發(fā)和進(jìn)步,歡迎轉(zhuǎn)發(fā)給更多人。歡迎加入架構(gòu)師社區(qū)技術(shù)交流群,眾多大咖帶你進(jìn)階架構(gòu)師,在后臺(tái)回復(fù)“加群”即可入群。







          這些年小編給你分享過的干貨

          Kubernetes的前世今生

          你們公司的架構(gòu)師是什么樣的?

          《Docker與CI持續(xù)集成/CD持續(xù)部署》

          《還有40天,Java 11就要橫空出世了》

          《JDK 10 的 109 項(xiàng)新特性》

          《學(xué)習(xí)微服務(wù)的十大理由》

          轉(zhuǎn)發(fā)在看就是最大的支持??

          瀏覽 54
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  丝袜日逼| 中国猛少妇xxxx猛交 | 免费国产精品视频 | 国产精品久久久爽爽爽麻豆色哟哟 | 国产精品无码7777777 |