广告
返回顶部
首页 > 资讯 > 后端开发 > Python >springboot集成shiro权限管理简单实现
  • 890
分享到

springboot集成shiro权限管理简单实现

springboot集成shiro权限管理springboot集成shiro 2022-11-13 14:11:21 890人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录前言依赖配置Filter过滤器配置securityManager配置Realm配置密码加密测试前言 为了解决项目当中的权限管理问题,我们一般会选择引入spring securit

前言

为了解决项目当中的权限管理问题,我们一般会选择引入spring security或者shiro框架来帮助我们更好地更快地构建权限管理体系。

依赖

首先第一步,我们需要给当前项目引入对应的依赖包。与Spring Boot集成一般首选starter包。

<!-- shiro权限管理框架 -->
<dependency>
    <groupId>org.apache.shiro</groupId>
    <artifactId>shiro-spring-boot-WEB-starter</artifactId>
    <version>1.9.1</version>
</dependency>

配置

无论是spring security还是shiro,两者都是基于servlet的Filter过滤器机制实现的权限管理。所以第一步配置我们就需要把对应的Filter给加入进来。

Filter过滤器配置

@Bean
public ShiroFilterFactoryBean shiroFilterFactoryBean() {
    ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
    // 设置securityManager,负责权限验证的核心事务处理。
    shiroFilterFactoryBean.setSecurityManager(securityManager());
    // 配置过滤器链
    Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
    // anon表示该url不需要经过权限验证
    filterChainDefinitionMap.put("/static
public class PassWordHashedCredentialsMatcher extends HashedCredentialsMatcher {
​
    public PasswordHashedCredentialsMatcher(String hashAlGorithmName) {
        super(hashAlgorithmName);
        setHashIterations(2);
    }
​
    public String encryptedPassword(String passwordString, String salt) {
        return hashProvidedCredentials(passwordString, ByteSource.Util.bytes(salt), getHashIterations()).toHex();
  }
}

其实我们就是继承了HashedCredentialsMatcher,在它的基础上提高了直接针对密码加密的功能。这样既能满足shiro的登陆验证,又能拿出来用到创建用户的时候加密使用。

测试

为了验证我们的配置是否成功,我们需要写几个测试接口:

package com.example.awesomespring.controller;
​
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
​
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
​
  @GetMapping("/{id}")
  @RequiresPermissions("User:info")
  String getUserInfo(@PathVariable String id) {
    log.info("获取用户信息开始");
    log.info("请求参数 id:{}", id);
    log.info("获取用户信息结束");
    return "getUserInfo";
  }
​
  @GetMapping("/hello")
  String hello() {
    return "hello world!";
  }
​
  @GetMapping("/read")
  @RequiresPermissions("User:read")
  String read() {
    return "read";
  }
}

上面的几个接口,我们如果直接访问的话,在浏览器中,会被重定向到"/login"页面,因为我们目前没有这个页面,所以一旦重定向这个url,说明用户没有登陆。

其次我们通过调用post请求"/login",提交username和password成功登陆后,页面会重定向到"/index"页面,目前我们也是没有这个页面的,不过从响应体中可以看到响应码是302。

当我们在用户登陆成功后,再访问"/user/read"是能正常访问,并返回结果"read";如果访问"/user/123456"是不行的,页面会直接报500错误。

通过上述测试,我们已经初步完成了shiro的集成

到此这篇关于SpringBoot集成shiro权限管理简单实现的文章就介绍到这了,更多相关springboot 集成shiro内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: springboot集成shiro权限管理简单实现

本文链接: https://www.lsjlt.com/news/171867.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作