當前位置:首頁 » 服務存儲 » spring文件上傳存儲加密
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

spring文件上傳存儲加密

發布時間: 2022-07-22 05:25:33

① 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>


  • 第二種:MD5方式加密

    [html]view plain

  • <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>


  • 第三種:使用MD5加密,並添加全局加密鹽

    Java代碼

    [html]view plain

  • <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>


  • 第四種:使用MD5加密,並添加動態加密鹽

    [html]view plain

  • <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>

  • 第五種:使用哈希演算法加密,加密強度為256

    [html]view plain

  • <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>

  • 第六種:使用哈希演算法加密,加密強度為SHA-256

    [html]view plain

  • <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>


  • 上述配置只是在Acegi通過表單提交的用戶認證信息中的密碼做各種加密操作。而我們存儲用戶密碼的時候,可以通過一下程序完成用戶密碼操作:

    [java]view plain

  • 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再加全局加密鹽加密的方式加密

  • }

  • }

⑦ 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;
}