生而为人

程序员的自我修养

0%

(exclude = DataSourceAutoConfiguration.class)

DynamicDataSourceAutoConfiguration

自动化配置的问题

需要排除的配置

[Spring Boot 排除自动配置的 4 种方法,关键时刻很有用!]

  1. @SpringBootApplication
  2. @EnableAutoConfiguration
  3. @SpringCloudApplication
  4. spring.autoconfigure.exclude

Spring Boot自动化配置的利弊及解决之道

排除的类总结
  1. @SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)

注解用法

@RequestBody和@RequestParam注解使用

@Async

  1. 可以指定线程池

不生效原因:

  1. 需要保证调用函数与注解修饰的函数在不同类中,因为不会走代理类。详解 详解2

@Value

注入static变量四种方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import lombok.Getter;

@Component
public class GlobalValue {

@Getter
public static String DATABASE;

@Value("${mysql.db:test}")
public void setDatabase(String db) {
DATABASE = db;
}
// 这里要特别注意:
// 1. @value需要注释set函数上
// 2. 自动生成的getter和setter方法,会带有static的限定符,需要去掉,才可以。
}

@Value
static
https://blog.csdn.net/ZYC88888/article/details/87863038

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘pubApiPush’: Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder ‘group_url’ in value “${group_url}”

group_url: [[查看结果|http://wolf.waimai.dev.sankuai.com/group/hubble/page/dolphinManage]]

%20 空格
%21 !
%23 #
%24 $
%25 %
%26 &
%27
%28 (
%29 )
%2A *
%2B +
%2C ,
%2E .
%2F /
%3A :
%3B ;
%3D =
%3F ?
%40 @
%5B [
%5C /
%5D ]

sql注入漏洞

修复方案:
如果拼接参数是SQL的查询参数都可以通过将mybatis配置文件中的 $ 改为 #,来解决
替换规则如下:
1.将 WHERE some_field = ‘${变量}’ 替换为 WHERE some_field = #{param}

2.将 like ‘%${变量}%’ 替换为 like concat(‘%’, #{变量}, ‘%’)

3.将 like concat(‘%’, ${变量}, ‘%’) 替换成 like concat(‘%’, #{变量}, ‘%’)

4.将 WHERE some_field IN (${变量}) 替换为

WHERE some_field IN

#{item}

对于拼接的变量不是SQL参数的,而是字段名、表名的情况,可以使用安全SDK检查输入是否是合法的标识符。
1.pom 引入安全SDK依赖

com.sankuai.security
sec-sdk
${尽可能最新版本}

\2. 代码中检查输入的变量的值是否是合法的标识符。
String sortName = req.getParameter(“sortName”);
if (sortName != null && !SecSdk.isValidSqlIdentifier(sortName)) {
// 危险,可能有SQL注入,
// return or throw exception
}

参考文档:
SQL注入介绍:https://sectraining.sankuai.com/?source=source-scanner/#/document/30001
安全SDK文档:https://km.sankuai.com/page/234776924

CORS 安全配置不当

XSS 跨站脚本攻击

越权类安全问题

interface 属性为 private static final

abstract 父类中的属性,默认式private的,非private的在子类中可以使用

joda.time

1
2
String pattern ="dd-MMM-yy hh.mm.ss aa";
DateTimeFormat.forPattern(pattern)

字符串补位

1
"%02d".format(currentTime.getMinuteOfHour)