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 {@Resourceprivate SwaggerProperties swaggerProperties;@Beanpublic 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();}@Beanpublic 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*/@Beanpublic UserRealm realm(HashedCredentialsMatcher hashedCredentialsMatcher){UserRealm userRealm = new UserRealm();userRealm.setCredentialsMatcher(hashedCredentialsMatcher);return userRealm;}/*** 注册realmmanager* @param realm* @return*/@Beanpublic DefaultWebSecurityManager securityManager(UserRealm realm,CustomSessionManager customSessionManager){DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();//注册自定义RealmdefaultWebSecurityManager.setRealm(realm);//注册sessiondefaultWebSecurityManager.setSessionManager(customSessionManager);return defaultWebSecurityManager;}/*** 注册过滤器* @return*/@Beanpublic ShiroFilterChainDefinition shiroFilterChainDefinition(){DefaultShiroFilterChainDefinition definition = new DefaultShiroFilterChainDefinition();//白名单(支持通配符)//放行whiteList.forEach(path->definition.addPathDefinition(path,SHIRO_ANON));//表示其他所有接口需要认证definition.addPathDefinition("/**",SHIRO_AUTHC);return definition;}/*** 注册密码加密器* @return*/@Beanpublic HashedCredentialsMatcher hashedCredentialsMatcher(){HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();hashedCredentialsMatcher.setHashAlgorithmName(Md5Hash.ALGORITHM_NAME);hashedCredentialsMatcher.setHashIterations(ShiroUtils.HASH_ITERATIONS);
//        hashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);return hashedCredentialsMatcher;}/*** cookie+session* jwt*/@Beanpublic 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;@Resourceprivate ShiroProperties shiroProperties;/*** 注册realm* @return*/@Beanpublic UserRealm realm(HashedCredentialsMatcher hashedCredentialsMatcher){UserRealm userRealm = new UserRealm();userRealm.setCredentialsMatcher(hashedCredentialsMatcher);return userRealm;}/*** 注册realmmanager* @param realm* @return*/@Beanpublic DefaultWebSecurityManager securityManager(UserRealm realm,CustomSessionManager customSessionManager){DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();//注册自定义RealmdefaultWebSecurityManager.setRealm(realm);//注册sessiondefaultWebSecurityManager.setSessionManager(customSessionManager);return defaultWebSecurityManager;}/*** 注册过滤器* @return*/@Beanpublic 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*/@Beanpublic HashedCredentialsMatcher hashedCredentialsMatcher(){HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();hashedCredentialsMatcher.setHashAlgorithmName(Md5Hash.ALGORITHM_NAME);hashedCredentialsMatcher.setHashIterations(ShiroUtils.HASH_ITERATIONS);
//        hashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);return hashedCredentialsMatcher;}/*** cookie+session* jwt*/@Beanpublic 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.mzph.cn/news/635292.shtml

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

相关文章

WordPress判断页面函数is_page()怎么使用?

平时我们在折腾 WordPress 的时候&#xff0c;经常需要用到 is_page()函数来判断是否属于页面&#xff0c;是否属于指定页面等。那么对于这个判断页面函数 is_page()应该怎么使用呢&#xff1f; is_page()函数介绍 is_page( int|string|array $page ) 其中$page&#xff0…

开发实践8_REST

一、Django REST Framework, Django View & APIView MTV模式实现前后端分离。Representational State Transfer 表现层状态转化。Representation 资源&#xff08;Resource a specific info. on net.&#xff09;具体呈现形式。ST 修改服务端的数据。修改数据 POST请求。…

java使用AES加密数据库解密

目录 前言代码加密&#xff08;AES&#xff09;sql解密 前言 在一些项目中&#xff0c;客户要求一方面把一些敏感信息进行加密存储到数据库中&#xff0c;另一方面又需要通过加密的信息进行查询&#xff0c;这时就需要在sql对加密的字段进行解密后再进行查询。 代码加密&#x…

力扣labuladong——一刷day95

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、力扣225. 用队列实现栈二、力扣232. 用栈实现队列 前言 两种数据结构底层其实都是数组或者链表实现的&#xff0c;只是 API 限定了它们的特性&#xff0c;那…

数据结构与算法教程,数据结构C语言版教程!(第五部分、数组和广义表详解)二

第五部分、数组和广义表详解 数组和广义表&#xff0c;都用于存储逻辑关系为“一对一”的数据。 数组存储结构&#xff0c;99% 的编程语言都包含的存储结构&#xff0c;用于存储不可再分的单一数据&#xff1b;而广义表不同&#xff0c;它还可以存储子广义表。 本章重点从矩阵…

复现github项目的基本步骤

1. 克隆 GitHub 项目 找到项目仓库&#xff1a;在 GitHub 上找到你想复现的项目。 复制克隆链接&#xff1a;点击仓库页面的“Clone or download”按钮&#xff0c;复制提供的 URL。 克隆仓库&#xff1a;打开终端或命令提示符&#xff0c;使用以下命令克隆仓库&#xff1a; …

对多种股权激励方式进行分析,明确按照业绩贡献确定激励对象

一、背景 某生物创新材料有限公司创立于1990年&#xff0c;坐落于成都某高新技术产业开发区&#xff0c;是一家以研发、生产和销售医疗器械、医用高分子材料、生物技术等生物、能源方面的产品为主的大型企业&#xff0c;该公司与美国某科技研究所结成合作伙伴&#xff0c;研发出…

[python语言]数据类型

目录 知识结构​编辑 复数类型 整数类型、浮点数类型 1、整型 2、浮点型 字符与字符串 1、转义字符 2、字符串的截取 3、字符串的拼接级连 4、字符串的格式化 1、format格式化 2、字符格式化 3、f标志位格式化--(推荐) 5、字符串的常用属性 1、对字符串做出判断…

电脑上怎么进行pdf合并?这几招分分钟解决

电脑上怎么进行pdf合并&#xff1f;在现代办公中&#xff0c;PDF文件已经成为了我们处理文档的常用格式之一。有时候&#xff0c;我们需要将多个PDF文件合并成一个文件&#xff0c;以方便阅读或打印。那么&#xff0c;如何在电脑上进行PDF合并呢&#xff1f;下面就给大家介绍几…

yolov5 检测封装

yolov5 pytorch推理 检测封装 v7测试成功 import csv import os import platform import sys from pathlib import Pathimport numpy as np import torchFILE = Path(__file__).resolve() ROOT = FILE.parents[0] # YOLOv5 root directory if str(ROOT) not in sys.path:sys.p…

知识图谱的演进

目录 前言1 Memex&#xff1a;信息存储的雏形2 超文本和Web&#xff1a;链接的崛起3 Semantic Web&#xff1a;从文本链接到数据链接4 Linked Big Data&#xff1a;规范化的语义表示5 谷歌的知识图谱搜索引擎6 多种语义网/知识图谱项目结语 前言 随着人工智能和互联网的飞速发…

Keil下载芯片包(DFP)时找不到根目录的解决办法

目录 1 发现的问题 2 想到的可能解决问题的措施 1 发现的问题 打开Keil时Pack Installer 自动打开下载芯片包&#xff0c;但弹出如下提示&#xff0c;无法下载&#xff1a; Refresh Pack description E: the specified CMsls Pack Root directorydoes NoT exist! Please tak…

go语言(一)----声明变量

package mainimport ("fmt""time" )func main() {fmt.Print("hello go!")time.Sleep(1 * time.Second)}运行后&#xff0c;结果如下&#xff1a; 1、golang表达式中&#xff0c;加&#xff1b;和不加&#xff1b;都可以 2、函数的{和函数名一…

Docker部署微服务问题及解决

&#x1f468;‍&#x1f393;作者简介&#xff1a;一位大四、研0学生&#xff0c;正在努力准备大四暑假的实习 &#x1f30c;上期文章&#xff1a;Docker容器命令案例&#xff1a;Nginx容器修改&#xff0c;Redis容器持久化 &#x1f4da;订阅专栏&#xff1a;Docker 希望文章…

开发的年终总结怎么写?

引用两句互联网废话&#xff1a; “解决关键问题要找到问题的关键” “先承认问题才能解决问题” 目标一致 “目标一致”&#xff1a;大方向一致。 公司目标 "快速履约"实施目标 "着眼点在事"产品是否具有要求的功能;提出的需求、bug, 快速响应;关注点…

眼镜用超声波清洗机洗会有损坏吗?超声波清洗机有必要买吗

相信很多朋友都十分清楚超声波清洗机&#xff0c;虽然知道但是迟迟不敢下手入一款属于自己超声波清洗机&#xff01;会担心超声波清洗机会不会把自己的眼镜给清洗坏了呢&#xff1f;什么样的超声波清洗机比较适合我呢&#xff1f;买一台超声波清洗机回来真的有必要吗&#xff1…

小程序系列--9.生命周期

1. 什么是生命周期&#xff1f; 2. 生命周期的分类 3. 什么是生命周期函数 4. 生命周期函数的分类 5. 应用的生命周期函数 6. 页面的生命周期函数

ASP.NET Core 对象池化技术

写在前面 Microsoft.Extensions.ObjectPool 是 ASP.NET Core 基础结构的一部分&#xff0c;当对象的初始化成本较高&#xff0c;并且可能被频繁使用时&#xff0c;才适合采用对象池技术&#xff1b;被ObjectPool管理的对象不会进入垃圾回收&#xff0c;使用时通过由实例对象实…

记录Qt和opencv 新环境配置过程

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、Qt是什么&#xff1f;二、Qt的版本三、安装步骤1.下载Qt2.双击安装包.exe开始安装3. 需要登陆才能继续安装&#xff0c;没有的就用邮箱注册账号4.注意安装路…

Message queue 消息队列--RabbitMQ 【基础入门】

&#x1f389;&#x1f389;欢迎来到我的CSDN主页&#xff01;&#x1f389;&#x1f389; &#x1f3c5;我是平顶山大师&#xff0c;一个在CSDN分享笔记的博主。&#x1f4da;&#x1f4da; &#x1f31f;推荐给大家我的博客专栏《Message queue 消息队列--RabbitMQ 【基础入门…