首页
关于
Search
1
BT宝塔面板免费使用专业版网站监控报表插件
276 阅读
2
Python批量校验两个文件夹里面的文件MD5
180 阅读
3
MySQL创建索引
110 阅读
4
欢迎使用 Typecho
92 阅读
5
更改宝塔nginx默认的日志格式
92 阅读
默认分类
Java
SpringBoot
MySQL
Linux
登录
/
注册
Search
标签搜索
MySQL
Linux
JAVA
Docker
JavaScript
JDK
Redis
CentOS
SQL
SpringBoot
HTTP
Python
CDN
IP
前端
Micky
累计撰写
57
篇文章
累计收到
1
条评论
今日撰写
0
篇文章
首页
栏目
默认分类
Java
SpringBoot
MySQL
Linux
页面
关于
用户登录
登录
注册
搜索到
8
篇与
Java
的结果
2022-06-28
Java生成随机IP
import java.util.Random; public class getRandomIp { /** * 生成随机IP * <p> * 规则 * 127.xxx.xxx.xxx 属于 “loopback” 地址,即只能你自己的本机可见,就是本机地址,比较常见的有 127.0.0.1 * 192.168.xxx.xxx 属于 private 私有地址 (site local address),属于本地组织内部访问,只能在本地局域网可见 * 同样 10.xxx.xxx.xxx、从 172.16.xxx.xxx 到172.31.xxx.xxx 都是私有地址,也是属于组织内部访问 * 169.254.xxx.xxx 属于连接本地地址(link local IP),在单独网段可用 * 从 224.xxx.xxx.xxx 到 239.xxx.xxx.xxx 属于组播地址 * 比较特殊的 255.255.255.255 属于广播地址 * 除此之外的地址就是点对点的可用的公开 IPv4 地址 * </p> */ public static String getRandomIp() { // 指定 IP 范围 int[][] range = { {607649792, 608174079}, // 36.56.0.0-36.63.255.255 {1038614528, 1039007743}, // 61.232.0.0-61.237.255.255 {1783627776, 1784676351}, // 106.80.0.0-106.95.255.255 {2035023872, 2035154943}, // 121.76.0.0-121.77.255.255 {2078801920, 2079064063}, // 123.232.0.0-123.235.255.255 {-1950089216, -1948778497}, // 139.196.0.0-139.215.255.255 {-1425539072, -1425014785}, // 171.8.0.0-171.15.255.255 {-1236271104, -1235419137}, // 182.80.0.0-182.92.255.255 {-770113536, -768606209}, // 210.25.0.0-210.47.255.255 {-569376768, -564133889}, // 222.16.0.0-222.95.255.255 }; Random random = new Random(); int index = random.nextInt(10); String ip = num2ip(range[index][0] + random.nextInt(range[index][1] - range[index][0])); return ip; } /** * 将十进制转换成IP地址 */ public static String num2ip(int ip) { int[] b = new int[4]; b[0] = (ip >> 24) & 0xff; b[1] = (ip >> 16) & 0xff; b[2] = (ip >> 8) & 0xff; b[3] = ip & 0xff; // 拼接 IP String x = b[0] + "." + b[1] + "." + b[2] + "." + b[3]; return x; } public static void main(String[] args) { int count = 100; for (int i = 0; i < count; i++) { String randomIp = getRandomIp(); System.out.println(randomIp); } } }
2022年06月28日
8 阅读
0 评论
0 点赞
2021-11-05
实用!一键生成数据库文档
数据库文档图一、数据库支持[x] MySQL[x] MariaDB[x] TIDB[x] Oracle[x] SqlServer[x] PostgreSQL[x] Cache DB二、配置1、pom文件引入screw核心包,HikariCP数据库连接池,HikariCP号称性能最出色的数据库连接池。 <!-- screw核心 --> <dependency> <groupId>cn.smallbun.screw</groupId> <artifactId>screw-core</artifactId> <version>1.0.3</version> </dependency> <!-- HikariCP --> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.4.5</version> </dependency> <!--mysql driver--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency>2、配置数据源配置数据源,设置 useInformationSchema 可以获取tables注释信息。spring.datasource.url=jdbc:mysql://45.93.1.5:3306/fire?useUnicode=true&characterEncoding=UTF-8&useSSL=false spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.xa.properties.useInformationSchema=true3、screw 核心配置screw有两种执行方式,第一种是pom文件配置,另一种是代码执行。 <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>cn.smallbun.screw</groupId> <artifactId>screw-maven-plugin</artifactId> <version>1.0.3</version> <dependencies> <!-- HikariCP --> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.4.5</version> </dependency> <!--mysql driver--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency> </dependencies> <configuration> <!--username--> <username>root</username> <!--password--> <password>123456</password> <!--driver--> <driverClassName>com.mysql.cj.jdbc.Driver</driverClassName> <!--jdbc url--> <jdbcUrl>jdbc:mysql://41.92.6.5:3306/fire</jdbcUrl> <!--生成文件类型--> <fileType>HTML</fileType> <!--打开文件输出目录--> <openOutputDir>false</openOutputDir> <!--生成模板--> <produceType>freemarker</produceType> <!--文档名称 为空时:将采用[数据库名称-描述-版本号]作为文档名称--> <!--<docName>测试文档名称</docName>--> <!--描述--> <description>数据库文档生成</description> <!--版本--> <version>${project.version}</version> <!--标题--> <title>fire数据库文档</title> </configuration> <executions> <execution> <phase>compile</phase> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build>配置完以后在 maven project->screw 双击执行ok。在这里插入图片描述代码生成方式也非常简单。@SpringBootTest public class ScrewApplicationTests { @Autowired ApplicationContext applicationContext; @Test void contextLoads() { DataSource dataSourceMysql = applicationContext.getBean(DataSource.class); // 生成文件配置 EngineConfig engineConfig = EngineConfig.builder() // 生成文件路径,自己mac本地的地址,这里需要自己更换下路径 .fileOutputDir("D:/") // 打开目录 .openOutputDir(false) // 文件类型 .fileType(EngineFileType.HTML) // 生成模板实现 .produceType(EngineTemplateType.freemarker).build(); // 生成文档配置(包含以下自定义版本号、描述等配置连接) Configuration config = Configuration.builder() .version("1.0.3") .description("生成文档信息描述") .dataSource(dataSourceMysql) .engineConfig(engineConfig) .produceConfig(getProcessConfig()) .build(); // 执行生成 new DocumentationExecute(config).execute(); } /** * 配置想要生成的表+ 配置想要忽略的表 * * @return 生成表配置 */ public static ProcessConfig getProcessConfig() { // 忽略表名 List<String> ignoreTableName = Arrays.asList("a", "test_group"); // 忽略表前缀,如忽略a开头的数据库表 List<String> ignorePrefix = Arrays.asList("a", "t"); // 忽略表后缀 List<String> ignoreSuffix = Arrays.asList("_test", "czb_"); return ProcessConfig.builder() //根据名称指定表生成 .designatedTableName(Arrays.asList("fire_user")) //根据表前缀生成 .designatedTablePrefix(new ArrayList<>()) //根据表后缀生成 .designatedTableSuffix(new ArrayList<>()) //忽略表名 .ignoreTableName(ignoreTableName) //忽略表前缀 .ignoreTablePrefix(ignorePrefix) //忽略表后缀 .ignoreTableSuffix(ignoreSuffix).build(); } }4、文档格式screw 有 HTML、DOC、MD 三种格式的文档。代码中的修改.fileType(EngineFileType.HTML)或者pom文件<fileType>MD</fileType>DOC文档样式work文档HTML文档样式在这里插入图片描述MD文档样式在这里插入图片描述
2021年11月05日
56 阅读
0 评论
0 点赞
2021-10-21
在Spring Boot中使用Cookies
本文大纲读取HTTP Cookie设置HTTP Cookie读取所有Cookie[]为Cookie设置过期时间Https与CookieHttpOnly Cookie删除CookieHTTP Cookie(也称为Web cookie,浏览器cookie)是服务器在用户浏览器中存储的小部分数据。服务器端应用程序在返回浏览器请求响应的时候设置cookie,浏览器存储cookie,并将它们在下一个请求一起发送的时候自动带回服务器端应用程序。Cookies提供了一种在服务器和浏览器之间交换信息的方法,以管理会话(登录,购物车,游戏得分),记住用户首选项(主题,隐私策略接受)以及跟踪整个站点的用户行为。Cookies在一定程度上解放了服务器端的压力,因为将一部分数据放在浏览器端存储,所以这部分数据不能是涉及应用安全的数据。在本文中,我们将学习如何在Spring Boot应用程序中读取、设置和删除HTTP cookie。二、读取HTTP CookieSpring框架提供@CookieValue注释来获取HTTP cookie的值,此注解可直接用在控制器方法参数中。 @GetMapping("/") public String readCookie(@CookieValue(value = "username", defaultValue = "Atta") String username) { return "Hey! My username is " + username; }在上述代码段中,请注意defaultValue = "Atta"。如果没有设置默认值,并且没有找到名称为username的Cookie,Spring将抛出java.lang.IllegalStateException异常。三、设置HTTP Cookie要在Spring Boot中设置cookie,我们可以使用HttpServletResponse类的方法addCookie()。您需要做的就是创建一个新的Cookie对象并将其添加到响应中。@GetMapping("/change-username") public String setCookie(HttpServletResponse response) { // 创建一个 cookie对象 Cookie cookie = new Cookie("username", "Jovan"); //将cookie对象加入response响应 response.addCookie(cookie); return "Username is changed!"; }四、读取所有Cookie[]除了使用@CookieValue注解,我们还可以使用HttpServletRequest类作为控制器方法参数来读取所有cookie。此类提供了getCookies()方法,该方法以数组形式返回浏览器发送的所有cookie。@GetMapping("/all-cookies") public String readAllCookies(HttpServletRequest request) { Cookie[] cookies = request.getCookies(); if (cookies != null) { return Arrays.stream(cookies) .map(c -> c.getName() + "=" + c.getValue()) .collect(Collectors.joining(", ")); } return "No cookies"; }五、为Cookie设置过期时间如果没有为cookie指定过期时间,则其生命周期将持续到Session过期为止。这样的cookie称为会话cookie。会话cookie保持活动状态,直到用户关闭其浏览器或清除其cookie。但是您可以覆盖此默认行为,并使用类的setMaxAge()方法设置cookie的过期时间。// 创建一个 cookie对象 Cookie cookie = new Cookie("username", "Jovan"); cookie.setMaxAge(7 * 24 * 60 * 60); // 7天过期 //将cookie对象加入response响应 response.addCookie(cookie);现在,usernameCookie不会因为Seesion结束到期,而是会在接下来的7天保持有效。传递给setMaxAge()方法的到期时间以秒为单位。到期日期和时间是相对于设置cookie的客户端而不是服务器而言的。六、Https与Cookie我们需要了解一个概念:什么的安全的Cookies?安全的cookie是仅可以通过加密的HTTPS连接发送到服务器的cookie。无法通过未加密的HTTP连接将cookie发送到服务器。也就是说,如果设置了setSecure(true),该Cookie将无法在Http连接中传输,只能是Https连接中传输。// 创建一个 cookie对象 Cookie cookie = new Cookie("username", "Jovan"); cookie.setSecure(true); //Https 安全cookie //将cookie对象加入response响应 response.addCookie(cookie);七、HttpOnly CookieHttpOnly cookie用于防止跨站点脚本(XSS)攻击,也就是说设置了Http Only的Cookie不能通过JavaScript的Document.cookieAPI访问,仅能在服务端由服务器程序访问。// 创建一个 cookie对象 Cookie cookie = new Cookie("username", "Jovan"); cookie.setHttpOnly(true); //不能被js访问的Cookie //将cookie对象加入response响应 response.addCookie(cookie);八、删除Cookie要删除Cookie,需要将Max-Age设置为0,并且将Cookie的值设置为null。不要将Max-Age指令值设置为-1负数。否则,浏览器会将其视为会话cookie。// 将Cookie的值设置为null Cookie cookie = new Cookie("username", null); //将`Max-Age`设置为0 cookie.setMaxAge(0); response.addCookie(cookie);概括Cookie 提供了一种在服务器和浏览器之间交换信息以管理会话(登录、购物车、游戏分数)、记住用户偏好(主题、隐私政策接受)以及跟踪用户在整个站点上的行为的方法。Spring Boot 提供了一种简单的方法来读取、写入和删除 HTTP cookie。@CookieValue注释将 cookie 的值映射到方法参数。当 cookie 不可用时,您应该设置默认值以避免运行时异常。HttpServletResponse类可用于在浏览器中设置新的 cookie。您只需要创建一个Cookie类的实例并将其添加到响应中。要读取所有 cookie,您可以使用HttpServletRequest'sgetCookies()方法返回一个Cookie.Max-Age 指令指定 cookie 应到期的日期和时间。如果您在 cookie 中存储敏感信息,请确保设置Secure和HttpOnly标记以避免 XSS 攻击。设置Path=/使当前域的 cookie 可在任何地方访问。要删除 cookie,请将 设置Max-Age为0并传递用于设置它的所有属性。
2021年10月21日
20 阅读
0 评论
0 点赞
1
2