上篇文章介绍了 Spring Boot 初级教程:Spring Boot(一):入门篇,方便大家快速入门、了解实践 Spring Boot 特性;本篇文章接着上篇内容继续为大家介绍 Spring Boot 的其它特性(有些未必是 Spring Boot 体系桟的功能,但是是 Spring 特别推荐的一些开源技术本文也会介绍),对了这里只是一个大概的介绍,特别详细的使用我们会在其它的文章中来展开说明。 Web 开发 Spring Boot Web 开发非常的简单,其中包括常用的 json 输出、filters、property、log 等 json 接口开发 在以前使用 Spring 开发项目,需要提供 json 接口时需要做哪些配置呢 添加 jackjson 等相关 jar 包 配置 Spring Controller 扫描 对接的方法添加 @ResponseBody 就这样我们会经常由于配置错误,导致406错误等等,Spring Boot 如何做呢,只需要类添加 @RestController 即可,默认类中的方法都会以 json 的格式返回
|
1 2 3 4 5 6 7 8 9 10 |
@RestController public class HelloController { @RequestMapping("/getUser") public User getUser() { User user=new User(); user.setUserName("小明"); user.setPassWord("xxxx"); return user; } } |
如果需要使用页面开发只要使用@Controller注解即可,下面会结合模板来说明 自定义 Filter 我们常常在项目中会使用 filters 用于记录调用日志、排除有 XSS 威胁的字符、执行权限验证等等。Spring Boot 自动添加了 OrderedCharacterEncodingFilter 和 HiddenHttpMethodFilter,并且我们可以自定义 Filter。 两个步骤: 实现 Filter 接口,实现 Filter 方法 添加@Configuration 注解,将自定义Filter加入过滤链 好吧,直接上代码
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
@Configuration public class WebConfiguration { @Bean public RemoteIpFilter remoteIpFilter() { return new RemoteIpFilter(); } @Bean public FilterRegistrationBean testFilterRegistration() { FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(new MyFilter()); registration.addUrlPatterns("/*"); registration.addInitParameter("paramName", "paramValue"); registration.setName("MyFilter"); registration.setOrder(1); return registration; } public class MyFilter implements Filter { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain filterChain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest request = (HttpServletRequest) srequest; System.out.println("this is MyFilter,url :"+request.getRequestURI()); filterChain.doFilter(srequest, sresponse); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } } } |
自定义 Property 在 Web 开发的过程中,我经常需要自定义一些配置文件,如何使用呢 配置在 application.properties 中
|
1 2 |
com.neo.title=纯洁的微笑 com.neo.description=分享生活和技术 |
自定义配置类
|
1 2 3 4 5 6 7 8 9 10 |
@Component public class NeoProperties { @Value("${com.neo.title}") private String title; @Value("${com.neo.description}") private String description; //省略getter settet方法 } |
log配置 […]
View Details推荐博客 纯洁的微笑 程序猿DD liaokailin的专栏 Spring Boot 揭秘与实战 系列 catoop的专栏 简书Spring Boot专题 方志朋Spring Boot 专栏 Spring-boot集成 推荐网站 Spring boot 官网 Spring Boot参考指南-中文版 Gradle 中文参考指南 慕课网视频 spring-boot-tutorials 开源书籍-微服务:从设计到部署 开源代码 spring boot官方例子 spring-boot-examples SpringBoot-Learning favorites-web springboot-learning-example spring-boot-all from:http://www.ityouknow.com/springboot/2015/12/30/springboot-collect.html
View Details1:在pom.xml 中 配置
|
1 2 3 4 5 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> |
注意:并不是pom.xml 的<build>标签中 2: 如果有页面的话 禁止页面缓存
|
1 2 3 4 5 6 7 8 9 |
spring: application: name: XXX aop: proxy-target-class: true thymeleaf: cache: false prefix: classpath:/templates/ mode: LEGACYHTML5 |
3:开idea工具的自动编译功能 完成以上步骤 即可实现SpringBoot项目自动编译+热部署,这我们修改后的代码能够立刻看到效果,提高效率! from:https://blog.csdn.net/woshiyeguiren/article/details/79312245 转者PS:其他构建工具可以去http://mvnrepository.com/搜索~
View Details什么是 Spring Boot Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。用我的话来理解,就是 Spring Boot 其实不是什么新的框架,它默认配置了很多框架的使用方式,就像 Maven 整合了所有的 Jar 包,Spring Boot 整合了所有的框架。 使用 Spring Boot 有什么好处 其实就是简单、快速、方便!平时如果我们需要搭建一个 Spring Web 项目的时候需要怎么做呢? 1)配置 web.xml,加载 Spring 和 Spring mvc 2)配置数据库连接、配置 Spring 事务 3)配置加载配置文件的读取,开启注解 4)配置日志文件 … 配置完成之后部署 Tomcat 调试 … 现在非常流行微服务,如果我这个项目仅仅只是需要发送一个邮件,如果我的项目仅仅是生产一个积分;我都需要这样折腾一遍! 但是如果使用 Spring Boot 呢? 很简单,我仅仅只需要非常少的几个配置就可以迅速方便的搭建起来一套 Web 项目或者是构建一个微服务! 使用 Spring Boot 到底有多爽,用下面这幅图来表达 快速入门 说了那么多,手痒痒的很,马上来一发试试! Maven 构建项目 1、访问 http://start.spring.io/ 2、选择构建工具 Maven Project、Java、Spring Boot 版本 2.1.3 以及一些工程基本信息,可参考下图所示: 3、点击 Generate Project 下载项目压缩包 4、解压后,使用 Idea 导入项目,File -> New -> Model from Existing Source.. -> 选择解压后的文件夹 -> OK,选择 Maven 一路 […]
View Details本期为大家精选了 码云 上优秀的 Spring Boot 语言开源项目,涵盖了企业级系统框架、文件文档系统、秒杀系统、微服务化系统、后台管理系统等,希望能够给大家带来一点帮助:) 1、项目名称:分布式敏捷开发系统架构 项目简介:基于 Spring + SpringMVC + Mybatis 分布式敏捷开发系统架构,提供整套公共微服务服务模块:集中权限管理(单点登录)、内容管理、支付中心、用户管理(支持第三方登录)、微信平台、存储系统、配置中心、日志分析、任务和通知等,支持服务治理、监控和追踪,努力为中小型企业打造全方位J2EE企业级开发解决方案。 项目地址:https://gitee.com/shuzheng/zheng 2、项目名称:模块化开发系统 项目简介:以 SpringBoot 为中心,模块化开发系统,用户可以随意删减除权限框架外 任意的系统模块。复用,组装性强主要应用技术: spring Security Ehcache quartz swagger2 Mysql5.6 springjdbc druid spring social spring session layerui+vue.js 项目地址:https://gitee.com/YYDeament/88ybg 3、项目名称:JAVA 分布式快速开发平台 项目简介:JAVA 分布式快速开发平台:SpringBoot,SpringMVC,Mybatis,mybatis-plus,motan/dubbo分布式,Redis 缓存,Shiro 权限管理,Spring-Session 单点登录,Quartz 分布式集群调度,Restful 服务,QQ/微信登录,App token 登录,微信/支付宝支付;日期转换、数据类型转换、序列化、汉字转拼音、身份证号码验证、数字转人民币、发送短信、发送邮件、加密解密、图片处理、excel 导入导出、FTP/SFTP/fastDFS 上传下载、二维码、XML 读写、高精度计算、系统配置工具类等。 项目地址:https://gitee.com/iBase4J/iBase4J 4、项目名称:快速开发框架 ThinkGem 项目简介:Java EE(J2EE)快速开发框架,基于经典技术组合(Spring MVC、Apache Shiro、MyBatis、Bootstrap UI),包括核心模块如:组织机构、角色用户、权限授权、数据权限、内容管理、工作流等。虽说很长时间没有大的更新了,但它的架构精良易于扩展深受大家喜爱,依然是中小企业的首选,它的功能设计、底层架构也非常具有参考意义、是学习入门的首选。关注我ThinkGem开源中国博客了解4.0最新动态。 项目地址:https://gitee.com/thinkgem/jeesite 5、项目名称:Java 快速开发平台 MCMS 项目简介:完整开源,Java 快速开发平台。基于 Spring、SpringMVC、Mybatis 架构,MStore 提供更多好用的插件与模板(文章、商城、微信、论坛、会员、评论、支付、积分、工作流、任务调度等,同时提供上百套免费模板任意选择),价值源自分享!铭飞系统不仅一套简单好用的开源系统、更是一整套优质的开源生态内容体系。 项目地址:https://gitee.com/mingSoft/MCMS 6、项目名称:分布式秒杀系统 项目简介:可能秒杀架构原理大家都懂,网上也有不少实现方式,但大多都是文字的描述,告诉你如何如何,什么加锁、缓存、队列之类。但很少全面有的案例告诉你如何去做,既然是从0到1,希望以下代码案例可以帮助到你。当然最终落实到生产,还有很长的路要走,要根据自己的业务进行编码,实施并部署。你将会在代码案例中学到以下知识: 如何搭建 SpringBoot 微服务 ThreadPoolExecutor […]
View Details英国安全公司 Secarma 的研究主管 Sam Thomas 本月在 Black Hat 和 BSides 安全会议上展示了 PHP 编程语言的安全漏洞,并指出该漏洞影响了所有接受用户资料的 PHP 应用程序和库,包括 WordPress 等内容管理系统(CMS),并将允许远程程序攻击。 序列化(Serialization)与反序列化(Deserialization)是所有编程语言都具备的功能,序列化将对象转换为字符串,以将数据迁移到不同服务器,服务或应用程序上,然后通过反序列将字符串还原到对象。 安全研究员 Stefan Essar 在 2009 年就透露了 PHP 中反序列化黑客控制的数据带来的风险,而相关的漏洞不仅存在于 PHP 中,还存在于其他编程语言中。 Thomas 公布的是 PHP 的新攻击技术,可用于各种场景,例如 XML External Entity(XEE)漏洞或服务器端伪造请求(SSFR)漏洞等。 Thomas 表示,过去外界认为 XXE 漏洞带来的最大问题是信息外泄,但现在可出发程序执行。相关攻击分为两个阶段。 首先,将包含恶意对象的 Phar 存档上传到攻击目标的本地文件系统,然后触发一个基于 phar:// 的文件操作,就可能导致恶意程序执行。 Thomas 已利用 PHP 的反序列化程序成功攻击了 WordPress 与 Typo3 内容管理平台,以及 Contao 所采用的 TCPDF 库。 from:https://www.oschina.net/news/99165/php-unserialization-vulnerability
View Details运行DOS命令: C:\Users\thinkpad>javaws D:\***.jnlp 如提示“应用程序已被java安全阻止”则进入控制面板-》Java 打开java控制面板,在安全选项卡中设置例外站点即可。 from:https://www.cnblogs.com/101key/p/6478395.html
View Details背景, 今天在写代码时要用到GZipStream来压缩需要Web传输的数据块。原本以为GZipStream Write ->Flush ->读取对应MemoryStream数据就Okay的事情,却总是得不到正确的结果。 研究, 经过查询MSDN,原来只有在GZipStream被Dispose后,对应的MemoryStream中才会有真正的压缩数据被写入。 以下是我用来测试的代码片段(红色部分为原来的错误调用,橙色部分是正确的调用方式) string data = "<Root><PIGContent>test</PIGContent><RemoteUrl>http://www.a.com</RemoteUrl></Root>"; byte[] buffer = System.Text.UTF8Encoding.UTF8.GetBytes(data); byte[] compressedbuffer = null; //Compress buffer MemoryStream ms = new MemoryStream(); using(GZipStream zs = new GZipStream(ms, CompressionMode.Compress,true)) { zs.Write(buffer, 0, buffer.Length); //下面两句被注释掉的代码有问题, 对应的compressedbuffer的长度只有10--该10字节应该只是压缩buffer的header //zs.Flush(); //compressedbuffer = ms.ToArray(); } //只有GZipStream在Dispose后调应对应MemoryStream.ToArray()所得到的Buffer才是我们需要的结果 compressedbuffer = ms.ToArray(); 总结, 相信大家都会对GZipstream这种别扭的操作方式表示不满,微软对此也表示过歉意,但是由于其考虑到要兼容就的代码,因此即使在.Net 4.5中你还是得忍受这种不和谐的代码。 本篇小结如有不妥之处,烦请指正。 from: https://blog.csdn.net/missautumn/article/details/8351296
View Details|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Management; using System.Runtime.InteropServices; using System.Text; namespace ConsoleApp1 { /// /// 系统信息类 - 获取CPU、内存、磁盘、进程信息 /// public class SystemInfo { private int m_ProcessorCount = 0; //CPU个数 private PerformanceCounter pcCpuLoad; //CPU计数器 private long m_PhysicalMemory = 0; //物理内存 private const int GW_HWNDFIRST = 0; private const int GW_HWNDNEXT = 2; private const int GWL_STYLE = (-16); private const int WS_VISIBLE = 268435456; private const int WS_BORDER = 8388608; #region AIP声明 [DllImport("IpHlpApi.dll")] extern static public uint GetIfTable(byte[] pIfTable, ref uint pdwSize, bool bOrder); [DllImport("User32")] private extern static int GetWindow(int hWnd, int wCmd); [DllImport("User32")] private extern static int GetWindowLongA(int hWnd, int wIndx); [DllImport("user32.dll")] private static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize); [DllImport("user32", CharSet = CharSet.Auto)] private extern static int GetWindowTextLength(IntPtr hWnd); #endregion #region 构造函数 /// /// 构造函数,初始化计数器等 /// public SystemInfo() { //初始化CPU计数器 pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total"); pcCpuLoad.MachineName = "."; pcCpuLoad.NextValue(); //CPU个数 m_ProcessorCount = Environment.ProcessorCount; //获得物理内存 ManagementClass mc = new ManagementClass("Win32_ComputerSystem"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if (mo["TotalPhysicalMemory"] != null) { m_PhysicalMemory = long.Parse(mo["TotalPhysicalMemory"].ToString()); } } } #endregion #region CPU个数 /// /// 获取CPU个数 /// public int ProcessorCount { get { return m_ProcessorCount; } } #endregion #region CPU占用率 /// /// 获取CPU占用率 /// public float CpuLoad { get { return pcCpuLoad.NextValue(); } } #endregion #region 可用内存 /// /// 获取可用内存 /// public long MemoryAvailable { get { long availablebytes = 0; //ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_PerfRawData_PerfOS_Memory"); //foreach (ManagementObject mo in mos.Get()) //{ // availablebytes = long.Parse(mo["Availablebytes"].ToString()); //} ManagementClass mos = new ManagementClass("Win32_OperatingSystem"); foreach (ManagementObject mo in mos.GetInstances()) { if (mo["FreePhysicalMemory"] != null) { availablebytes = 1024 * long.Parse(mo["FreePhysicalMemory"].ToString()); } } return availablebytes; } } #endregion #region 物理内存 /// /// 获取物理内存 /// public long PhysicalMemory { get { return m_PhysicalMemory; } } #endregion #region 获得分区信息 /// /// 获取分区信息 /// public List<DiskInfo> GetLogicalDrives() { List<DiskInfo> drives = new List<DiskInfo>(); ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk"); ManagementObjectCollection disks = diskClass.GetInstances(); foreach (ManagementObject disk in disks) { // DriveType.Fixed 为固定磁盘(硬盘) if (int.Parse(disk["DriveType"].ToString()) == (int)DriveType.Fixed) { drives.Add(new DiskInfo(disk["Name"].ToString(), long.Parse(disk["Size"].ToString()), long.Parse(disk["FreeSpace"].ToString()))); } } return drives; } /// /// 获取特定分区信息 /// /// 盘符 public List<DiskInfo> GetLogicalDrives(char DriverID) { List<DiskInfo> drives = new List<DiskInfo>(); WqlObjectQuery wmiquery = new WqlObjectQuery("SELECT * FROM Win32_LogicalDisk WHERE DeviceID = ’" + DriverID + ":’"); ManagementObjectSearcher wmifind = new ManagementObjectSearcher(wmiquery); foreach (ManagementObject disk in wmifind.Get()) { if (int.Parse(disk["DriveType"].ToString()) == (int)DriveType.Fixed) { drives.Add(new DiskInfo(disk["Name"].ToString(), long.Parse(disk["Size"].ToString()), long.Parse(disk["FreeSpace"].ToString()))); } } return drives; } #endregion #region 获得进程列表 /// /// 获得进程列表 /// public List<ProcessInfo> GetProcessInfo() { List<ProcessInfo> pInfo = new List<ProcessInfo>(); Process[] processes = Process.GetProcesses(); foreach (Process instance in processes) { try { pInfo.Add(new ProcessInfo(instance.Id, instance.ProcessName, instance.TotalProcessorTime.TotalMilliseconds, instance.WorkingSet64, instance.MainModule.FileName)); } catch { } } return pInfo; } /// /// 获得特定进程信息 /// /// 进程名称 public List<ProcessInfo> GetProcessInfo(string ProcessName) { List<ProcessInfo> pInfo = new List<ProcessInfo>(); Process[] processes = Process.GetProcessesByName(ProcessName); foreach (Process instance in processes) { try { pInfo.Add(new ProcessInfo(instance.Id, instance.ProcessName, instance.TotalProcessorTime.TotalMilliseconds, instance.WorkingSet64, instance.MainModule.FileName)); } catch { } } return pInfo; } #endregion #region 结束指定进程 /// /// 结束指定进程 /// /// 进程的 Process ID public static void EndProcess(int pid) { try { Process process = Process.GetProcessById(pid); process.Kill(); } catch { } } #endregion #region 查找所有应用程序标题 /// /// 查找所有应用程序标题 /// /// 应用程序标题范型 public static List<string> FindAllApps(int Handle) { List<string> Apps = new List<string>(); int hwCurr; hwCurr = GetWindow(Handle, GW_HWNDFIRST); while (hwCurr > 0) { int IsTask = (WS_VISIBLE | WS_BORDER); int lngStyle = GetWindowLongA(hwCurr, GWL_STYLE); bool TaskWindow = ((lngStyle & IsTask) == IsTask); if (TaskWindow) { int length = GetWindowTextLength(new IntPtr(hwCurr)); StringBuilder sb = new StringBuilder(2 * length + 1); GetWindowText(hwCurr, sb, sb.Capacity); string strTitle = sb.ToString(); if (!string.IsNullOrEmpty(strTitle)) { Apps.Add(strTitle); } } hwCurr = GetWindow(hwCurr, GW_HWNDNEXT); } return Apps; } #endregion } public class DiskInfo { public DiskInfo() { } public DiskInfo(string name, long size, long freeSpace) { this.Name = name; this.Size = size; this.FreeSpace = freeSpace; } public string Name { get; set; } public long Size { get; set; } public long FreeSpace { get; set; } } public class ProcessInfo { public ProcessInfo() { } public ProcessInfo(int id, string processName, double totalMilliseconds, long workingSet64, string fileName) { Id = id; ProcessName = processName; TotalMilliseconds = totalMilliseconds; WorkingSet64 = workingSet64; FileName = fileName; } public int Id { get; set; } public string ProcessName { get; set; } public double TotalMilliseconds { get; set; } public long WorkingSet64 { get; set; } public string FileName { get; set; } } } |
参考: https://www.cnblogs.com/zery/p/6256850.html https://www.cnblogs.com/maowang1991/archive/2013/08/27/3285983.html
View Details管理-->事件查看器 可以查看【应用程序】、【安全】、【系统】等分类的日志
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
static void Main(string[] args) { EventLog eventlog = new EventLog(); eventlog.Log = "Security"; //"Application"应用程序, "Security"安全, "System"系统 EventLogEntryCollection eventLogEntryCollection = eventlog.Entries; foreach (EventLogEntry entry in eventLogEntryCollection) { //if (entry.EventID == 4624) //{ // continue; //} string info = string.Empty; if (@"TaskScheduler" == entry.Source.ToString()) { info += "类型:" + entry.EntryType.ToString() + ";"; info += "日期" + entry.TimeGenerated.ToLongDateString() + ";"; info += "时间" + entry.TimeGenerated.ToLongTimeString() + ";"; info += "来源" + entry.Source.ToString() + ";"; Console.WriteLine(info); } } } |
from:https://www.cnblogs.com/gossip/p/5249982.html
View Details