springboot知识04

news2024/9/24 1:17:40

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;
    }

}

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C语言基础语法跟练 day4

41、牛牛有一个半径为 r 的球&#xff0c;他想知道这个球的体积。 #include <stdio.h> #include<math.h> int main() {float r;scanf("%f",&r);float v (4.0/3)*3.14*pow(r,3);printf("%.2f",v);return 0; } 42、小乐乐比较懒惰&#x…

Red Hat Enterprise Linux 9.3 安装图解

引导和开始安装 选择倒计时结束前&#xff0c;通过键盘上下键选择下图框选项&#xff0c;启动图形化安装过程。需要注意的不同主板默认或者自行配置的固件类型不一致&#xff0c;引导界面有所不同。也就是说使用UEFI和BIOS的安装引导界面是不同的&#xff0c;如图所示。若手动调…

Stable Code 3B:边缘编程

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

力扣hot100 最长有效括号 动态规划

Problem: 32. 最长有效括号 文章目录 思路Code 思路 &#x1f468;‍&#x1f3eb; 参考题解 Code ⏰ 时间复杂度: O ( n ) O(n) O(n) &#x1f30e; 空间复杂度: O ( n ) O(n) O(n) class Solution {public int longestValidParentheses(String s){int n s.length();…

SwiftUI 框架有哪些主要优势

SwiftUI是苹果公司在2019年推出的一种用于构建用户界面的框架&#xff0c;它使用Swift语言编写&#xff0c;并且与iOS、iPadOS、macOS、watchOS和tvOS等平台兼容。下面简单的看下有哪些主要的优势。 声明式的界面描述 使用声明式编程风格&#xff0c;通过简洁的代码描述用户界…

C#,入门教程(21)——命名空间(namespace)与程序结构的基础知识

上一篇&#xff1a; C#&#xff0c;入门教程(20)——列表&#xff08;List&#xff09;的基础知识https://blog.csdn.net/beijinghorn/article/details/124094382 编写软件&#xff08;大软件称为系统&#xff09;与盖大楼一个道理。 假设咱们现在需要盖一座名为“天梯大厦”的…

移动端 h5-table react版本支持虚拟列表

介绍 适用于 react ts 的 h5 移动端项目 table 组件 github 链接 &#xff1a;https://github.com/duKD/react-h5-table 有帮助的话 给个小星星 有两种表格组件 常规的&#xff1a; 支持 左侧固定 滑动 每行点击回调 支持 指定列排序 支持滚动加载更多 效果和之前写的vue…

2023江苏职教高考计算机技能考试--填空题解析

/*--------------- 【程序填空】 ----------------- 题目&#xff1a;验证任意一个大于1的正整数都可以找到 一串连续奇数&#xff0c;它们的和等于该整数的立方。 例如&#xff1a;4的立方等于64&#xff0c;6413579111315程序运行结果如下&#xff1a;3的立方等于27&#xff…

Vue四个阶段,八个钩子函数

- 创造阶段&#xff1a;创建Vue实例和初始化数据事件&#xff0c;数据代理&#xff0c;监测watch - beforeCreate&#xff0c;只是创建实例&#xff0c;不能this.$el,this.msg,this.方法名&#xff08;&#xff09; - created&#xff0c;数据代理了&#xff0c;能v…

上位机编程:CP56Time2a格式精讲

Cp56Time2a介绍&#xff1a; Cp56Time2a是西门子PLC&#xff08;可编程逻辑控制器&#xff09;中用于时间数据传输的一种特殊格式&#xff0c;主要用于PCS7和基于TCP/IP的S7通信过程中。这种时间格式主要为了确保在不同的系统和设备之间进行精确的时间同步。 Cp56Time2a格式&a…

CodeReview 小工具

大家开发中有没有遇到一个版本开发的非常杂&#xff0c;开发很多个项目&#xff0c;改动几周后甚至已经忘了自己改了些什么&#xff0c;领导要对代码review的时候&#xff0c;理不清楚自己改过的代码&#xff0c;只能将主要改动的大功能过一遍。这样就很容易造成review遗漏&…

力扣日记1.19-【二叉树篇】538. 把二叉搜索树转换为累加树

力扣日记&#xff1a;【二叉树篇】538. 把二叉搜索树转换为累加树 日期&#xff1a;2023.1.19 参考&#xff1a;代码随想录、力扣 ps&#xff1a;因为准备组会汇报又搁置了好久&#xff08;其实就是懒逃避T^T)&#xff0c;但这是最后一道二叉树啦啊啊啊&#xff01;&#xff01…

calloc与realloc和malloc的区别以及new

目录 calloc、realloc 和 malloc 三个函数的区别在于 更详细的示例代码 交叉使用 内存泄漏 悬空指针 内存重叠 new 的语法 使用 new 运算符在堆上创建学生对象的示例 new和malloc都可以用于在堆上分配内存 calloc、realloc 和 malloc 是 C/C 中用于动态内存分配的函…

PrestaShop购物系统 SQL注入漏洞复现(CVE-2023-30150)

0x01 产品简介 PrestaShop 是一个功能丰富,基于 PHP5 开发的 Web2.0 网上购物系统。PrestaShop 具有可定制,稳定等特点。整个系统只有 5.8MB,易于快速安装。 0x02 漏洞概述 PrestaShop 的部分主题中使用Leo Custom Ajax模块拓展,Leo Custom Ajax模块中可以在/modules/le…

ValueError: Unable to read workbook: could not read strings from data.xlsx解决方案

大家好,我是爱编程的喵喵。双985硕士毕业,现担任全栈工程师一职,热衷于将数据思维应用到工作与生活中。从事机器学习以及相关的前后端开发工作。曾在阿里云、科大讯飞、CCF等比赛获得多次Top名次。现为CSDN博客专家、人工智能领域优质创作者。喜欢通过博客创作的方式对所学的…

RNN:Long Short-term Memory(中)

目录 1 LSTM 的简图 2 LSTM 的整体结构 2.1 结构图 2.2 流程图 3 举个例子 3.1 简单看看 3.2 代入 LSTM 4 Original Network v.s. LSTM 5 细看 LSTM 原视频&#xff1a;李宏毅 2020&#xff1a;Recurrent Neural Network (Part I) 1 LSTM 的简图 LSTM 实际…

css3+javaScript实现一个左右钟摆-摇晃的红灯笼网页特效

css3javaScript实现一个左右钟摆-摇晃的红灯笼网页特效&#xff01;前天逛博客时无意中看见了&#xff0c;别人的博客顶部有一个会左右钟摆的摇晃的红灯笼&#xff0c;产生了想法&#xff0c;我也想给自己做一个&#xff0c;但是网上找了很多方案&#xff0c;都没有实现。终于在…

教您如何下载保存钉钉视频到电脑本地

教您如何下载保存钉钉视频到电脑和手机相册地瓜网络技术 大家好&#xff0c;我们这边是地瓜网络技术&#xff01;30秒教会你下载钉钉视频&#xff01;现在很多群管理员把视频设置为禁止下载&#xff0c;导致我们无法正常的下载群直播回放视频&#xff0c; 今天我们就教大家如何…

第十二篇【传奇开心果系列】Ant Design Mobile of React开发移动应用:内置组件实现酷炫CSS 动画

Ant Design Mobile of React 开发移动应用示例博文系列 第一篇【传奇开心果系列】Ant Design Mobile of React 开发移动应用:从helloworld开始 第二篇【传奇开心果系列】Ant Design Mobile of React 开发移动应用:天气应用 第三篇【传奇开心果系列】Ant Design Mobile of Reac…

flutter获取地理定位:geolocator依赖详细用法

本文使用geolocator插件实现app物理定位功能。 该插件的主要功能有&#xff1a; 获取最后已知位置&#xff1b;获取设备当前位置&#xff1b;获取连续的位置更新&#xff1b;检查设备是否启用了定位服务&#xff1b;计算两个地理坐标之间的距离&#xff08;米&#xff09;&am…