mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
update > optimize code for issue #1845
This commit is contained in:
+98
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.spring.boot.autoconfigure;
|
||||
|
||||
import com.alibaba.druid.filter.config.ConfigFilter;
|
||||
import com.alibaba.druid.filter.encoding.EncodingConvertFilter;
|
||||
import com.alibaba.druid.filter.logging.CommonsLogFilter;
|
||||
import com.alibaba.druid.filter.logging.Log4j2Filter;
|
||||
import com.alibaba.druid.filter.logging.Log4jFilter;
|
||||
import com.alibaba.druid.filter.logging.Slf4jLogFilter;
|
||||
import com.alibaba.druid.filter.stat.StatFilter;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.wall.WallFilter;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
/**
|
||||
* @author lihengming [89921218@qq.com]
|
||||
*/
|
||||
@ConfigurationProperties("spring.datasource.druid")
|
||||
class DruidDataSourceWrapper extends DruidDataSource implements InitializingBean {
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
//if not found prefix 'spring.datasource.druid' jdbc properties ,'spring.datasource' prefix jdbc properties will be used.
|
||||
if (super.getUsername() == null) {
|
||||
super.setUsername(env.getProperty("spring.datasource.username"));
|
||||
}
|
||||
if (super.getPassword() == null) {
|
||||
super.setPassword(env.getProperty("spring.datasource.password"));
|
||||
}
|
||||
if (super.getUrl() == null) {
|
||||
super.setUrl(env.getProperty("spring.datasource.url"));
|
||||
}
|
||||
if (super.getDriverClassName() == null) {
|
||||
super.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired(required = false)
|
||||
public void addStatFilter(StatFilter statFilter) {
|
||||
super.filters.add(statFilter);
|
||||
}
|
||||
|
||||
@Autowired(required = false)
|
||||
public void addConfigFilter(ConfigFilter configFilter) {
|
||||
super.filters.add(configFilter);
|
||||
}
|
||||
|
||||
@Autowired(required = false)
|
||||
public void addEncodingConvertFilter(EncodingConvertFilter encodingConvertFilter) {
|
||||
super.filters.add(encodingConvertFilter);
|
||||
}
|
||||
|
||||
@Autowired(required = false)
|
||||
public void addSlf4jLogFilter(Slf4jLogFilter slf4jLogFilter) {
|
||||
super.filters.add(slf4jLogFilter);
|
||||
}
|
||||
|
||||
@Autowired(required = false)
|
||||
public void addLog4jFilter(Log4jFilter log4jFilter) {
|
||||
super.filters.add(log4jFilter);
|
||||
}
|
||||
|
||||
@Autowired(required = false)
|
||||
public void addLog4j2Filter(Log4j2Filter log4j2Filter) {
|
||||
super.filters.add(log4j2Filter);
|
||||
}
|
||||
|
||||
@Autowired(required = false)
|
||||
public void addCommonsLogFilter(CommonsLogFilter commonsLogFilter) {
|
||||
super.filters.add(commonsLogFilter);
|
||||
}
|
||||
|
||||
@Autowired(required = false)
|
||||
public void addWallFilter(WallFilter wallFilter) {
|
||||
super.filters.add(wallFilter);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.alibaba.druid.spring.boot.autoconfigure.metadata;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import org.springframework.boot.autoconfigure.jdbc.metadata.AbstractDataSourcePoolMetadata;
|
||||
import org.springframework.boot.autoconfigure.jdbc.metadata.DataSourcePoolMetadata;
|
||||
|
||||
/**
|
||||
* {@link DataSourcePoolMetadata} for a Druid DataSource.
|
||||
*
|
||||
* @author lihengming [89921218@qq.com]
|
||||
*/
|
||||
public class DruidDataSourcePoolMetadata extends AbstractDataSourcePoolMetadata<DruidDataSource> {
|
||||
/**
|
||||
* Create an instance with the data source to use.
|
||||
*
|
||||
* @param dataSource the data source
|
||||
*/
|
||||
public DruidDataSourcePoolMetadata(DruidDataSource dataSource) {
|
||||
super(dataSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getActive() {
|
||||
return getDataSource().getActiveCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getMax() {
|
||||
return getDataSource().getMaxActive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getMin() {
|
||||
return getDataSource().getMinIdle();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValidationQuery() {
|
||||
return getDataSource().getValidationQuery();
|
||||
}
|
||||
}
|
||||
+200
@@ -0,0 +1,200 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.spring.boot.autoconfigure.properties;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* @author lihengming [89921218@qq.com]
|
||||
*/
|
||||
@ConfigurationProperties("spring.datasource.druid")
|
||||
public class DruidStatProperties {
|
||||
private String[] aopPatterns;
|
||||
private StatViewServlet statViewServlet = new StatViewServlet();
|
||||
private WebStatFilter webStatFilter = new WebStatFilter();
|
||||
|
||||
|
||||
public String[] getAopPatterns() {
|
||||
return aopPatterns;
|
||||
}
|
||||
|
||||
public void setAopPatterns(String[] aopPatterns) {
|
||||
this.aopPatterns = aopPatterns;
|
||||
}
|
||||
|
||||
public StatViewServlet getStatViewServlet() {
|
||||
return statViewServlet;
|
||||
}
|
||||
|
||||
public void setStatViewServlet(StatViewServlet statViewServlet) {
|
||||
this.statViewServlet = statViewServlet;
|
||||
}
|
||||
|
||||
public WebStatFilter getWebStatFilter() {
|
||||
return webStatFilter;
|
||||
}
|
||||
|
||||
public void setWebStatFilter(WebStatFilter webStatFilter) {
|
||||
this.webStatFilter = webStatFilter;
|
||||
}
|
||||
|
||||
public static class StatViewServlet {
|
||||
/**
|
||||
* Enable StatViewServlet.
|
||||
*/
|
||||
private boolean enabled = true;
|
||||
private String urlPattern;
|
||||
private String allow;
|
||||
private String deny;
|
||||
private String loginUsername;
|
||||
private String loginPassword;
|
||||
private String resetEnable;
|
||||
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public String getUrlPattern() {
|
||||
return urlPattern;
|
||||
}
|
||||
|
||||
public void setUrlPattern(String urlPattern) {
|
||||
this.urlPattern = urlPattern;
|
||||
}
|
||||
|
||||
public String getAllow() {
|
||||
return allow;
|
||||
}
|
||||
|
||||
public void setAllow(String allow) {
|
||||
this.allow = allow;
|
||||
}
|
||||
|
||||
public String getDeny() {
|
||||
return deny;
|
||||
}
|
||||
|
||||
public void setDeny(String deny) {
|
||||
this.deny = deny;
|
||||
}
|
||||
|
||||
public String getLoginUsername() {
|
||||
return loginUsername;
|
||||
}
|
||||
|
||||
public void setLoginUsername(String loginUsername) {
|
||||
this.loginUsername = loginUsername;
|
||||
}
|
||||
|
||||
public String getLoginPassword() {
|
||||
return loginPassword;
|
||||
}
|
||||
|
||||
public void setLoginPassword(String loginPassword) {
|
||||
this.loginPassword = loginPassword;
|
||||
}
|
||||
|
||||
public String getResetEnable() {
|
||||
return resetEnable;
|
||||
}
|
||||
|
||||
public void setResetEnable(String resetEnable) {
|
||||
this.resetEnable = resetEnable;
|
||||
}
|
||||
}
|
||||
|
||||
public static class WebStatFilter {
|
||||
/**
|
||||
* Enable WebStatFilter.
|
||||
*/
|
||||
private boolean enabled = true;
|
||||
private String urlPattern;
|
||||
private String exclusions;
|
||||
private String sessionStatMaxCount;
|
||||
private String sessionStatEnable;
|
||||
private String principalSessionName;
|
||||
private String principalCookieName;
|
||||
private String profileEnable;
|
||||
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public String getUrlPattern() {
|
||||
return urlPattern;
|
||||
}
|
||||
|
||||
public void setUrlPattern(String urlPattern) {
|
||||
this.urlPattern = urlPattern;
|
||||
}
|
||||
|
||||
public String getExclusions() {
|
||||
return exclusions;
|
||||
}
|
||||
|
||||
public void setExclusions(String exclusions) {
|
||||
this.exclusions = exclusions;
|
||||
}
|
||||
|
||||
public String getSessionStatMaxCount() {
|
||||
return sessionStatMaxCount;
|
||||
}
|
||||
|
||||
public void setSessionStatMaxCount(String sessionStatMaxCount) {
|
||||
this.sessionStatMaxCount = sessionStatMaxCount;
|
||||
}
|
||||
|
||||
public String getSessionStatEnable() {
|
||||
return sessionStatEnable;
|
||||
}
|
||||
|
||||
public void setSessionStatEnable(String sessionStatEnable) {
|
||||
this.sessionStatEnable = sessionStatEnable;
|
||||
}
|
||||
|
||||
public String getPrincipalSessionName() {
|
||||
return principalSessionName;
|
||||
}
|
||||
|
||||
public void setPrincipalSessionName(String principalSessionName) {
|
||||
this.principalSessionName = principalSessionName;
|
||||
}
|
||||
|
||||
public String getPrincipalCookieName() {
|
||||
return principalCookieName;
|
||||
}
|
||||
|
||||
public void setPrincipalCookieName(String principalCookieName) {
|
||||
this.principalCookieName = principalCookieName;
|
||||
}
|
||||
|
||||
public String getProfileEnable() {
|
||||
return profileEnable;
|
||||
}
|
||||
|
||||
public void setProfileEnable(String profileEnable) {
|
||||
this.profileEnable = profileEnable;
|
||||
}
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.spring.boot.autoconfigure.stat;
|
||||
|
||||
import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
|
||||
import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;
|
||||
import org.aopalliance.aop.Advice;
|
||||
import org.springframework.aop.Advisor;
|
||||
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
|
||||
import org.springframework.aop.support.RegexpMethodPointcutAdvisor;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* @author lihengming [89921218@qq.com]
|
||||
*/
|
||||
@ConditionalOnProperty("spring.datasource.druid.aop-patterns")
|
||||
public class DruidSpringAopConfiguration {
|
||||
@Value("${spring.aop.proxy-target-class:false}")
|
||||
private boolean proxyTargetClass;
|
||||
|
||||
@Bean
|
||||
public Advice advice() {
|
||||
return new DruidStatInterceptor();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Advisor advisor(DruidStatProperties properties) {
|
||||
return new RegexpMethodPointcutAdvisor(properties.getAopPatterns(), advice());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() {
|
||||
DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
|
||||
advisorAutoProxyCreator.setProxyTargetClass(proxyTargetClass);
|
||||
return advisorAutoProxyCreator;
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.spring.boot.autoconfigure.stat;
|
||||
|
||||
import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
|
||||
import com.alibaba.druid.support.http.StatViewServlet;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.web.servlet.ServletRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* @author lihengming [89921218@qq.com]
|
||||
*/
|
||||
@ConditionalOnWebApplication
|
||||
@ConditionalOnProperty(name = "spring.datasource.druid.stat-view-servlet.enabled", havingValue = "true", matchIfMissing = true)
|
||||
public class DruidStatViewServletConfiguration {
|
||||
@Bean
|
||||
public ServletRegistrationBean servletRegistrationBean(DruidStatProperties properties) {
|
||||
DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
|
||||
ServletRegistrationBean registrationBean = new ServletRegistrationBean();
|
||||
registrationBean.setServlet(new StatViewServlet());
|
||||
registrationBean.addUrlMappings(config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*");
|
||||
if (config.getAllow() != null) {
|
||||
registrationBean.addInitParameter("allow", config.getAllow());
|
||||
}
|
||||
if (config.getDeny() != null) {
|
||||
registrationBean.addInitParameter("deny", config.getDeny());
|
||||
}
|
||||
if (config.getLoginUsername() != null) {
|
||||
registrationBean.addInitParameter("loginUsername", config.getLoginUsername());
|
||||
}
|
||||
if (config.getLoginPassword() != null) {
|
||||
registrationBean.addInitParameter("loginPassword", config.getLoginPassword());
|
||||
}
|
||||
if (config.getResetEnable() != null) {
|
||||
registrationBean.addInitParameter("resetEnable", config.getResetEnable());
|
||||
}
|
||||
return registrationBean;
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 1999-2017 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.spring.boot.autoconfigure.stat;
|
||||
|
||||
import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
|
||||
import com.alibaba.druid.support.http.WebStatFilter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* @author lihengming [89921218@qq.com]
|
||||
*/
|
||||
@ConditionalOnWebApplication
|
||||
@ConditionalOnProperty(name = "spring.datasource.druid.web-stat-filter.enabled", havingValue = "true", matchIfMissing = true)
|
||||
public class DruidWebStatFilterConfiguration {
|
||||
@Bean
|
||||
public FilterRegistrationBean filterRegistrationBean(DruidStatProperties properties) {
|
||||
DruidStatProperties.WebStatFilter config = properties.getWebStatFilter();
|
||||
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
|
||||
WebStatFilter filter = new WebStatFilter();
|
||||
registrationBean.setFilter(filter);
|
||||
registrationBean.addUrlPatterns(config.getUrlPattern() != null ? config.getUrlPattern() : "/*");
|
||||
registrationBean.addInitParameter("exclusions", config.getExclusions() != null ? config.getExclusions() : "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
|
||||
if (config.getSessionStatEnable() != null) {
|
||||
registrationBean.addInitParameter("sessionStatEnable", config.getSessionStatEnable());
|
||||
}
|
||||
if (config.getSessionStatMaxCount() != null) {
|
||||
registrationBean.addInitParameter("sessionStatMaxCount", config.getSessionStatMaxCount());
|
||||
}
|
||||
if (config.getPrincipalSessionName() != null) {
|
||||
registrationBean.addInitParameter("principalSessionName", config.getPrincipalSessionName());
|
||||
}
|
||||
if (config.getPrincipalCookieName() != null) {
|
||||
registrationBean.addInitParameter("principalCookieName", config.getPrincipalCookieName());
|
||||
}
|
||||
if (config.getProfileEnable() != null) {
|
||||
registrationBean.addInitParameter("profileEnable", config.getProfileEnable());
|
||||
}
|
||||
return registrationBean;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user