This generated password is for development use only. Your security configuration must be updated bef

本文介绍了如何在SpringSecurity中通过application.yml手动配置登录账号/密码,以及项目启动后的自动秘钥生成过程。

1、在application.yml中手动配置SpringSecurity登录账号/密码

2、或者是项目启动后自动生成

51c70ea2-21fa-48a0-aefa-cc2e0a69120  秘钥
当在 IntelliJ IDEA 中出现 'This generated password is for development use only. Your security configuration must be updated before running your application in production.' 提示时,可从以下几方面解决: #### 更换开发用密码 在项目中查找生成开发用密码的相关代码,通常可能在配置文件(如 `application.properties` 或 `application.yml`)或者代码逻辑里。将开发用密码替换为更安全的密码。 例如,在 Spring Boot 的 `application.properties` 中: ```properties # 原开发用密码 # spring.datasource.password=generated_dev_password # 更换为安全密码 spring.datasource.password=your_secure_password ``` #### 更新安全配置 - **身份认证与授权**:加强身份认证机制,使用更安全的认证方式,如 OAuth 2.0、JWT(JSON Web Token)等。在 Spring Security 中配置 JWT 认证示例: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.web.SecurityFilterChain; @Configuration @EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .authorizeRequests() .anyRequest().authenticated() .and() .oauth2ResourceServer() .jwt(); return http.build(); } } ``` - **数据加密**:对敏感数据进行加密存储和传输。例如,使用 Java 的 `Cipher` 类对数据进行加密: ```java import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import java.nio.charset.StandardCharsets; import java.util.Base64; public class EncryptionExample { public static String encrypt(String plainText, SecretKey secretKey) throws Exception { Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8)); return Base64.getEncoder().encodeToString(encryptedBytes); } } ``` - **访问控制**:限制对应用资源的访问,只允许授权的用户或角色访问特定资源。在 Spring Security 中可以通过配置角色来实现: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.web.SecurityFilterChain; @Configuration @EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .anyRequest().authenticated() .and() .formLogin(); return http.build(); } } ``` #### 环境配置分离 将开发环境和生产环境的配置分开,避免在生产环境中使用开发用的配置。可以使用 Spring Boot 的多环境配置,在 `application.properties` 中: ```properties # 开发环境配置 spring.profiles.active=dev # 开发环境数据库配置 --- spring.config.activate.on-profile=dev spring.datasource.url=jdbc:mysql://localhost:3306/dev_db spring.datasource.username=dev_user spring.datasource.password=dev_password # 生产环境数据库配置 --- spring.config.activate.on-profile=prod spring.datasource.url=jdbc:mysql://prod-server:3306/prod_db spring.datasource.username=prod_user spring.datasource.password=prod_password ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值