首页 > 其他分享 >SpringBoot 03 Shiro

SpringBoot 03 Shiro

时间:2023-10-17 17:16:03浏览次数:32  
标签:defaultSecurityManager 03 SpringBoot apache shiro put org filterChainDefinitionM

shiro是apache的一个开源框架,是一个权限管理的框架,实现认证、授权、加密、会话管理。

shiro优势举例

  1. 易用:相当于其他安全框架,shiro比较简单易用。
  2. 使用非常广泛,资料好找。
  3. 灵活:可以工作在很多环境 。
  4. web支持:对web的支持好, 如thymeleaf标签支持。
  5. 支持:应用广泛,是Apache软件基金会成员(有维护更新保证)
一.导包
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring-boot-starter</artifactId>
            <version>1.8.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.theborakompanioni</groupId>
            <artifactId>thymeleaf-extras-shiro</artifactId>
        </dependency>

二、realm

package com.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.springframework.stereotype.Component;
//realm连数据库
//认证
@Component("myRealm")
public class MyRealm extends AuthenticatingRealm {
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        return null;
    }
}

三、shiro相关配置


/* shiro相关配置*/
    @Bean //匹配器
    public SimpleCredentialsMatcher simpleCredentialsMatcher(){//简单的匹配规则,没有加密配置
        SimpleCredentialsMatcher simpleCredentialsMatcher =new SimpleCredentialsMatcher();
        return simpleCredentialsMatcher;
    }
    //会话管理器
    @Bean
    public DefaultWebSessionManager defaultWebSessionManager(){
        DefaultWebSessionManager defaultWebSessionManager = new DefaultWebSessionManager();
        return defaultWebSessionManager;
    }
    //安全管理器  最重要
    @Bean
    public DefaultSecurityManager defaultSecurityManager(DefaultWebSessionManager defaultWebSessionManager,SimpleCredentialsMatcher simpleCredentialsMatcher,MyRealm myRealm){
        DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
        myRealm.setCredentialsMatcher(simpleCredentialsMatcher);
        defaultSecurityManager.setRealm(myRealm);
        defaultSecurityManager.setSessionManager(defaultWebSessionManager);
        return defaultSecurityManager;
    }
    //过滤法则
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultSecurityManager defaultSecurityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(defaultSecurityManager);
        Map<String,String> filterChainDefinitionMap = new LinkedHashMap<>();//有顺序  不用无序的HashMap
        //放行
        //静态资源放行
        filterChainDefinitionMap.put("/favicon.ico","anno");
        filterChainDefinitionMap.put("/css/**","anno");
        filterChainDefinitionMap.put("/js/**","anno");
        filterChainDefinitionMap.put("/img/**","anno");
        //动态资源放行
        filterChainDefinitionMap.put("/","anon");
        filterChainDefinitionMap.put("/a","anon");

        //不放行
        filterChainDefinitionMap.put("/**","authc");
        //未认证跳往地址
        shiroFilterFactoryBean.setLoginUrl("/");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;

    }

标签:defaultSecurityManager,03,SpringBoot,apache,shiro,put,org,filterChainDefinitionM
From: https://www.cnblogs.com/oyww-2027/p/17770142.html

相关文章

  • SpringBoot 02 shiro框架查询用户权限与角色
      实体类 @Data@AllArgsConstructor@NoArgsConstructor@TableName("t_user")publicclassRUser{@TableId(value="id",type=IdType.AUTO)privateIntegerusrId;privateStringusrName;privateStringusrAccount;pri......
  • CSS-03
    去掉li前面的项目符号:list-style:none圆角边框border-radius:12px;可以是px或百分比若将正方形改为圆形,则:border-radius:50%;或者将其设为宽度的一半。盒子阴影box-shadow:8px10px10px10pxrgba(0,0,0,.3);水平阴影垂直阴影模糊距离阴影的尺寸阴影颜色透明色:rg......
  • SpringBoot应用jar包启动原理详解
    1、maven打包SpringBoot项目的pom.xml文件中默认使用spring-boot-maven-plugin插件进行打包:<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId>&l......
  • springboot启动流程源码解析(带流程图)
    大致流程如下:初始化SpringApplication,从META-INF下的spring.factories读取ApplicationListener/ApplicationContextInitializer运行SpringApplication的run方法读取项目中环境变量、jvm配置信息、配置文件信息等创建Spring容器对象(ApplicationContext)利用ApplicationCon......
  • [906] Replace NaN (Not-a-Number) values with 'Null' in Pandas
    InPandas,youcanreplaceNaN(Not-a-Number)valuesinaDataFramewithNone(Python'sNonetype)ornp.nan(NumPy'sNaN)values.Here'showyoucanreplaceNaNvalueswithNone:importpandasaspdimportnumpyasnp#CreateasampleDa......
  • app逆向day03-反编译工具和hook框架
    一反编译工具1.1常见反编译工具常见的反编译工具:jadx(推荐)、jeb、GDA反编译工具依赖于java环境,所以我们按照jdk1.2JDK环境安装#官方地址:(需要注册-最新java21)https://www.oracle.com/java/technologies/downloads/1.2.1win平台安装#1下载jdk-8u371-windows-x64.e......
  • springboot heapdump信息获取
    springboot信息泄露可能泄漏的路由/api-docs/v2/api-docs/swagger-ui.html/api.html/sw/swagger-ui.html/api/swagger-ui.html/template/swagger-ui.html/spring-security-rest/api/swagger-ui.html/spring-security-oauth-resource/swagger-ui.html/mappings/actua......
  • 为网站接入免费SSL证书let's encrypt 并实现自动续签功能
    以ubuntu系统为例:1.安装certbot:aptupdateaptupgradesudoaptinstallcertbotpython3-certbot-nginx2.验证Web服务器端口是否打开并允许通过防火墙sudoufwstatusverbose3.获取SSL证书sudocertbot--nginx-dyour_domain.com-dwww.your_domain.com4.检查C......
  • 关于crontab运行脚本时报错KeyError: 'PATH'
    最近在服务器上为let'sencrypt证书添加自动续签计划任务时,发现总是不成功,但手动执行该计划任务所对应的sh脚本则没问题,这让我怀疑crontab执行时可能缺少了点什么导致的,想追踪一下crontab的执行日志,发现并没有,需要手动修改配置文件打开:sudovim/etc/rsyslog.d/50-default.conf......
  • Math teacher's homework 题解
    preface网上的题解看不懂,看代码看懂了:)solution考虑\(\mathrm{x_i}\)的倒数第\(\mathrm{low_i-1}\)位到倒数第\(\mathrm{1}\)位可以乱选(选\(\mathrm{0/1}\)都满足\(\mathrm{x_i\leqm_i}\)),那么就需要\(\mathrm{x_i}\)和\(\mathrm{m_i}\)的第\(\mathrm{1}\)位......