① spring安全框架在什么方法加密
你好,Spring框架是基础,它提供了基础的IOC和AOP服务。
Spring security 是在Spring的基础服务上,提供的基于Spring的企业应用系统安全访问控制的服务。
Spring Security框架大量使用了Spring框架的AOP服务。它不能脱离Spring而使用。
② spring文件上传
万变不离其宗,要实现文件的上传需要对应的JAR包:
1、commons-fileupload-1.2.2.jar
2、commons-io-2.0.1.jar
③ spring配置文件中的数据库用户名和密码怎么加密
一般spring容器启动时,通过PropertyPlaceholderConfigurer类读取jdbc.properties文件里的数据库配置信息。
通过这个原理,我们把加密后的数据库配置信息放到jdbc.properties文件里,然后自定义一个继承PropertyPlaceholderConfigurer的类重写processProperties方法,实现解密,把解密后的信息又放回去。
④ spring实现对properties文件怎么加密
使用注解声明事物参看下spring2.5的新特性;在方法前面加上@transation注解,就具有了事物功能;
⑤ 如何能让spring框架加载加密后的.class文件
加密:使用AES加密,将文件的字节码读取,对字节码进行加密后替换源文件
Java代码
/**
*
* 字节加密
*/
public static byte[] encrypt(byte[] data, String key) throws Exception {
Key k = toKey(Base64.decode(key));
byte[] raw = k.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
return cipher.doFinal(data);
}
/**
*
* 字节加密
*/
public static byte[] encrypt(byte[] data, String key) throws Exception {
Key k = toKey(Base64.decode(key));
byte[] raw = k.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
return cipher.doFinal(data);
}
解密:
1、在tomcat的WebappClassLoader中修改源码(自动义类加载器);
2、修改spring源码Code包源码。
加密方法
Java代码
public static byte[] decrypt(byte[] data, String key) throws Exception {
Key k = toKey(Base64.decode(key));
byte[] raw = k.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
return cipher.doFinal(data);
}
public static byte[] decrypt(byte[] data, String key) throws Exception {
Key k = toKey(Base64.decode(key));
byte[] raw = k.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
return cipher.doFinal(data);
}
在 WebappClassLoader中解密
Java代码
/**
* 判断如需是需要解密的类进行数据处理
* */
//--------------------------------------start----------------------------------//
byte []data=null;
try {
if(isDecode(name)){
System.out.println("2818:--&&&-"+name);
data=AESUtils.decrypt(entry.binaryContent, key);
}else{
data=entry.binaryContent;
}
} catch (Exception e) {
e.printStackTrace();
}
try {
clazz = defineClass(name, data, 0,
data.length,
new CodeSource(entry.codeBase, entry.certificates));
//--------------------------------------end----------------------------------//
/**
* 判断如需是需要解密的类进行数据处理
* */
//--------------------------------------start----------------------------------//
byte []data=null;
try {
if(isDecode(name)){
System.out.println("2818:--&&&-"+name);
data=AESUtils.decrypt(entry.binaryContent, key);
}else{
data=entry.binaryContent;
}
} catch (Exception e) {
e.printStackTrace();
}
try {
clazz = defineClass(name, data, 0,
data.length,
new CodeSource(entry.codeBase, entry.certificates));
//--------------------------------------end----------------------------------//
在spring的code包的SimpleMetadataReader修改器构造函数
Java代码
// TODO 修改源码判断是否需要解密
SimpleMetadataReader(Resource resource, ClassLoader classLoader)
throws IOException {
InputStream is = resource.getInputStream();
ClassReader classReader = null;
try {
String name = "";
if (resource.getURI().toString().indexOf("jar:file") == -1) {
name = resource.getFile().getAbsolutePath();
if (!"".equals(name) && isDecode(name, cams)) {
byte[] data = inputStreamToByte(is);
try {
is = new ByteArrayInputStream(AESUtils.decrypt(data,
key));
// is = new ByteArrayInputStream(data);
} catch (Exception e) {
e.printStackTrace();
}
}
}
classReader = new ClassReader(is);
} finally {
is.close();
}
// TODO 修改源码判断是否需要解密
SimpleMetadataReader(Resource resource, ClassLoader classLoader)
throws IOException {
InputStream is = resource.getInputStream();
ClassReader classReader = null;
try {
String name = "";
if (resource.getURI().toString().indexOf("jar:file") == -1) {
name = resource.getFile().getAbsolutePath();
if (!"".equals(name) && isDecode(name, cams)) {
byte[] data = inputStreamToByte(is);
try {
is = new ByteArrayInputStream(AESUtils.decrypt(data,
key));
// is = new ByteArrayInputStream(data);
} catch (Exception e) {
e.printStackTrace();
}
}
}
classReader = new ClassReader(is);
} finally {
is.close();
}
在同样需要进行解密。
注:(此加密有弊端)
1、加密解密算法需保持一致。
2、加密加密密钥需是同一密钥。
⑥ spring提供的几种密码加密方式
第一种:不使用任何加密方式的配置
[html]view plain
<beanid="AuthenticationProvider"
class="org.acegisecurity.providers..DaoAuthenticationProvider">
<propertyname="userDetailsService"ref="userDetailsService"/>
<!--明文加密,不使用任何加密算法,在不指定该配置的情况下,Acegi默认采用的就是明文加密-->
<!--<propertyname="passwordEncoder"><beanclass="org.acegisecurity.providers.encoding.PlaintextPasswordEncoder">
<propertyname="ignorePasswordCase"value="true"></property></bean></property>-->
</bean>
<beanid="AuthenticationProvider"class="org.acegisecurity.providers..DaoAuthenticationProvider">
<propertyname="userDetailsService"ref="userDetailsService"/>
<propertyname="passwordEncoder">
<beanclass="org.acegisecurity.providers.encoding.Md5PasswordEncoder">
<!--false表示:生成32位的Hex版,这也是encodeHashAsBase64的,Acegi默认配置;true表示:生成24位的Base64版-->
<propertyname="encodeHashAsBase64"value="false"/>
</bean>
</property>
</bean>
<beanid="AuthenticationProvider"class="org.acegisecurity.providers..DaoAuthenticationProvider">
<propertyname="userDetailsService"ref="userDetailsService"/>
<propertyname="passwordEncoder">
<beanclass="org.acegisecurity.providers.encoding.Md5PasswordEncoder">
<propertyname="encodeHashAsBase64"value="false"/>
</bean>
</property>
<!--对密码加密算法中使用特定的加密盐及种子-->
<propertyname="saltSource">
<beanclass="org.acegisecurity.providers..salt.SystemWideSaltSource">
<propertyname="systemWideSalt"value="acegisalt"/>
</bean>
</property>
</bean>
<beanid="AuthenticationProvider"class="org.acegisecurity.providers..DaoAuthenticationProvider">
<propertyname="userDetailsService"ref="userDetailsService"/>
<propertyname="passwordEncoder">
<beanclass="org.acegisecurity.providers.encoding.Md5PasswordEncoder">
<propertyname="encodeHashAsBase64"value="false"/>
</bean>
</property>
<!--对密码加密算法中使用特定的加密盐及种子-->
<propertyname="saltSource">
<!--通过动态的加密盐进行加密,该配置通过用户名提供加密盐,通过UserDetails的getUsername()方式-->
<beanclass="org.acegisecurity.providers..salt.ReflectionSaltSource">
<propertyname="userPropertyToUse"value="getUsername"/>
</bean>
</property>
</bean>
<beanid="AuthenticationProvider"class="org.acegisecurity.providers..DaoAuthenticationProvider">
<propertyname="userDetailsService"ref="userDetailsService"/>
<propertyname="passwordEncoder">
<beanclass="org.acegisecurity.providers.encoding.ShaPasswordEncoder">
<constructor-argvalue="256"/>
<propertyname="encodeHashAsBase64"value="false"/>
</bean>
</property>
</bean>
<beanid="AuthenticationProvider"class="org.acegisecurity.providers..DaoAuthenticationProvider">
<propertyname="userDetailsService"ref="userDetailsService"/>
<propertyname="passwordEncoder">
<beanclass="org.acegisecurity.providers.encoding.ShaPasswordEncoder">
<constructor-argvalue="SHA-256"/>
<propertyname="encodeHashAsBase64"value="false"/>
</bean>
</property>
</bean>
packageorg.hz.test;
importjava.security.NoSuchAlgorithmException;
importorg.springframework.security.authentication.encoding.Md5PasswordEncoder;
importorg.springframework.security.authentication.encoding.ShaPasswordEncoder;
publicclassMD5Test{
publicstaticvoidmd5(){
Md5PasswordEncodermd5=newMd5PasswordEncoder();
//false表示:生成32位的Hex版,这也是encodeHashAsBase64的,Acegi默认配置;true表示:生成24位的Base64版
md5.setEncodeHashAsBase64(false);
Stringpwd=md5.encodePassword("1234",null);
System.out.println("MD5:"+pwd+"len="+pwd.length());
}
publicstaticvoidsha_256(){
ShaPasswordEncodersha=newShaPasswordEncoder(256);
sha.setEncodeHashAsBase64(true);
Stringpwd=sha.encodePassword("1234",null);
System.out.println("哈希算法256:"+pwd+"len="+pwd.length());
}
publicstaticvoidsha_SHA_256(){
ShaPasswordEncodersha=newShaPasswordEncoder();
sha.setEncodeHashAsBase64(false);
Stringpwd=sha.encodePassword("1234",null);
System.out.println("哈希算法SHA-256:"+pwd+"len="+pwd.length());
}
publicstaticvoidmd5_SystemWideSaltSource(){
Md5PasswordEncodermd5=newMd5PasswordEncoder();
md5.setEncodeHashAsBase64(false);
//使用动态加密盐的只需要在注册用户的时候将第二个参数换成用户名即可
Stringpwd=md5.encodePassword("1234","acegisalt");
System.out.println("MD5SystemWideSaltSource:"+pwd+"len="+pwd.length());
}
publicstaticvoidmain(String[]args){
md5();//使用简单的MD5加密方式
sha_256();//使用256的哈希算法(SHA)加密
sha_SHA_256();//使用SHA-256的哈希算法(SHA)加密
md5_SystemWideSaltSource();//使用MD5再加全局加密盐加密的方式加密
}
}
第二种:MD5方式加密
[html]view plain
第三种:使用MD5加密,并添加全局加密盐
Java代码
[html]view plain
第四种:使用MD5加密,并添加动态加密盐
[html]view plain
第五种:使用哈希算法加密,加密强度为256
[html]view plain
第六种:使用哈希算法加密,加密强度为SHA-256
[html]view plain
上述配置只是在Acegi通过表单提交的用户认证信息中的密码做各种加密操作。而我们存储用户密码的时候,可以通过一下程序完成用户密码操作:
[java]view plain
⑦ spring+mybatis数据源密码怎样加密有没有必要
不需要加密,可以直接放置在spring配置文件中,也可以定义应用程序服务器数据源,spring利用jndi数据源
⑧ springboot上传文件到服务器aes加密
业务需求:数据库中的用户名密码明文存储在配置文件中,不是十分安全。所以将数据库中的用户名密码使用AES对称加密放入配置文件中,达到加密效果。同时也不想使用tomcat等中间件等太繁重,就使用了spring boot 轻量级框架。个人比较菜,轻喷。
关于如何搭建spring boot项目其他的人说的很详细 参考初识Spring Boot框架
入口类代码
@Controller
@SpringBootApplication
@EnableAutoConfiguration
{
publicstaticvoidmain(String[]args){
SpringApplication.run(Aesdemo1Application.class,args);
}
}
运行时只要运行main方法 或者打包后java -jar 即可(写成.bat文件 点击运行方便简单)
@Controller
publicclassGetKeyController{
@GetMapping("/getkey")
publicStringgreetingForm(Modelmodel){
model.addAttribute("passwordBean",newPasswordBean());return"index";
}
@PostMapping("/getkey")
publicStringgreetingSubmit(@){
Strings1=AESUtil.encrypt(passwordBean.getPassword(),passwordBean.getVar1());
passwordBean.setVar2(s1);
return"result";
}
}
启动后有这里还有一个控制器类
浏览器地址输入 http://localhost:8080/getkey 即可跳转到greetingForm 方法,赋入PasswordBean属性后 跳转到index.html
PasswordBean 是自己定义的bean类 里面有password var1 var2 3个属性
index.html代码
<!DOCTYPEhtml>
<htmllang="en"xmlns:th="http://www.thymeleaf.org">
<head>
<metacharset="UTF-8"/>
<title>Title</title>
</head>
<body>
<formaction="#"th:action="@{/getkey}"th:object="${passwordBean}"method="post">
<p>密码:<inputtype="text"th:field="*{password}"/></p>
<p>加密字符:<inputtype="text"th:field="*{var1}"/></p>
<p><inputtype="submit"value="Submit"/>
<inputtype="reset"value="Reset"/></p>
</form>
</body>
</html>
注意使用了thymeleaf框架 所以必须引入
输入要加密的和盐即可获得通过post方法到result即可获得加密后字符串
<!DOCTYPEhtml>
<htmllang="en"xmlns:th="http://www.thymeleaf.org">
<head>
<metacharset="UTF-8"/>
<title>Title</title>
</head>
<body>
<h1>Result</h1>
<pth:text="'密码:'+${passwordBean.password}"/>
<pth:text="'加密字符:'+${passwordBean.var1}"/>
<pth:text="'加密后字符:'+${passwordBean.var2}"/>
<ahref="/getkey">Submitanothermessage</a>
</body>
</html>
⑨ springboot项目中怎样预防文件上传漏洞急需
预防文件上传漏洞
1.为了防范用户上传恶意的可执行文件和脚本,以及将文件上传服务器当做免费的文件存储服务器使用,需要对上传的文件类型进行白名单(非黑名单,这点非常重要)校验,并且限制上传文件的大小,上传的文件,需要进行重新命名,使攻击者无法猜测到上传文件的访问路径。
2.对于上传的文件来说,不能简单的通过后缀名称来判断文件的类型,因为恶意攻击可以将可
执行文件的后缀名称改成图片或者其他的后缀类型,诱导用户执行。因此,判断文件类型需
要使用更安全的方式。
3.很多类型的文件,起始的几个字节内容是固定的,因此,根据这几个字节的内容,就可以确
定文件类型,这几个字节也被称为魔数(magic number)。(将文件转换成二进制)
⑩ 在Spring中如何使用加密外部属性文件
1.jdbc.properties文件
driverClassName=com.mysql.jdbc.Driver
url:jdbc:mysql://localhost:3306/testdb
username=root
password=123
1
2
3
4
2.xml文件引入属性文件
<!-- 引入配置文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfiguer"
p:location="class:jdbc.preperties"
p:fileEncoding="utf-8"/>
<!-- 使用属性值 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${drverClassName}"
p:url="${url}"
p:username="${userName}"
p:password="${password}"/>
1
2
3
4
5
6
7
8
9
10
11
3.PropertyPlaceholderConfigurer其他属性
locations:配置多个配置文件(配置 list 一样)
fileEncoding:编码格式
order:如歌配置多个PropertyPlaceholderConfigurer,通过该属性指定优先顺序
placeholderPrefix:指定占位符前缀,默认为“${”
placeholderSuffix:占位符后缀,默认为“}”
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/opt/demo/config/demo-db.properties</value>
<value>classpath:/opt/demo/config/demo-db2.properties</value>
</list>
</property>
</bean>
1
2
3
4
5
6
7
8
4.使用”<”context:property-placeholder”>”引入属性文件
//这种方式比较优雅,但如果想要配置日他额外高级属性,如属性加密,使用数据库保存配置信息等,必须使用扩展的PropertyPlaceholderConfigurer类并使用bean配置方式
<context:property-placeholder location="class:jdbc.properties"/>
//引入多个配置文件 逗号分隔
<context:property-placeholder
location="classpath:constants.properties ,
classpath:constants_new.properties"
fileEncoding="utf-8"
/>
1
2
3
4
5
6
7
8
9
10
11
5.在基于注解的java类配置中引用属性
@Component
public class Test{
//@Value注解可以为Bean注入一个字面量,也可通过@Value("#{proName}")形式注入值
@Value("com.mysql.jdbc.Driver")
private String driverClassName;
@Value("${url}")
private String url;
}