druid監控設定、AjaxDomainFilter允許跨域響應設定成commpon公共jar包 教程 火推
原始碼下載:https://gitee.com/qq1350048638/lj_common
lj_common構成
別的工程在pom 引入,
相同重複的工作 就都通過lj_common 來操作,相當於把lj_common看成一個jar包
<dependency>
<groupId>com.lj</groupId>
<artifactId>lj_common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
pom.xml
把相關包引入進來,重複的全部放到這裡,方便其他工程直接呼叫即可
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lj</groupId>
<artifactId>lj_common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.7.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<!-- bean掃描、自動配置、@bean定義 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- 健康監控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- jdbc\連線池 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- springboot核心mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.2</version>
</dependency>
<!-- mvc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 熱部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 引入druid連線池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<!-- mysql驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</project>
AjaxDomainFilter類
“GET,POST,DELETE,PUT”請求全部先走這裡並通過
httpResponse.setHeader 可設定域名請求通過白名單,及請求方式;
package com.lj.ovls.common.util;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
@WebFilter(servletNames={"dispatcherServlet"})
public class AjaxDomainFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request,
ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("--------AjaxDomainFilter-----------");
//設定允許跨域響應的參�?
HttpServletResponse httpResponse = (HttpServletResponse)response;
httpResponse.setHeader("Access-Control-Allow-Origin", "*");
httpResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,DELETE,PUT");
//放過請求繼續處理
chain.doFilter(request, response);
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
}
com.lj.ovls.common.druid 包 有3個類
package com.lj.ovls.common.druid;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
public class DruidConfig {
@Value("${spring.datasource.url}")
private String dbUrl;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driverClassName;
@Bean
public DataSource createDruid(){
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(dbUrl);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);
try {
datasource.setFilters("stat");//啟動sql監控
} catch (SQLException e) {
e.printStackTrace();
}
return datasource;
}
}
package com.lj.ovls.common.druid;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import com.alibaba.druid.support.http.WebStatFilter;
@WebFilter(urlPatterns="/*",initParams={
@WebInitParam(name="exclusions",value=".js,.css,.jpg,png,/druid/*")
})
public class DruidStatFilter extends WebStatFilter{
}
package com.lj.ovls.common.druid;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import com.alibaba.druid.support.http.StatViewServlet;
@WebServlet(urlPatterns="/druid/*",initParams={
@WebInitParam(name="loginUsername",value="xdl"),
@WebInitParam(name="loginPassword",value="123"),
})
public class DruidStatServlet extends StatViewServlet{
private static final long serialVersionUID = 1L;
}
配置好之後,只需要啟動對應的工程 即可展示出監控資訊
http://localhost:8881/druid/ 賬號xdl 密碼 123 可以自己設定
ResponseResult類
用來設定響應結果給前端 json返回給前端
package com.lj.ovls.common.entity;
import java.io.Serializable;
/** * 統一響應結果物件 * @author */
public class ResponseResult implements Serializable{
/* * 處理狀態 */
private int status;
/* 訊息 */
private String msg;
/* * 資料 */
private Object data;
....get set 省略
}
相關文章
- PHP 設定多域名允許跨域訪問PHP跨域
- 設定MYSQL允許用IP訪問MySql
- MYSQL設定允許所有IP訪問MySql
- 允許跨域請求跨域
- Excel如何設定列印區域?Exce列印區域設定教程Excel
- egg 設定跨域白名單跨域
- 為什麼hashtable不允許設定Null但是hashmap允許?NullHashMap
- .NetCore在跨域時設定自定義響應頭的注意事項NetCore跨域
- 設定允許root使用者登入SSH
- Mysql設定允許外網訪問(圖文)MySql
- token響應式設定
- 蘋果iPhone XR/XS Max怎麼監控流量?iPhone流量監控設定教程蘋果iPhone
- win10允許遠端桌面連線如何設定_win10怎樣設定允許遠端桌面連線Win10
- Laravel API 允許跨域訪問LaravelAPI跨域
- SpringBoot 允許跨域配置Spring Boot跨域
- Go Gin 允許跨域訪問Go跨域
- CROS跨域請求設定,偏重前端ROS跨域前端
- 怎樣設定才能允許外網訪問MySQLMySql
- 如何使flask允許跨域請求Flask跨域
- 【Angular】設定代理解決跨域問題Angular跨域
- 設定mysql允許外部IP連線的解決方法MySql
- linux下gssftp允許root登陸的設定(轉)LinuxFTP
- drf : 請求(Request)與響應(Response),全域性設定和區域性設定drf的預設配置項。
- 啟動google 瀏覽器允許跨域Go瀏覽器跨域
- Win10系統下怎麼設定只允許執行指定應用Win10
- 阿里雲圖片跨域訪問設定阿里跨域
- 配置Charles 設定手機代理並允許https請求HTTP
- 設定Mysql5.6允許外網訪問詳細流程MySql
- phpstudy設定允許遠端訪問mysql資料庫PHPMySql資料庫
- Mysql8 設定允許遠端連線 (Windows環境)MySqlWindows
- 筆記本DNS怎麼設定 Win10筆記本設定阿里公共DNS教程筆記DNSWin10阿里
- iOS 10設定應用許可權iOS
- springboot配置CORS允許跨域訪問Spring BootCORS跨域
- 讓 Weex UI 響應系統設定UI
- 如何設定 HomePod?HomePod設定教程分享
- 關於 Laravel 設定跨域的中介軟體整理Laravel跨域
- 設定谷歌瀏覽器跨域(本地除錯可用)谷歌瀏覽器跨域除錯
- Solaris 11 設定root使用者允許SSH遠端登入