• Spring Boot 整合 Shiro(后端)


    1 Shiro
    什么是 Shiro
    官网: http://shiro.apache.org/
    是一款主流的 Java 安全框架,不依赖任何容器,可以运行在 Java SE
    Java EE 项目中,它的主要作用是对访问系统的用户进行身份认证、
    授权、会话管理、加密等操作。
    Shiro 就是用来解决安全管理的系统化框架。
    2 Shiro 核心组件
    用户、角色、权限 会给角色赋予权限,给用户赋予角色
    1 UsernamePasswordToken Shiro 用来封装用户登录信息,使用 用户的登录信息来创建令牌 Token
    2 SecurityManager Shiro 的核心部分,负责安全认证和授权。
    3 Suject Shiro 的一个抽象概念,包含了用户信息。
    4 Realm ,开发者自定义的模块,根据项目的需求,验证和授权的逻 辑全部写在 Realm 中。
    5 AuthenticationInfo ,用户的角色信息集合,认证时使用。
    6 AuthorzationInfo ,角色的权限信息集合,授权时使用。
    7 DefaultWebSecurityManager ,安全管理器,开发者自定义的 Realm 需要注入到 DefaultWebSecurityManager 进行管理才能生 效。
    8 ShiroFilterFactoryBean ,过滤器工厂, Shiro 的基本运行机制是开 发者定制规则,Shiro 去执行,具体的执行操作就是由 ShiroFilterFactoryBean 创建的一个个 Filter 对象来完成。
    Shiro 的运行机制如下图所示。
    3 Spring Boot 整合 Shiro
    1 、创建 Spring Boot 应用,集成 Shiro 及相关组件, pom.xml
    1. "1.0" encoding="UTF-8"?>
    2. "http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. 4.0.0
    6. com.example
    7. springboot-shiro
    8. 0.1.0
    9. 11
    10. 2.5.4
    11. 1.7.1
    12. org.springframework.boot
    13. spring-boot-starter-web
    14. ${spring.boot.version}
    15. org.apache.shiro
    16. shiro-spring-boot-starter
    17. ${shiro.version}
    18. org.apache.maven.plugins
    19. maven-compiler-plugin
    20. 3.8.1
    21. ${java.version}
    22. ${java.version}
    23. org.springframework.boot
    24. spring-boot-maven-plugin
    25. ${spring.boot.version}

    2 、自定义 Shiro 过滤器
    1. import sun.net.www.protocol.http.AuthenticationInfo;
    2. public class AccoutRealm extends AuthorizingRealm {
    3. @Autowired
    4. private AccountService accountService;
    5. /**
    6. * 授权
    7. *
    8. * @param principalCollection
    9. * @return
    10. */
    11. @Override
    12. protected AuthorizationInfo
    13. doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    14. return null;
    15. }
    16. /**
    17. * 认证
    18. *
    19. * @param authenticationToken
    20. * @return
    21. * @throws AuthenticationException
    22. */
    23. @Override
    24. protected AuthenticationInfo
    25. doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
    26. UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
    27. Account account = accountService.findByUsername(token.getUsername());
    28. if (account != null) {
    29. return new SimpleAuthenticationInfo(account, account.getPassword(), getName());
    30. }
    31. return null;
    32. }
    33. }
    3、配置类 
    1. @Configuration
    2. public class ShiroConfig {
    3. @Bean
    4. public ShiroFilterFactoryBean
    5. shiroFilterFactoryBean(@Qualifier("securityManager")
    6. DefaultWebSecurityManager securityManager){
    7. ShiroFilterFactoryBean factoryBean = new
    8. ShiroFilterFactoryBean();
    9. factoryBean.setSecurityManager(securityManager);
    10. return factoryBean;
    11. }
    12. @Bean
    13. public DefaultWebSecurityManager
    14. securityManager(@Qualifier("accoutRealm") AccoutRealm
    15. accoutRealm){
    16. DefaultWebSecurityManager manager = new
    17. DefaultWebSecurityManager();
    18. manager.setRealm(accoutRealm);
    19. return manager;
    20. }
    21. @Bean
    22. public AccoutRealm accoutRealm(){
    23. return new AccoutRealm();
    24. }
    25. }
    编写认证和授权规则:
    认证过滤器 anon :无需认证。
    authc :必须认证。
    authcBasic :需要通过 HTTPBasic 认证。
    user :不一定通过认证,只要曾经被 Shiro 记录即可,比如:记住我。
    授权过滤器
    perms :必须拥有某个权限才能访问。
    role :必须拥有某个角色才能访问。
    port :请求的端口必须是指定值才可以。
    rest :请求必须基于 RESTful POST PUT GET DELETE
    ssl :必须是安全的 URL 请求,协议 HTTPS
    创建 3 个页面, main.html manage.html administrator.html
    访问权限如下:
    1 、必须登录才能访问 main.html
    2 、当前用户必须拥有 manage 授权才能访问 manage.html
    3 、当前用户必须拥有 administrator 角色才能访问
    administrator.html
    Shiro 整合 Thymeleaf
    1 pom.xml 引入依赖
    com.github.theborakompanioni
    thymeleaf-extras-shiro
    2.0.0
    2 、配置类添加 ShiroDialect
    @Bean
    public ShiroDialect shiroDialect (){
    return new ShiroDialect ();
    }
    3 index.html
    lang = "en" xmlns:th = "http://www.thymeleaf.org"
    xmlns:shiro = "http://www.thymeleaf.org/thymeleaf-extras
    shiro" >
    charset = "UTF-8" >
    </span> <span style="color:#333333;">Title</span> <span style="color:#117700;">
    rel = "shortcut icon" href = "#" />

    index

    th:if = "${session.account != null}" >
    th:text = "${session.account.username}+'
    迎回来! '" > href = "/logout" > 退出
    shiro:hasPermission = "manage" >
    shiro:hasRole = "administrator" >
    数据库
  • 相关阅读:
    querystring模块、formidable模块的介绍
    【数据库】使用SQLAlchemy建立模型之间的基础关系模式
    计算机学院2022级新生周赛(一)题解
    react native引用原生组件时无法显示的问题处理
    动力电池UL2580测试项目包括哪些
    你知道哪几种Java锁?分别有什么特点?
    【CSS】设置文本样式
    Gif格式图片怎么制作?超简单的方法分享
    1105 链表合并 – PAT乙级真题
    23种设计模式-抽象工厂模式介绍加实战代码
  • 原文地址:https://blog.csdn.net/qq_57747969/article/details/132617546