1.1 Bug場景:
1.1 SpringBoot項目啟動成功訪問任意接口都跳轉到login登錄頁面




2.1Bug原因
在 SpringBoot 項目中使用了 SpringSecurity ,這是因為在SpringBoot中,默認的Spring Security就是生效了的,此時的接口都是被保護的,我們需要通過驗證才能正常的訪問。Spring Security提供了一個默認的用戶,用戶名是user,而密碼則是啟動項目的時候自動生成的。
我們查看項目啟動的日志,會發現控制台有如下的一段Log

3.1 解決方法:
1. 如果不需要使用 SpringSecurity 去掉以依賴從新啟動項目就可以
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2. 如果需要使用 SpringSecurity ,就是用 user + 生成的密碼進行登陸,登陸成功后就跳轉正常的調用頁面。
或者對spring boot1.5配置security關閉http基本驗證,只需要在application.properites中配置即可,但是spring boot 2.0+之后這樣配置就不能生效了。
security.basic.enabled=false
3. 在項目中添加一個配置類(推薦使用第三種)
package com.ruoyi.api.config.security; 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.config.annotation.web.configuration.WebSecurityConfigurerAdapter; /** * 訪問接口不在調用security * @author Liyh * @date 2020/12/22 */ @Configuration @EnableWebSecurity public class CloseSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { //super.configure(http); http.csrf().disable(); //配置不需要登陸驗證 http.authorizeRequests().anyRequest().permitAll().and().logout().permitAll(); } }
