<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)

          共 3864字,需瀏覽 8分鐘

           ·

          2020-11-03 15:32

          點(diǎn)擊上方“JAVA”,星標(biāo)公眾號(hào)

          重磅干貨,第一時(shí)間送達(dá)

          0、寫在前面的話

          一直想能仿公司框架的形式,著手做一個(gè)簡(jiǎn)單的腳手架,一來(lái)是帶著目標(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ā)的腳手架,提供了底層抽象和通用功能,拿來(lái)即用
          • 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è)集成過(guò)程中,當(dāng)然不免遇到了各種奇奇怪怪的問(wèn)題,這里做一些簡(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 多模塊帶來(lái)的注意事項(xiàng)

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

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

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

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

          如果在單元測(cè)試時(shí)發(fā)現(xiàn) xxxMapper 或 xxxDao 的 Bean 無(wú)法注入,那么請(qǐng)注意你使用的注解了。在持久層接口上注解使用 @Mapper,而不是僅僅使用 @Repository。實(shí)際上哪怕不使用 @Repository 也可以注入持久層的 Bean,但是IDE會(huì)在Service類中報(bào)紅提醒 xxxDao 沒(méi)有注冊(cè) Bean,所以最好還是加上 @Repository,盡管去掉也沒(méi)有什么影響。

          @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 findAll();
          Set findByUserId(Long userId);
          }

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

          在 SpringBoot 和 Shiro 的集成中,Shiro的配置通常是使用一個(gè)自定義配置類,通過(guò)在方法上使用 @Bean 注解來(lái)將配置注冊(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ò)):

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

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

          3、Demo源碼

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


          近期熱文:

          【送書】
          IntelliJ IDEA 15款 神級(jí)超級(jí)牛逼插件推薦
          Chrome 靈魂插件!
          一份 Spring Boot 項(xiàng)目搭建模板
          介紹一款賊美的Vue+Element開源后臺(tái)管理UI
          瀏覽 49
          點(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>
                  特级西西人体4444XXXX | aaa国产精品 | 日本六十老龄高熟0930 | 婷婷丁香五月天激情 | 日本免费看黄 |