springboot知识04

1、集成swagger+shiro放行

(1)导包

(2)SwaggerConfig(公共)

package com.smart.community.common.swagger.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;

/**
 * @author liuhuitang
 */
@Configuration
@EnableOpenApi
public class SwaggerConfig {

    @Resource
    private SwaggerProperties swaggerProperties;

    @Bean
    public Docket docket(ApiInfo apiInfo){
        return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo)
                .enable(swaggerProperties.getEnable())
                .groupName(swaggerProperties.getGroupName())
                .select()
                .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title(swaggerProperties.getTitle())
                .description(swaggerProperties.getDescription())
                .version(swaggerProperties.getVersion())
                .contact(new Contact(swaggerProperties.getName(), swaggerProperties.getUrl(), swaggerProperties.getEmail()))
                .build();
    }
}

(3)SwaggerProperties(公共)

package com.smart.community.common.swagger.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author liuhuitang
 */
@Component
@ConfigurationProperties("swagger")
@Data
public class SwaggerProperties {
    private String title;
    private String basePackage;
    private String groupName = "default-group";
    private String description;

    private String name;
    private String url;
    private String email;
    private Boolean enable = true;

    private String version;

}

(4)yml文件(公共)


spring:
  #  swagger使用
  mvc:
#    配置Spring MVC如何匹配URL路径
    path-match:
#      基于Ant风格的路径模式匹配
      matching-strategy: ant_path_matcher

(5)yml文件(具体)

swagger:
  base-package: "com.smart.community.services.controller"
  title: "智慧社区在线Api文档"
  group-name: "物业服务模块"
  description: "出现bug,请熟读该文档"
  name: "智慧社区"
  url: "https://www.baidu.com"
  email: "11111@163.com"
  version: "V1.0.0"

(6)设置放行(具体yml里面)

shiro:
  loginUrl: "/test/no/login"
  white:
    list: "/auth/**,/doc.html,/swagger**/**,/webjars/**,/v3/**,/druid/**"

(7)ShiroConfig 重新配置

        1)单值方式

package com.smart.community.services.config;


import com.smart.community.services.realm.UserRealm;
import com.smart.community.services.session.CustomSessionManager;
import com.smart.community.services.utils.ShiroUtils;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author liuhuitang
 */
@Configuration
public class ShiroConfig {

    public static final String SHIRO_ANON="anon";
    public static final String SHIRO_AUTHC = "authc";

    @Value("${shiro.white.list}")
    private List<String> whiteList;

    /**
     * 注册realm
     * @return
     */
    @Bean
    public UserRealm realm(HashedCredentialsMatcher hashedCredentialsMatcher){
        UserRealm userRealm = new UserRealm();
        userRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        return userRealm;


    }

    /**
     * 注册realmmanager
     * @param realm
     * @return
     */
    @Bean
    public DefaultWebSecurityManager securityManager(UserRealm realm,CustomSessionManager customSessionManager){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        //注册自定义Realm
        defaultWebSecurityManager.setRealm(realm);
        //注册session
        defaultWebSecurityManager.setSessionManager(customSessionManager);
        return defaultWebSecurityManager;
    }

    /**
     * 注册过滤器
     * @return
     */
    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition(){
        DefaultShiroFilterChainDefinition definition = new DefaultShiroFilterChainDefinition();

        //白名单(支持通配符)
        //放行
        whiteList.forEach(path->definition.addPathDefinition(path,SHIRO_ANON));
        //表示其他所有接口需要认证
        definition.addPathDefinition("/**",SHIRO_AUTHC);

        return definition;

    }


    /**
     * 注册密码加密器
     * @return
     */
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        hashedCredentialsMatcher.setHashAlgorithmName(Md5Hash.ALGORITHM_NAME);
        hashedCredentialsMatcher.setHashIterations(ShiroUtils.HASH_ITERATIONS);
//        hashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);
        return hashedCredentialsMatcher;
    }


    /**
     * cookie+session
     * jwt
     */
    @Bean
    public CustomSessionManager sessionManager(){
        CustomSessionManager customSessionManager = new CustomSessionManager();

//        session的过期时间 -1永不过期 0用一次 >0根据时间(单位毫秒)
        //todo 作用?
        customSessionManager.setGlobalSessionTimeout(7*24*60*60*1000);
        customSessionManager.setSessionIdCookieEnabled(true);
        customSessionManager.setSessionIdUrlRewritingEnabled(false);
        return customSessionManager;
    }



}

        2)多值方式(多) 

                1)配置信息(上面的单也可以)
shiro:
  loginUrl: "/test/no/login"
  white:
    list:
      - "/auth/**"
      - "/doc.html"
      - "/swagger**/**"
      - "/webjars/**"
      - "/v3/**"
      - "/druid/**"
                2)新建ShiroProperties属性类
package com.smart.community.services.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.List;

@ConfigurationProperties("shiro.white")
@Data
public class ShiroProperties {

    private List<String> list;

}
                3)ShiroProperties
package com.smart.community.services.config;


import com.smart.community.services.realm.UserRealm;
import com.smart.community.services.session.CustomSessionManager;
import com.smart.community.services.utils.ShiroUtils;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author liuhuitang
 */
@Configuration
@EnableConfigurationProperties(ShiroProperties.class)
public class ShiroConfig {

    public static final String SHIRO_ANON="anon";
    public static final String SHIRO_AUTHC = "authc";

    @Value("${shiro.white.list}")
//    private List<String> whiteList;

    @Resource
    private ShiroProperties shiroProperties;



    /**
     * 注册realm
     * @return
     */
    @Bean
    public UserRealm realm(HashedCredentialsMatcher hashedCredentialsMatcher){
        UserRealm userRealm = new UserRealm();
        userRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        return userRealm;


    }

    /**
     * 注册realmmanager
     * @param realm
     * @return
     */
    @Bean
    public DefaultWebSecurityManager securityManager(UserRealm realm,CustomSessionManager customSessionManager){
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        //注册自定义Realm
        defaultWebSecurityManager.setRealm(realm);
        //注册session
        defaultWebSecurityManager.setSessionManager(customSessionManager);
        return defaultWebSecurityManager;
    }

    /**
     * 注册过滤器
     * @return
     */
    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition(){
        DefaultShiroFilterChainDefinition definition = new DefaultShiroFilterChainDefinition();

        //白名单(支持通配符)
        //放行
//        whiteList.forEach(path->definition.addPathDefinition(path,SHIRO_ANON));
        shiroProperties.getList().forEach(path->definition.addPathDefinition(path,SHIRO_ANON));
        //表示其他所有接口需要认证
        definition.addPathDefinition("/**",SHIRO_AUTHC);

        return definition;

    }


    /**
     * 注册密码加密器
     * @return
     */
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        hashedCredentialsMatcher.setHashAlgorithmName(Md5Hash.ALGORITHM_NAME);
        hashedCredentialsMatcher.setHashIterations(ShiroUtils.HASH_ITERATIONS);
//        hashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);
        return hashedCredentialsMatcher;
    }


    /**
     * cookie+session
     * jwt
     */
    @Bean
    public CustomSessionManager sessionManager(){
        CustomSessionManager customSessionManager = new CustomSessionManager();

//        session的过期时间 -1永不过期 0用一次 >0根据时间(单位毫秒)
        //todo 作用?
        customSessionManager.setGlobalSessionTimeout(7*24*60*60*1000);
        customSessionManager.setSessionIdCookieEnabled(true);
        customSessionManager.setSessionIdUrlRewritingEnabled(false);
        return customSessionManager;
    }



}

2、mybatisPlus的配置分页转换

package com.smart.community.common.db.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * @author liuhuitang
 */
public class CommonBeanUtils extends BeanUtils {

    /**
     * 将A对象的属性赋值给B对象
     * @param source
     * @param targetSupplier
     * @return
     * @param <S>
     * @param <T>
     */
    public static <S,T> T convertTo(S source, Supplier<T> targetSupplier){
        //        这里的ObjectUtils.isEmpty(targetSupplier)是为了方式下面的get出现空指针异常
        if (ObjectUtils.isEmpty(source) || ObjectUtils.isEmpty(targetSupplier)){
            return null;
        }
        T t = targetSupplier.get();
        copyProperties(source,t);
        return t;
    }

    /**
     * 调用convertTo(s, targetSupplier)方法来生成一个新的T类型对象。
     * 然后使用collect(Collectors.toList())将所有这些新生成的T类型对象收集到一个新的列表中
     * @param sources
     * @param targetSupplier
     * @return
     * @param <S>
     * @param <T>
     */
    public static <S,T> List<T> convertListTo(List<S> sources, Supplier<T> targetSupplier){
        if (ObjectUtils.isEmpty(sources) || ObjectUtils.isEmpty(targetSupplier)){
            return null;
        }

        //把每个对象赋值属性然后用collect转为集合
        return sources.stream().map(s -> convertTo(s,targetSupplier)).collect(Collectors.toList());
    }

    /**
     * mybatisplus 配置分页转换
     * @param source
     * @param target
     * @param targetSupplier
     * @return
     * @param <S>
     * @param <T>
     */
    public static <S,T> IPage<T> convertPageInfo(IPage<S> source, IPage<T> target, Supplier<T> targetSupplier){
        //属性复制
        copyProperties(source,target);
        target.setRecords(convertListTo(source.getRecords(),targetSupplier));
        return target;
    }

}

3、stream流

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/336139.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

手把手教你薅熊链Berachain测试网空投

Berachain&#xff0c;这名字响当当&#xff01;是基于流动性证明的高性能区块链&#xff0c;结合了Tendermint和流动性共识证明&#xff0c;还采用了Celestia作为DA层。这速度快、成本低、确定性高&#xff0c;简直就是未来的大热门&#xff01;你知道吗&#xff1f;这家公司可…

竞赛保研 机器学习股票大数据量化分析与预测系统 - python 竞赛保研

文章目录 0 前言1 课题背景2 实现效果UI界面设计web预测界面RSRS选股界面 3 软件架构4 工具介绍Flask框架MySQL数据库LSTM 5 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; 机器学习股票大数据量化分析与预测系统 该项目较为新颖&am…

【设计模式】腾讯二面:自动贩卖机/音频播放器使用了什么设计模式?

状态模式是什么&#xff1f; 状态模式&#xff0c;也被称作状态对象模式&#xff0c;是一种行为设计模式。 当一个对象的内在状态改变时&#xff0c;允许改变其行为&#xff0c;这个对象看起来像是改变了其类。 它让对象在其内部状态改变时改变自己的行为。外部调用者无需了…

学习笔记应用——创建用户账户并且拥有自己的信息

一、创建用户账户 将建立一个用户注册和身份验证系统&#xff0c;让用户能够注册账户&#xff0c;进而登录和注销。我们将创建一个新的应用程序&#xff0c;其中包含与处理用户账户相关的所有功能。 创建user 我们首先使用命令 startapp 来创建一个名为 users 的应用程序&…

爬虫之Cookie获取:利用浏览器模拟一个cookie出来、面对反爬虫、加密的cookie的应对方法

爬虫之Cookie获取&#xff1a;利用浏览器模拟一个cookie出来、面对反爬虫、加密的cookie的应对方法 在爬虫或模拟请求时&#xff0c;特别是获取验证码的时候&#xff0c;反爬虫的网站的cookie或定期失效&#xff0c;复制出来使用是不行的为了应对这种方式&#xff0c;我们可能…

(Bean实例化的基本流程 )学习Spring的第六天

Bean实例化的基本流程 其实可以解释为三个过程: 1 . 有关Bean的信息被封装成一个map集合 : DeanDefinitionMap . key为Bean的名称 , value为有关<bena>标签的信息 2 . Spring框架对这个存储Bean信息的Map进行遍历 , 进行创建对象 , 把创建好的对象存储到另一个Map集合里…

【华为 ICT HCIA eNSP 习题汇总】——题目集4

1、&#xff08;多选&#xff09;网络中出现故障后&#xff0c;管理员通过排查发现某台路由器的配置被修改了&#xff0c;那么管理员应该采取哪些措施来避免这种状况再次发生&#xff1f; A、管理员应该通过配置 ACL 来扩展只有管理员能够登录设备 B、管理员应该在路由的管理端…

【项目管理】CMMI-原因分析与解决过程(CAR)

概述&#xff1a; “原因分析与解决”通过预防缺陷或者问题的引入以及识别并适当纳入优秀过程性能的原因&#xff0c;改进质量与生产率。 目录 1、文档结构 2、原因分析与解决过程域包括如下活动 3、选择需要加以分析的结果(启动条件) 4、过程活动与实践对照表 5、实例 1、…

前端打同一个包可以从测试晋升到生产的配置方案

前端打同一个包从测试晋升到生产环境的方案&#xff0c;是一种高效、可靠且易于维护的部署方式。在这种方案中&#xff0c;前端代码在开发完成后&#xff0c;经过测试验证无误后&#xff0c;可以直接打包部署到生产环境&#xff0c;无需进行额外的配置或修改。这样可以减少部署…

虹科分享 | 汽车技术的未来:Netropy如何测试和确保汽车以太网的性能

文章速览&#xff1a; 什么是汽车以太网&#xff1f;汽车以太网的用途是什么&#xff1f;汽车以太网的测试要求是什么&#xff1f;流量生成如何帮助测试汽车以太网&#xff1f; 如今汽车不再是单纯的代步工具&#xff0c;把人从A点带到B点&#xff0c;同时还配备了车载信息娱乐…

pytest文档内置fixture的request详情

前言 request 是 pytest 的内置 fixture &#xff0c; "为请求对象提供对请求测试上下文的访问权&#xff0c;并且在fixture被间接参数化的情况下具有可选的“param”属性。"这是官方文档对request的描述&#xff0c;可参考的文档不多。 一、FixtureRequest Fixtur…

免费使用IntelliJ IDEA的7种方式(2024 最新版)

大家好&#xff0c;我是小黑&#xff0c;今天要和大家分享的是如何免费使用 IntelliJ IDEA。我们都知道&#xff0c;作为一名程序员&#xff0c;拥有一个高效的开发工具是至关重要的。IntelliJ IDEA 无疑是市面上最受欢迎的开发工具之一。但是&#xff0c;获取授权的成本有时会…

跟着pink老师前端入门教程-day07

去掉li前面的项目符号&#xff08;小圆点&#xff09; 语法&#xff1a;list-style: none; 十五、圆角边框 在CSS3中&#xff0c;新增了圆角边框样式&#xff0c;这样盒子就可以变成圆角 border-radius属性用于设置元素的外边框圆角 语法&#xff1a;border-radius:length…

【复现】Apache Solr信息泄漏漏洞_24

目录 一.概述 二 .漏洞影响 三.漏洞复现 1. 漏洞一&#xff1a; 四.修复建议&#xff1a; 五. 搜索语法&#xff1a; 六.免责声明 一.概述 Apache Solr是一个独立的企业级搜索应用服务器&#xff0c;它对外提供类似于Web-service的API接口。用户可以通过http请求&#x…

【ARMv8M Cortex-M33 系列 7.1 -- xPSR | CFSR | HFSR | BFAR | MMFAR 寄存器】

请阅读【嵌入式开发学习必备专栏 之 ARM Cortex-Mx专栏】 文章目录 问题背景Cortex-M33 Fault 寄存器介绍xPSR (程序状态寄存器)CFSR (可配置故障状态寄存器)HFSR (硬件故障状态寄存器)BFAR (总线故障地址寄存器)MMFAR (内存管理故障地址寄存器) 问题背景 由于在RA4M2&#xf…

muduo 网络库源码解析和使用

1. base 模块 1.1 API 1.1.1 eventfd int eventfd(unsigned int initval, int flags);&#xff08;1&#xff09;类似信号量&#xff1b;其内部保存了一个 uint64_t 计数器 count&#xff0c;使用 initval 初始化&#xff1b; &#xff08;2&#xff09;read 没有设置 EFD…

ACM:每日学习 状压dp

状压dp&#xff1a; 状压dp是对一般dp的改进&#xff1a; //对于判断多种物品的取法&#xff0c;开多维数组比较麻烦&#xff0c;也不好开&#xff0c;使用二进制来表示物品的取与否。 //使用二进制的话&#xff0c;位运算就更能省时间了&#xff0c;而且更会节省空空间&…

02-编程猜谜游戏

本章通过演示如何在实际程序中使用 Rust&#xff0c;你将了解 let 、 match 、方法、关联函数、外部crate等基础知识。 本章将实现一个经典的初学者编程问题&#xff1a;猜谜游戏。 工作原理如下&#xff1a;程序将随机生成一个介于 1 和 100 之间的整数。然后&#xff0c;程序…

【算法实验】实验六

实验6-1 硬币找钱问题—贪心 问题描述&#xff1a; 设有6 种不同面值的硬币&#xff0c;各硬币的面值分别为5 分&#xff0c;1 角&#xff0c;2 角&#xff0c;5 角&#xff0c;1 元&#xff0c;2 元。现要用这些面值的硬币来购物和找钱。购物时可以使用的各种面值的硬币个数存…

CHS_01.2.2.1+调度的概念、层次

CHS_01.2.2.1调度的概念、层次 调度的概念、层次知识总览调度的基本概念调度的三个层次——高级调度![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/6957fdec179841f69a0508914145da36.png)调度的三个层次——低级调度调度的三个层次——中级调度补充知识&#xff…