首页 > 其他分享 >SpringBoot入门实战:SpringBoot整合Shiro

SpringBoot入门实战:SpringBoot整合Shiro

时间:2024-07-27 18:59:20浏览次数:11  
标签:return SpringBoot shiroFilterFactoryBean new public Shiro 入门

1.背景介绍

SpringBoot是一个用于快速开发Spring应用程序的框架。它的核心是对Spring框架的一层封装,使其更加简单易用。SpringBoot整合Shiro是一种将SpringBoot与Shiro整合的方法,以实现身份验证和授权功能。

Shiro是一个强大的Java安全框架,它提供了身份验证、授权、密码存储和会话管理等功能。Shiro可以与Spring框架整合,以实现更强大的安全功能。

在本文中,我们将介绍如何将SpringBoot与Shiro整合,以实现身份验证和授权功能。我们将从背景介绍、核心概念与联系、核心算法原理和具体操作步骤、数学模型公式详细讲解、具体代码实例和详细解释说明等方面进行讲解。

2.核心概念与联系

在整合SpringBoot和Shiro之前,我们需要了解一些核心概念和联系。

2.1 SpringBoot

SpringBoot是一个用于快速开发Spring应用程序的框架。它的核心是对Spring框架的一层封装,使其更加简单易用。SpringBoot提供了许多预先配置好的依赖项,以及一些自动配置功能,使得开发者可以更快地开发应用程序。

2.2 Shiro

Shiro是一个强大的Java安全框架,它提供了身份验证、授权、密码存储和会话管理等功能。Shiro可以与Spring框架整合,以实现更强大的安全功能。

2.3 SpringBoot与Shiro的整合

SpringBoot与Shiro的整合是为了实现身份验证和授权功能的。通过整合Shiro,我们可以在SpringBoot应用程序中实现身份验证和授权功能,以提高应用程序的安全性。

3.核心算法原理和具体操作步骤以及数学模型公式详细讲解

在本节中,我们将详细讲解Shiro的核心算法原理、具体操作步骤以及数学模型公式。

3.1 Shiro的核心算法原理

Shiro的核心算法原理包括:

  • 身份验证:Shiro提供了多种身份验证方式,如密码验证、Token验证等。身份验证的核心原理是通过比较用户输入的密码与数据库中存储的密码来验证用户身份。
  • 授权:Shiro提供了多种授权方式,如基于角色的授权、基于资源的授权等。授权的核心原理是通过检查用户的角色和权限来决定用户是否具有访问某个资源的权限。
  • 密码存储:Shiro提供了多种密码存储方式,如MD5、SHA-1等。密码存储的核心原理是通过对密码进行加密存储,以保护密码的安全性。
  • 会话管理:Shiro提供了会话管理功能,用于管理用户的会话。会话管理的核心原理是通过对会话进行跟踪和管理,以保证用户的会话安全。

3.2 Shiro的具体操作步骤

Shiro的具体操作步骤包括:

  1. 配置Shiro的依赖项:在项目的pom.xml文件中添加Shiro的依赖项。
  2. 配置Shiro的Filter:在项目的Web配置类中添加Shiro的Filter,以实现身份验证和授权功能。
  3. 配置Shiro的Realm:在项目的Shiro配置类中添加Shiro的Realm,以实现数据库的访问。
  4. 配置Shiro的权限和角色:在项目的Shiro配置类中添加Shiro的权限和角色,以实现授权功能。
  5. 配置Shiro的密码存储:在项目的Shiro配置类中添加Shiro的密码存储,以实现密码的存储和加密功能。
  6. 配置Shiro的会话管理:在项目的Shiro配置类中添加Shiro的会话管理,以实现会话的跟踪和管理功能。

3.3 Shiro的数学模型公式

Shiro的数学模型公式包括:

  • 身份验证的数学模型公式:f(x) = \begin{cases} 1, & \text{if } x = \text{password} \\ 0, & \text{otherwise} \end{cases}
  • 授权的数学模型公式:g(x, y) = \begin{cases} 1, & \text{if } x \in y \\ 0, & \text{otherwise} \end{cases}
  • 密码存储的数学模型公式:h(x) = \text{encrypt}(x)
  • 会话管理的数学模型公式:s(x) = \text{session}(x)

4.具体代码实例和详细解释说明

在本节中,我们将通过一个具体的代码实例来详细解释Shiro的使用方法。

4.1 项目结构

项目的结构如下:

- src
  - main
    - java
      - com
        - example
          - shiro
            - ShiroConfig.java
            - ShiroFilter.java
            - ShiroRealm.java
    - resources
      - application.properties

4.2 ShiroConfig.java

ShiroConfig.java是项目的Shiro配置类,用于配置Shiro的Filter、Realm、权限、角色、密码存储和会话管理。

@Configuration
@ComponentScan("com.example.shiro")
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(ShiroRealm shiroRealm) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager());
        shiroFilterFactoryBean.setLoginUrl("/login");
        shiroFilterFactoryBean.setSuccessUrl("/index");
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized");

        Map<String, String> filterChainDefinitionMap = new HashMap<>();
        filterChainDefinitionMap.put("/login", "anon");
        filterChainDefinitionMap.put("/logout", "logout");
        filterChainDefinitionMap.put("/unauthorized", "anon");
        filterChainDefinitionMap.put("/", "authc");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);

        return shiroFilterFactoryBean;
    }

    @Bean
    public ShiroRealm shiroRealm() {
        return new ShiroRealm();
    }

    @Bean
    public DefaultWebSecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(shiroRealm());
        securityManager.setCacheManager(cacheManager());
        return securityManager;
    }

    @Bean
    public CachingRealm cachingRealm() {
        CachingRealm cachingRealm = new CachingRealm();
        cachingRealm.setRealm(shiroRealm());
        return cachingRealm;
    }

    @Bean
    public SimpleAuthorizationInfo authorizationInfo() {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.addRole("admin");
        authorizationInfo.addStringPermission("admin:view");
        return authorizationInfo;
    }

    @Bean
    public SimpleAuthenticationInfo authenticationInfo() {
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo("admin", "admin", "admin");
        return authenticationInfo;
    }

    @Bean
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        cacheManager.setCaches(Collections.singletonMap("shiro-cache", new EhCacheManager("shiro-cache")));
        return cacheManager;
    }
}

4.3 ShiroFilter.java

ShiroFilter.java是项目的Shiro的Filter,用于实现身份验证和授权功能。

@Component
public class ShiroFilter extends OncePerRequestFilter {

    @Autowired
    private ShiroRealm shiroRealm;

    @Override
    protected boolean shouldConvertException(Exception ex, HttpServletRequest request, HttpServletResponse response) {
        return false;
    }

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        try {
            ShiroFilterChain filterChain = new ShiroFilterChain(request, response, filterChain);
            filterChain.doFilter();
        } catch (Exception ex) {
            if (ex instanceof UnknownAccountException) {
                response.sendRedirect("/login");
            } else {
                response.sendRedirect("/unauthorized");
            }
        }
    }
}

4.4 ShiroRealm.java

ShiroRealm.java是项目的Shiro的Realm,用于实现数据库的访问。

public class ShiroRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String) token.getPrincipal();
        User user = userService.findByUsername(username);
        if (user == null) {
            throw new UnknownAccountException("Unknown user: " + username);
        }
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user, user.getPassword(), getName());
        return authenticationInfo;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        User user = (User) principals.getPrimaryPrincipal();
        authorizationInfo.addRole(user.getRole());
        authorizationInfo.addStringPermission(user.getPermission());
        return authorizationInfo;
    }
}

5.未来发展趋势与挑战

在未来,Shiro可能会发展为更强大的安全框架,提供更多的安全功能和更好的性能。同时,Shiro也可能会面临更多的挑战,如如何保护应用程序免受新型攻击的挑战,如如何提高应用程序的安全性的挑战等。

6.附录常见问题与解答

在本节中,我们将列出一些常见问题及其解答。

6.1 问题1:如何配置Shiro的Filter?

答案:在项目的Web配置类中添加Shiro的Filter,如下所示:

@Configuration
@ComponentScan("com.example.shiro")
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(ShiroRealm shiroRealm) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager());
        shiroFilterFactoryBean.setLoginUrl("/login");
        shiroFilterFactoryBean.setSuccessUrl("/index");
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorized");

        Map<String, String> filterChainDefinitionMap = new HashMap<>();
        filterChainDefinitionMap.put("/login", "anon");
        filterChainDefinitionMap.put("/logout", "logout");
        filterChainDefinitionMap.put("/unauthorized", "anon");
        filterChainDefinitionMap.put("/", "authc");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);

        return shiroFilterFactoryBean;
    }
}

6.2 问题2:如何配置Shiro的Realm?

答案:在项目的Shiro配置类中添加Shiro的Realm,如下所示:

@Bean
public ShiroRealm shiroRealm() {
    return new ShiroRealm();
}

6.3 问题3:如何配置Shiro的权限和角色?

答案:在项目的Shiro配置类中添加Shiro的权限和角色,如下所示:

@Bean
public SimpleAuthorizationInfo authorizationInfo() {
    SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
    authorizationInfo.addRole("admin");
    authorizationInfo.addStringPermission("admin:view");
    return authorizationInfo;
}

@Bean
public SimpleAuthenticationInfo authenticationInfo() {
    SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo("admin", "admin", "admin");
    return authenticationInfo;
}

6.4 问题4:如何配置Shiro的密码存储?

答案:在项目的Shiro配置类中添加Shiro的密码存储,如下所示:

@Bean
public CacheManager cacheManager() {
    SimpleCacheManager cacheManager = new SimpleCacheManager();
    cacheManager.setCaches(Collections.singletonMap("shiro-cache", new EhCacheManager("shiro-cache")));
    return cacheManager;
}

6.5 问题5:如何配置Shiro的会话管理?

答案:在项目的Shiro配置类中添加Shiro的会话管理,如下所示:

@Bean
public DefaultWebSecurityManager securityManager() {
    DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
    securityManager.setRealm(shiroRealm());
    securityManager.setCacheManager(cacheManager());
    return securityManager;
}

7.总结

在本文中,我们详细介绍了如何将SpringBoot与Shiro整合,以实现身份验证和授权功能。我们介绍了Shiro的核心概念、联系、算法原理、操作步骤以及数学模型公式。同时,我们通过一个具体的代码实例来详细解释Shiro的使用方法。最后,我们总结了一些常见问题及其解答。希望本文对您有所帮助。

© 著作权归作者所有,转载或内容合作请联系作者

喜欢的朋友记得点赞、收藏、关注哦!!!

标签:return,SpringBoot,shiroFilterFactoryBean,new,public,Shiro,入门
From: https://blog.csdn.net/qq_24428851/article/details/140722840

相关文章

  • 入门C语言Day18——break&continue&goto语句
    前面的博文中有提到do-while与for循环语句,其中的流程图中有break和continue这两个部分还没解释。所以今天先来解释一下break与continue语句。break和continue两个关键字都被运用在循环中。break的作用是永久的终止循环,只要break被执行,直接就会跳出循环,继续往后执行。......
  • Midjourney入门-局部重绘详解与具体操作
    局部重绘是Midjourney一个非常实用的功能。通过局部重绘你可以对Midjourney生成的图片进行二次修改,达到控图改图的效果。接下来我们讲下操作step1-图片生成先生成一组图片,并挑选一张step2-认识三种重绘功能在生成的图片底下有三个按钮:Vary(Subtle)-变化(微妙)通......
  • Midjourney入门-图片高清放大详细操作
    当我们生成一组图片后,并且挑选到了自己喜欢的图片等情况下如果图片像素不够怎么办,Midjourney默认的图片大小只有1024*1024这时候,我们可以选择高清放大图片高清放大当你用选择U1/U2/U3/U4任意一张图片后,在图片底下会有多个按钮选项供你再次选择Upscale(Subtle)-细化放......
  • SpringBoot上传和下载文件
    SpringBoot上传和下载文件创建表DROPTABLEIFEXISTS`file`;CREATETABLE`file`(`id`int(0)NOTNULL,`filename`varchar(255)CHARACTERSETutf8mb4COLLATEutf8mb4_0900_ai_ciNULLDEFAULTNULLCOMMENT'文件名',`path`varchar(255)CHARACTERSE......
  • 基于微信小程序+SpringBoot+Vue的校园自助打印系统(带1w+文档)
    基于微信小程序+SpringBoot+Vue的校园自助打印系统(带1w+文档)基于微信小程序+SpringBoot+Vue的校园自助打印系统(带1w+文档)管理信息可以处理复杂的信息从而提高用户的工作效率,减少失误。所以本基于Vue和微信小程序的校园自助打印系统的开发非常有意义,本系统里的信......
  • Vue入门(二)常用指令
    一、Vue常用指令Vue常用指令-指令:是带有v-前缀的特殊属性,不同指令具有不同含义。例如v-html,v-if,v-for。-使用指令时,通常编写在标签的属性上,值可以使用JS的表达式。-常用指令  二、常用指令介绍2.1文本插值v-html:-v-html:把文本解......
  • 【STC 相关】【转载】51单片机学习教程(简单入门)
    转载自:知乎https://zhuanlan.zhihu.com/p/628407258作者:rakey(作者主页https://www.zhihu.com/people/rakey-49) 学习51单片机之前你一定要具备的基础知识。1、C语言基础。2、数字电路基础。3、模拟电路基础。如果你已经具备这些知识那么我们就可以来学习单片机。学习......
  • Vue入门(一)
    一、Vue介绍-Vue是一套构建用户界面的渐进式前端框架。渐进式框架:简单的来说你可以将Vue作为你的应用一部分嵌入其中,代理更丰富的交互体验。就是用你想用或者能用的功能特性,你不想用的部分功能可以先不用。Vue不强求你一次性接受并使用它的全部功能特性-只关......
  • 易优cms 安装常见问题汇总 Eyoucms快速入门
    安装报错,请仔细核对数据库账号和密码答:请检查填写的数据库链接信息是否正确,此问题都是填写的数据库地址,账号,密码不正确导致的本地测试正常,放到虚拟主机就这样了安装的时候出现这个:虚拟主机HPH5.5 ,数据库MYSQL 5.6PHP Warning:  include_once(./templates/step1.php......
  • marktext入门
    快捷键marktext/docs/KEYBINDINGS_WINDOWS.mdatmaster·marktext/marktext·GitHubFilemenuIdDefaultDescriptionfile.new-fileCtrl+NNewfilefile.new-tabCtrl+TNewtabfile.open-fileCtrl+OOpenmarkdownfilefile.open-folderCtrl+Shift+O......