SpringBoot整合Spring Security構(gòu)建安全的Web應用
Spring Security是一個強大的身份驗證和訪問控制框架,用于保護Spring應用程序。它提供了全面的安全服務,包括身份驗證、授權(quán)、攻擊防護等。本文將介紹如何在Spring Boot應用程序中整合Spring Security,以構(gòu)建一個安全可靠的Web應用。
1. 添加依賴
首先,需要在pom.xml文件中添加Spring Security的依賴:
<dependencies>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- 其他依賴... -->
</dependencies>
2. 配置Spring Security
在Spring Boot應用程序中,可以通過創(chuàng)建一個配置類來配置Spring Security。創(chuàng)建一個類,并添加@EnableWebSecurity注解,繼承WebSecurityConfigurerAdapter類:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
}
這個配置類指定了一些基本的安全規(guī)則,包括允許所有用戶訪問首頁和登錄頁面,要求其他頁面進行身份驗證。登錄頁面的路徑為/login。
3. 創(chuàng)建用戶服務
接下來,需要創(chuàng)建一個實現(xiàn)UserDetailsService接口的用戶服務類。這個類負責從數(shù)據(jù)庫或其他地方加載用戶信息。
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
@Service
public class CustomUserDetailsService implements UserDetailsService {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// 此處應從數(shù)據(jù)庫加載用戶信息
return User.withUsername(username)
.password("password")
.roles("USER")
.build();
}
}
在實際應用中,應該從數(shù)據(jù)庫中加載用戶信息,并根據(jù)實際需求進行密碼加密等處理。
4. 控制器和視圖
創(chuàng)建一個簡單的控制器來處理首頁、登錄頁和其他頁面:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/")
public String home() {
return "home";
}
@GetMapping("/login")
public String login() {
return "login";
}
@GetMapping("/hello")
public String hello() {
return "hello";
}
}
在resources/templates目錄下創(chuàng)建home.html、login.html和hello.html文件。
5. 運行應用
現(xiàn)在,可以運行Spring Boot應用程序,并訪問http://localhost:8080。應用程序?qū)⒅囟ㄏ虻降卿涰撁?,輸入用戶名和密碼后,將跳轉(zhuǎn)到首頁。

這只是一個簡單的Spring Security配置,實際項目中可能需要更復雜的配置,包括數(shù)據(jù)庫認證、角色控制等。但通過這個簡單的例子,你可以了解到如何快速集成Spring Security,并建立一個基本的安全框架。
- SpringBoot啟動security后如何關(guān)閉彈出的/login頁面
- Springboot整合SpringSecurity的完整案例詳解
- SpringBoot整合新版SpringSecurity完整過程
- SpringBoot集成Swagger使用SpringSecurity控制訪問權(quán)限問題
- SpringBoot集成SpringSecurity安全框架方式
- SpringSecurity在SpringBoot中的自動裝配過程
- Springbootadmin與security沖突問題及解決
- SpringBoot整合Springsecurity實現(xiàn)數(shù)據(jù)庫登錄及權(quán)限控制功能
- SpringBoot配置Spring?Security的實現(xiàn)示例
相關(guān)文章
Springboot實現(xiàn)根據(jù)用戶ID切換動態(tài)數(shù)據(jù)源
在很多具體應用場景中,我們需要用到動態(tài)數(shù)據(jù)源的情況,比如多租戶的場景,系統(tǒng)登錄時需要根據(jù)用戶信息切換到用戶對應的數(shù)據(jù)庫。這篇文章主要介紹了SpringBoot根據(jù)用戶ID實現(xiàn)切換動態(tài)數(shù)據(jù)源的示例代碼,感興趣的可以了解一下2021-12-12
SpringBoot集成MinIO實現(xiàn)大文件分片上傳的示例代碼
本文主要介紹了SpringBoot集成MinIO實現(xiàn)大文件分片上傳,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-05-05
Java按順序提取Word內(nèi)容的方法步驟(文本+數(shù)學公式)
本文分享通過Java POI庫解析Word試卷內(nèi)容,將數(shù)學公式轉(zhuǎn)換為LaTeX并保持原有順序的方法,涉及XML文本提取、節(jié)點替換及文檔重構(gòu),解決公式位置錯亂問題,實現(xiàn)非結(jié)構(gòu)化到結(jié)構(gòu)化轉(zhuǎn)換,需要的朋友可以參考下2025-09-09
SpringBoot調(diào)整ApplicationContextAware如何實現(xiàn)類加載順序
SpringBoot調(diào)整ApplicationContextAware實現(xiàn)類加載順序問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05
Spring 整合Shiro 并擴展使用EL表達式的實例詳解
Shiro是一個輕量級的權(quán)限控制框架,應用非常廣泛。本文的重點是介紹Spring整合Shiro,并通過擴展使用Spring的EL表達式。需要的朋友可以參考下2018-03-03
SpringMVC中的HandlerMappingIntrospector工具類詳解
這篇文章主要介紹了SpringMVC中的HandlerMappingIntrospector工具類詳解,這是一個Spring MVC助手類,用于集合應用所配置的HandlerMapping(url pattern和請求處理handler之間的映射)表,用于獲取針對某個請求的如下信息,需要的朋友可以參考下2023-12-12

