SpringBoot、MyBatis、Shiro、Thymeleaf整合思路
簡單的闡述一下基本的整合思路,方便後面需要用的時候查詢。
- 建立工程
- 匯入座標、依賴
<!-- 匯入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>
- 配置
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
- 建立pojo、dao、mapper、service、serviceimpl、controller
-
pojo類的註解有
@Data :配置常見的get、set
@AllArgsConstructor :帶引數建構函式
@NoArgsConstructor: 無引數建構函式 -
dao介面注意**@Mapper**註解的使用
-
mapper檔案的namespace名稱空間只要指向dao介面
-
serviceimpl類必須配置**@Service註解,有事務的新增@Transactional**
- 配置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
- 啟動類
別忘了@ComponentScan的註解
@SpringBootApplication
@ComponentScan(basePackages = "com.test.**")
public class SpringshiroApplication {
public static void main(String[] args) {
SpringApplication.run(SpringshiroApplication.class, args);
}
}
相關文章
- shiro 整合MybatisMyBatis
- springBoot整合thymeleafSpring Boot
- [thymeleaf]springboot整合thymeleaf, html使用預置方法Spring BootHTML
- 【Shiro】4.Springboot整合ShiroSpring Boot
- SpringBoot 實戰 (十二) | 整合 thymeleafSpring Boot
- springboot+shiro整合Spring Boot
- SpringBoot--- Shiro(攔截,認證)、Thymeleaf(模板引擎)Spring Boot
- SpringBoot極簡整合ShiroSpring Boot
- springboot 整合 Shiro 配置類Spring Boot
- 教你 Shiro + SpringBoot 整合 JWTSpring BootJWT
- springBoot 整合 mybatisSpring BootMyBatis
- Springboot整合MybatisSpring BootMyBatis
- SpringBoot-shiro登入攔截.使用者認證.整合MybatisSpring BootMyBatis
- Shiro和Spring MVC、Mybatis整合教程SpringMVCMyBatis
- SpringBoot系列(六)整合thymeleaf詳解版Spring Boot
- SpringBoot整合Jsp和Thymeleaf (附工程)Spring BootJS
- SpringBoot之整合thymeleaf渲染Web頁面Spring BootWeb
- 教你Shiro+SpringBoot整合JWTSpring BootJWT
- springboot+mybatis整合Spring BootMyBatis
- SpringBoot | 3.2 整合MyBatisSpring BootMyBatis
- Springboot+mybatis 整合Spring BootMyBatis
- SpringBoot整合系列–整合MyBatis-plusSpring BootMyBatis
- 基於SpringBoot+Mybatis+Thymeleaf商品資訊管理系統Spring BootMyBatis
- springBoot 整合 mybatis+OracleSpring BootMyBatisOracle
- SpringBoot整合Mybatis-PlusSpring BootMyBatis
- springboot專案整合mybatisSpring BootMyBatis
- springboot-整合mybatis,securitySpring BootMyBatis
- springboot mybatis 專案框架原始碼 shiro 整合程式碼生成器 ehcache快取Spring BootMyBatis框架原始碼快取
- springboot mybatis 後臺框架平臺 整合程式碼生成器 shiro 許可權Spring BootMyBatis框架
- springboot整合shiro實現身份認證Spring Boot
- springboot+shiro 整合與基本應用Spring Boot
- SpringBoot與Shiro整合-許可權管理Spring Boot
- springboot_2--thymeleaf、異常、log4j、mybatis、redisSpring BootMyBatisRedis
- SpringBoot學習之整合MybatisSpring BootMyBatis
- SpringBoot 實戰 (九) | 整合 MybatisSpring BootMyBatis
- SpringBoot 2.1.1.RELEASE 整合MyBatisSpring BootMyBatis
- springboot整合mybatis及其反向工程Spring BootMyBatis
- SpringBoot | 3.3 整合MyBatis-PlusSpring BootMyBatis