最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot整合新版SpringSecurity完整過程

 更新時間:2024年02月07日 09:04:29   作者:IT·陳寒  
Spring Security是保障Spring應用程序安全的強大框架,而新版的Spring Security引入了lambda表達式來配置,使得安全配置更加簡潔、優(yōu)雅,本文將介紹如何在Spring Boot項目中整合新版Spring Security,需要的朋友可以參考下

1. 引言

Spring Security是一個用于身份驗證和授權的框架,它提供了一套全面的安全服務,可輕松集成到Spring應用程序中。新版Spring Security引入了lambda表達式的配置方式,取代了之前的繁瑣XML配置和方法調用鏈式配置,使得配置更加清晰、簡潔。

2. 項目依賴配置

首先,確保你的Spring Boot項目中包含了Spring Security的依賴。在pom.xml中添加以下依賴:

<!-- Spring Security -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

3. 使用Lambda表達式配置Spring Security

在新版Spring Security中,使用lambda表達式配置可以顯著提高配置的可讀性和可維護性。以下是一個簡單的例子,展示如何使用lambda表達式配置基本的身份驗證和授權。

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.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.security.provisioning.InMemoryUserDetailsManager;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public UserDetailsService userDetailsService() {
        UserDetails user = User.withDefaultPasswordEncoder()
                .username("user")
                .password("password")
                .roles("USER")
                .build();

        return new InMemoryUserDetailsManager(user);
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

    @Configuration
    public class WebSecurityConfig 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();
        }
    }
}

上述配置中,我們使用lambda表達式配置了一個簡單的身份驗證和授權。userDetailsService方法配置了一個內存中的用戶,configure方法配置了訪問權限和登錄頁面。

4. 自定義身份驗證邏輯

在實際項目中,我們通常需要實現(xiàn)自定義的身份驗證邏輯。通過lambda表達式,我們可以更清晰地定義自己的UserDetailsServiceAuthenticationProvider。

@Configuration
@EnableWebSecurity
public class CustomSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomUserDetailsService customUserDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authenticationProvider());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/public/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

    @Bean
    public DaoAuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
        provider.setUserDetailsService(customUserDetailsService);
        provider.setPasswordEncoder(passwordEncoder());
        return provider;
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }
}

在上述配置中,我們注入了一個自定義的UserDetailsService,并通過lambda表達式配置了AuthenticationProvider。這樣我們可以更靈活地定義用戶信息的獲取和身份驗證邏輯。

5. 認證與授權注解

新版Spring Security還引入了一系列基于注解的認證與授權。通過lambda表達式,我們可以更直觀地配置這些注解。

5.1 @Secured注解

@Configuration
@EnableWebSecurity
public class SecuredSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/public/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

    @Secured("ROLE_ADMIN")
    @GetMapping("/admin")
    public String adminPage() {
        return "admin";
    }
}

在上述代碼中,通過@Secured("ROLE_ADMIN")注解配置了訪問路徑/admin需要具備ROLE_ADMIN角色。

5.2 @PreAuthorize和@PostAuthorize注解

@Configuration
@EnableWebSecurity
public class PrePostSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/public/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }

    @PreAuthorize("hasRole('ADMIN')")
    @GetMapping("/admin")
    public String adminPage() {
        return "admin";
    }

    @PostAuthorize("hasRole('USER')")
    @GetMapping("/user")
    public String userPage() {
        return "user";
    }
}

在上述代碼中,通過@PreAuthorize@PostAuthorize注解分別配置了方法的前置和后置授權規(guī)則。

6. 總結

通過本文的介紹,我們學習了如何在Spring Boot項目中整合新版Spring Security,并通過lambda表達式進行簡潔、優(yōu)雅的安全配置。新版Spring Security的引入使得配置更加直觀,開發(fā)者可以更輕松地實現(xiàn)自定義的身份驗證邏輯和授權規(guī)則。希望通過本文的學習,讀者能夠更加熟練地使用Spring Security保障應用程序的安全性。

以上就是SpringBoot整合新版SpringSecurity完整過程的詳細內容,更多關于SpringBoot整合SpringSecurity的資料請關注腳本之家其它相關文章!

相關文章

  • SpringSecurity自定義登錄接口的實現(xiàn)

    SpringSecurity自定義登錄接口的實現(xiàn)

    本文介紹了使用Spring Security實現(xiàn)自定義登錄接口,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-01-01
  • Java中String.join()高效字符串拼接的實現(xiàn)

    Java中String.join()高效字符串拼接的實現(xiàn)

    本文主要介紹了Java中String.join()高效字符串拼接的實現(xiàn),包括基礎拼接、StreamAPI結合、非字符串類型處理及與傳統(tǒng)方式對比,具有一定的參考價值,感興趣的可以了解一下
    2025-05-05
  • Java循環(huán)對bean的屬性進行賦值的實現(xiàn)

    Java循環(huán)對bean的屬性進行賦值的實現(xiàn)

    本文主要介紹了Java循環(huán)對bean的屬性進行賦值,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • spring結合hibernate示例詳解

    spring結合hibernate示例詳解

    這篇文章主要介紹了spring結合hibernate結合,需要的朋友可以參考下
    2017-09-09
  • 深入C++ typedef的用法總結(必看)

    深入C++ typedef的用法總結(必看)

    本篇文章是對C++中typedef的用法進行了詳細的總結分析,需要的朋友參考下
    2013-05-05
  • Java實現(xiàn)高并發(fā)秒殺的七種方式

    Java實現(xiàn)高并發(fā)秒殺的七種方式

    本文主要介紹了Java實現(xiàn)高并發(fā)秒殺的六種方式,包括使用緩存、數(shù)據(jù)庫樂觀鎖、數(shù)據(jù)庫悲觀鎖、分布式鎖、隊列限流、令牌桶算法和限流器,具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • 如何解決SpringMVC不能訪問html頁面

    如何解決SpringMVC不能訪問html頁面

    這篇文章主要介紹了如何解決SpringMVC不能訪問html頁面問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • springboot使用kafka事務的示例代碼

    springboot使用kafka事務的示例代碼

    Kafka?同數(shù)據(jù)庫一樣支持事務,當發(fā)生異常的時候可以進行回滾,確保消息監(jiān)聽器不會接收到一些錯誤的或者不需要的消息,本文就來介紹一下springboot使用kafka事務的示例代碼,具有一定的參考價值,感興趣的可以了解一下
    2024-06-06
  • 基于OpenCV與JVM實現(xiàn)矩陣處理圖像

    基于OpenCV與JVM實現(xiàn)矩陣處理圖像

    本文主要介紹了Java圖像處理實戰(zhàn)之基于OpenCV與JVM實現(xiàn)矩陣處理圖像。文中的示例代碼講解詳細,對我們學習圖像處理有一定的幫助,感興趣的可以試一試
    2022-01-01
  • Swagger中@API?tags中含有中文異常問題的解決

    Swagger中@API?tags中含有中文異常問題的解決

    這篇文章主要介紹了Swagger中@API?tags中含有中文異常問題的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教。
    2022-01-01

最新評論

徐闻县| 大方县| 泌阳县| 慈溪市| 北碚区| 连州市| 石嘴山市| 江安县| 林周县| 龙陵县| 岢岚县| 上饶县| 哈尔滨市| 桓台县| 巴彦县| 综艺| 石柱| 兰州市| 泰顺县| 伊通| 长顺县| 郧西县| 耒阳市| 滕州市| 千阳县| 泸定县| 拜泉县| 韶山市| 彰化市| 色达县| 阿拉善盟| 陕西省| 永胜县| 兴城市| 阿坝| 理塘县| 扎兰屯市| 鲁甸县| 甘孜| 内丘县| 临朐县|