Shiro学习笔记_03:整合SpringBoot项目实战
发布日期:2021-04-30 21:03:22
浏览次数:129
分类:精选文章
本文共 5818 字,大约阅读时间需要 19 分钟。
Shiro ????
??
- ????SpringBoot????
- 6.1 ????
- 6.2 ????
- 6.3 ????
- 6.4 ?????
- 6.5 ???????
- [6.7 MD5?Salt ?????](#67-MD5?Salt ?????)
- 6.8 ????
- [6.9 ?? CacheManager](#69-?? CacheManager)
- Cache ??
- [Shiro ??? EhCache ??](#Shiro-??? EhCache ??)
- Redis ????
- ??? Redis ????
- 6.10 ?????
???? SpringBoot ????
6.1 ????
?? Shiro ??? SpringBoot ?????????????????????????????? ShiroFilterFactoryBean ? DefaultWebSecurityManager?????? realm ?????????????????????
6.2 ????
1. ????
?? SpringBoot ???????????? Web ?????? Shiro ??????
2. ????
- JSP ?????
org.apache.tomcat.embed tomcat-embed-jasper jstl jstl 1.2 - Shiro ?? SpringBoot ???
org.apache.shiro shiro-spring-boot-starter 1.5.3
3. ????
- ?
application.properties????server.port=8080server.servlet.context-path=/shirospringapplication.name=shirospring
mvc.view.prefix=/springmvc.view.suffix=.jsp
#### 4. ????JSP ? SpringBoot ??????????? IDE ?????---### 6.3 ????#### 1. ?????```javapackage com.lut.config;import com.lut.shiro.realms.CustomerRealm;import org.apache.shiro.authc.credential.HashedCredentialsMatcher;import org.apache.shiro.realm.Realm;import org.apache.shiro.spring.web.ShiroFilterFactoryBean;import org.apache.shiro.web.mgt.DefaultWebSecurityManager;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class ShiroConfig { @Bean public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager) { ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager); Map map = new HashMap<>(); map.put("/index.jsp", "authc"); shiroFilterFactoryBean.setFilterChainDefinitionMap(map); shiroFilterFactoryBean.setLoginUrl("/login.jsp"); return shiroFilterFactoryBean; } @Bean public DefaultWebSecurityManager getDefaultWebSecurityManager(Realm realm) { DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager(); defaultWebSecurityManager.setRealm(realm); return defaultWebSecurityManager; } @Bean public Realm getRealm() { CustomerRealm customerRealm = new CustomerRealm(); HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(); matcher.setHashAlgorithmName("md5"); matcher.setHashIterations(1024); customerRealm.setCredentialsMatcher(matcher); return customerRealm; }} 2. ??? realm
package com.lut.shiro.realms;import org.apache.shiro.authc.AuthenticationException;import org.apache.shiro.authc.AuthenticationInfo;import org.apache.shiro.authc.AuthenticationToken;import org.apache.shiro.authc.SimpleAuthenticationInfo;import org.apache.shiro.authz.AuthorizationInfo;import org.apache.shiro.authz.SimpleAuthorizationInfo;import org.apache.shiro.realm.AuthorizingRealm;import org.apache.shiro.subject.PrincipalCollection;import org.springframework.util.CollectionUtils;import org.springframework.util.ObjectUtils;public class CustomerRealm extends AuthorizingRealm { @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { return null; } @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { String principal = (String) token.getPrincipal(); UserService userService = (UserService) ApplicationContextUtils.getBean("userService"); User user = userService.findByUserName(principal); if (!ObjectUtils.isEmpty(user)) { return new SimpleAuthenticationInfo(user.getUsername(), user.getPassword(), new MyByteSource(user.getSalt()), this.getName()); } return null; }} 3. JSP ??
<%@ page contentType="text/html;utf-8" isELIgnored="false" %>???? ????
4. ????
?? http://localhost:8080/shiro/index.jsp?????????????
6.4 ?????
anon ???
???????? URL?
authc ???
???????????????????
Logout ???
?????????
?????
?? perms?roles?ssl?user ??
6.5 ???????
????
@RequestMapping("login")public String login(String username, String password) { try { Subject subject = SecurityUtils.getSubject(); subject.login(new UsernamePasswordToken(username, password)); return "redirect:/index.jsp"; } catch (UnknownAccountException e) { e.printStackTrace(); System.out.println("?????!"); } catch (IncorrectCredentialsException e) { e.printStackTrace(); System.out.println("????!"); } catch (Exception e) { e.printStackTrace(); System.out.println(e.getMessage()); } return "redirect:/login.jsp";} ????
@RequestMapping("logout")public String logout() { Subject subject = SecurityUtils.getSubject(); subject.logout(); return "redirect:/login.jsp";} 6.7 MD5?Salt ?????
??????????
- ???? salt
- MD5 ????
- ?? salt ???????
?????
- ??????
- ????
Shiro ??
- ???????
- ????
6.8 ????
?????
- ???????????????????????
- ?????????
???????
<shiro:hasanyroles name="admin, user">
??????????
<shiro:haspermission name="user:create:01">
6.9 ?? CacheManager
Cache ??
- ?????????
Shiro ??? EhCache ??
- ?? EhCacheManager
Redis ????
- ?? RedisTemplate
- ????
6.10 ?????
??????
- ?????
- ???????
???????
- ????
- ??????
Shiro ??
- ???????
- ????
??????? B?????????????????
发表评论
最新留言
很好
[***.229.124.182]2026年06月17日 22时48分56秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
php增删改查封装方法
2023-03-01
php多条件筛选功能的实现
2023-03-01
php多线程
2023-03-01
PHP大数组循环-避免产生Notice或者是Warning
2023-03-01
PHP大数组过滤元素、修改元素性能分析
2023-03-01
PHP大文件切片下载代码
2023-03-01
php如何做表格,新手怎么制作表格
2023-03-01
RabbitMQ集群 - 普通集群搭建、宕机情况
2023-03-01
php如何正确的获得文件的后缀名
2023-03-01
PHP如何生成唯一的数字ID
2023-03-01
PHP如何获取当前页面的最后修改时间
2023-03-01
PHP如何读取json数据
2023-03-01
PHP字符串
2023-03-01
PHP字符串递增
2023-03-01
php学习之基础语法
2023-03-01
RabbitMQ集群 - 仲裁队列、Raft协议(最详细的选举流程)
2023-03-01
PHP学习总结(11)——PHP入门篇之WAMPServer多站点配置
2023-03-01
PHP学习总结(12)——PHP入门篇之变量
2023-03-01
PHP学习总结(13)——PHP入门篇之常量
2023-03-01