SpringBoot、MyBatis、Shiro、Thymeleaf整合思路

Ddsof_Cai發表於2020-10-07

簡單的闡述一下基本的整合思路,方便後面需要用的時候查詢。

  1. 建立工程
  2. 匯入座標、依賴
<!--        匯入mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
<!--        spring整合mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>
<!--        匯入thymeleaf-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
<!--        匯入shiro-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.6.0</version>
        </dependency>
<!--        匯入lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.22</version>
        </dependency>
<!--        thymeleaf整合shiro-->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-shiro</artifactId>
            <version>2.0.0</version>
        </dependency>
  1. 配置
    application.yml
    注意mapper的掃描位置
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.demo.test.pojo

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
  1. 建立pojo、dao、mapper、service、serviceimpl、controller
  • pojo類的註解有
    @Data :配置常見的get、set
    @AllArgsConstructor :帶引數建構函式
    @NoArgsConstructor: 無引數建構函式

  • dao介面注意**@Mapper**註解的使用

  • mapper檔案的namespace名稱空間只要指向dao介面

  • serviceimpl類必須配置**@Service註解,有事務的新增@Transactional**

  1. 配置shiro
    自定義類ShiroCofig,新增@Configuration註解
    (倒敘配置比較方便)
  • ShiroFilterFactoryBean :配置攔截
    在這裡插入圖片描述

  • DefaultWebSecurityManager
    在這裡插入圖片描述

  • UserRealm 配置自定義的Realm繼承AuthorizingRealm重寫
    在這裡插入圖片描述

doGetAuthorizationInfo:授權

 @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        
        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
        //拿到當前登入的物件
        Subject subject = SecurityUtils.getSubject();
        User user = (User)subject.getPrincipal();
        //新增使用者許可權
        info.addStringPermission(user.getPerms());
        return info;
    }

doGetAuthenticationInfo:認證
(可以自行新增鹽制加密)

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

        UsernamePasswordToken usertoken=(UsernamePasswordToken) token;
        User user = userService.selectUserByName(usertoken.getUsername());
        if (user==null){
            return  null;
        }

        //設定Session
        Subject current = SecurityUtils.getSubject();
        Session session = current.getSession();
        session.setAttribute("USER_SESSION",user);
        return new SimpleAuthenticationInfo(user,user.getPassword(),"");
    }
  • ShiroDialect :用來整合shiro和thymeleaf
    在這裡插入圖片描述
  1. 啟動類
    別忘了@ComponentScan的註解
@SpringBootApplication
@ComponentScan(basePackages = "com.test.**")
public class SpringshiroApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringshiroApplication.class, args);
    }

}

相關文章