Shiro整合Springboot缓存之EhCache实现

Shiro整合Springboot缓存之EhCache实现

Cache作用

1.Cache缓存:计算机内存中一段数据
2.作用:用来减轻DB的访问压力,从而提高系统的查询效率
3.流程:
在这里插入图片描述

引入shiro和ehcache的整合依赖

<!--引入shiro和ehcahce依赖-->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-ehcache</artifactId>
    <version>1.5.3</version>
</dependency>

开启缓存

//3.创建自定义Realm
@Bean
public Realm getRealm(){
    CustomerRealm customerRealm = new CustomerRealm();
    //修改凭证校验匹配器
    HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
    //设置加密算法为md5
    credentialsMatcher.setHashAlgorithmName("MD5");
    //设置散列次数
    credentialsMatcher.setHashIterations(1024);
    customerRealm.setCredentialsMatcher(credentialsMatcher);

    //开启缓存管理
    customerRealm.setCacheManager(new EhCacheManager());
    customerRealm.setCachingEnabled(true);  //开启全局缓存
    customerRealm.setAuthenticationCachingEnabled(true);  //开启认证缓存
    customerRealm.setAuthenticationCacheName("authenticationCache");
    customerRealm.setAuthorizationCachingEnabled(true);  //开启授权缓存
    customerRealm.setAuthorizationCacheName("authorizationCache");

    return customerRealm;
}

其它的代码实现可以参考:Springboot整合Shiro

shiro整合Spring Boot可以实现菜单管理功能,下面给出一个简单的示例: 首先,在Spring Boot中引入shiro和数据库相关的依赖,例如使用MySQL作为数据库,可以引入以下依赖: ``` <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring-boot-web-starter</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> ``` 其次,创建一个菜单实体类,例如Menu: ```java @Entity @Table(name = "menu") public class Menu { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String url; // 其他属性省略 // getter和setter方法省略 } ``` 然后,创建一个菜单的控制器类,例如MenuController,用来处理菜单的增删改查等操作: ```java @RestController @RequestMapping("/menu") public class MenuController { @Autowired private MenuService menuService; @GetMapping public List<Menu> getMenuList() { return menuService.findAllMenus(); } @PostMapping public Menu createMenu(@RequestBody Menu menu) { return menuService.saveMenu(menu); } @DeleteMapping("/{id}") public void deleteMenu(@PathVariable("id") Long id) { menuService.deleteMenu(id); } // 其他方法省略 } ``` 最后,在应用的配置文件中添加shiro的相关配置,例如application.properties: ``` # Shiro配置 shiro: enabled: true login-url: /login success-url: /home unauthorized-url: /403 filter-chain-definitions: /logout: logout /static/**: anon /login: anon /403: anon /**: authc ``` 以上是一个简单的示例,实际应用中还需要根据具体需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值