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

Spring Security實現(xiàn)身份認(rèn)證和授權(quán)的示例代碼

 更新時間:2023年06月16日 16:07:04   作者:2013crazy  
在 Spring Boot 應(yīng)用中使用 Spring Security 可以非常方便地實現(xiàn)用戶身份認(rèn)證和授權(quán),本文主要介紹了Spring Security實現(xiàn)身份認(rèn)證和授權(quán)的示例代碼,感興趣的可以了解一下

Spring Security 是一個開源的安全框架,提供了基于權(quán)限的訪問控制、身份認(rèn)證、安全性事件發(fā)布等功能。在 Spring Boot 應(yīng)用中使用 Spring Security 可以非常方便地實現(xiàn)用戶身份認(rèn)證和授權(quán)。

Spring Security 實現(xiàn)身份認(rèn)證的主要方式是使用認(rèn)證過濾器鏈,該過濾器鏈包含多個過濾器,用于對用戶進(jìn)行身份驗證和授權(quán)。在 Spring Security 中,認(rèn)證和授權(quán)處理是通過過濾器鏈中的過濾器來實現(xiàn)的,最終返回一個認(rèn)證成功的用戶對象。本文將介紹 Spring Security 如何實現(xiàn)身份認(rèn)證和授權(quán),并提供示例代碼。

1. Spring Security 的身份認(rèn)證

Spring Security 的身份認(rèn)證是通過 AuthenticationManager 接口實現(xiàn)的。AuthenticationManager 接口是一個認(rèn)證管理器,用于對用戶進(jìn)行身份驗證。在 Spring Security 中,AuthenticationManager 接口的默認(rèn)實現(xiàn)是 ProviderManager。

ProviderManager 是一個認(rèn)證管理器,它包含一個或多個 AuthenticationProvider 實現(xiàn),用于對用戶進(jìn)行身份驗證。AuthenticationProvider 接口是一個認(rèn)證提供者,用于驗證用戶身份。在 Spring Security 中,AuthenticationProvider 的默認(rèn)實現(xiàn)是 DaoAuthenticationProvider。

DaoAuthenticationProvider 是一個認(rèn)證提供者,用于對用戶進(jìn)行身份驗證。它需要一個 UserDetailsService 實現(xiàn)來獲取用戶信息和密碼,然后使用 PasswordEncoder 進(jìn)行密碼校驗。UserDetailsService 接口是一個用戶詳細(xì)信息服務(wù)接口,用于獲取用戶信息和密碼。PasswordEncoder 接口是一個密碼編碼器接口,用于對密碼進(jìn)行編碼和解碼。

下面是一個基本的 Spring Security 配置示例,用于實現(xiàn)身份認(rèn)證:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
? ? @Autowired
? ? private UserDetailsService userDetailsService;
? ? @Autowired
? ? private PasswordEncoder passwordEncoder;
? ? @Override
? ? protected void configure(HttpSecurity http) throws Exception {
? ? ? ? http.authorizeRequests()
? ? ? ? ? ? .antMatchers("/admin/**").hasRole("ADMIN")
? ? ? ? ? ? .antMatchers("/user/**").hasRole("USER")
? ? ? ? ? ? .anyRequest().authenticated()
? ? ? ? ? ? .and()
? ? ? ? ? ? .formLogin()
? ? ? ? ? ? .and()
? ? ? ? ? ? .logout()
? ? ? ? ? ? .and()
? ? ? ? ? ? .csrf().disable();
? ? }
? ? @Override
? ? protected void configure(AuthenticationManagerBuilder auth) throws Exception {
? ? ? ? auth.userDetailsService(userDetailsService)
? ? ? ? ? ? .passwordEncoder(passwordEncoder);
? ? }
? }

在上面的代碼中,使用 @EnableWebSecurity 注解啟用 Spring Security。configure(HttpSecurity http) 方法用于配置訪問控制,指定哪些 URL 需要哪些角色才能訪問,以及任何請求都需要經(jīng)過身份驗證。formLogin() 方法啟用基于表單的身份驗證,logout() 方法啟用注銷支持,csrf().disable() 方法禁用 CSRF 保護(hù)。

configure(AuthenticationManagerBuilder auth) 方法用于配置身份驗證,指定使用哪個 UserDetailsService 實現(xiàn)來獲取用戶信息和密碼,以及使用哪個 PasswordEncoder 實現(xiàn)進(jìn)行密碼校驗。

2. Spring Security 的授權(quán)

Spring Security 的授權(quán)是通過 AccessDecisionManager 接口實現(xiàn)的。AccessDecisionManager 接口是一個訪問決策管理器,用于決定用戶是否有權(quán)限訪問某個資源。在 Spring Security 中,AccessDecisionManager 接口的默認(rèn)實現(xiàn)是 AffirmativeBased。

AffirmativeBased 是一個訪問決策管理器,它包含一個或多個 AccessDecisionVoter 實現(xiàn),用于決定用戶是否有權(quán)限訪問某個資源。AccessDecisionVoter 接口是一個投票者,用于決定用戶是否有權(quán)限訪問某個資源。在 Spring Security 中,AccessDecisionVoter 的默認(rèn)實現(xiàn)是 RoleVoter。

RoleVoter 是一個投票者,用于根據(jù)用戶的角色決定用戶是否有權(quán)限訪問某個資源。在 Spring Security 中,我們可以通過實現(xiàn) AccessDecisionVoter 接口來自定義投票者,根據(jù)自己的需求來決定用戶是否有權(quán)限訪問某個資源。

下面是一個基本的 Spring Security 配置示例,用于實現(xiàn)授權(quán):

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
? ? @Autowired
? ? private UserDetailsService userDetailsService;
? ? @Autowired
? ? private PasswordEncoder passwordEncoder;
? ? @Override
? ? protected void configure(HttpSecurity http) throws Exception {
? ? ? ? http.authorizeRequests()
? ? ? ? ? ? .antMatchers("/admin/**").hasRole("ADMIN")
? ? ? ? ? ? .antMatchers("/user/**").hasRole("USER")
? ? ? ? ? ? .anyRequest().authenticated()
? ? ? ? ? ? .and()
? ? ? ? ? ? .formLogin()
? ? ? ? ? ? .and()
? ? ? ? ? ? .logout()
? ? ? ? ? ? .and()
? ? ? ? ? ? .csrf().disable();
? ? }
? ? @Override
? ? protected void configure(AuthenticationManagerBuilder auth) throws Exception {
? ? ? ? auth.userDetailsService(userDetailsService)
? ? ? ? ? ? .passwordEncoder(passwordEncoder);
? ? }

@Bean在上面的代碼中,使用 @Bean 注解創(chuàng)建了一個自定義的 AccessDecisionVoter 實例,用于自定義投票邏輯。在 configure(HttpSecurity http) 方法中,通過 accessDecisionManager() 方法將自定義的 AccessDecisionVoter 實例添加到訪問決策管理器中。

3. 完整的示例代碼

下面是一個完整的 Spring Security 配置示例代碼,用于實現(xiàn)身份認(rèn)證和授權(quán):

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
? ? @Autowired
? ? private UserDetailsService userDetailsService;
? ? @Autowired
? ? private PasswordEncoder passwordEncoder;
? ? @Override
? ? protected void configure(HttpSecurity http) throws Exception {
? ? ? ? http.authorizeRequests()
? ? ? ? ? ? .antMatchers("/admin/**").hasRole("ADMIN")
? ? ? ? ? ? .antMatchers("/user/**").hasRole("USER")
? ? ? ? ? ? .anyRequest().authenticated()
? ? ? ? ? ? .and()
? ? ? ? ? ? .formLogin()
? ? ? ? ? ? .and()
? ? ? ? ? ? .logout()
? ? ? ? ? ? .and()
? ? ? ? ? ? .csrf().disable()
? ? ? ? ? ? .exceptionHandling()
? ? ? ? ? ? .accessDeniedPage("/403");
? ? }
? ? @Override
? ? protected void configure(AuthenticationManagerBuilder auth) throws Exception {
? ? ? ? auth.userDetailsService(userDetailsService)
? ? ? ? ? ? .passwordEncoder(passwordEncoder);
? ? }
? ? @Bean
? ? public AccessDecisionVoter<Object> accessDecisionVoter(){
? ? ? ? RoleHierarchyVoter roleHierarchyVoter = new RoleHierarchyVoter(roleHierarchy());
? ? ? ? return roleHierarchyVoter;
? ? }
? ? @Bean
? ? public RoleHierarchyImpl roleHierarchy() {
? ? ? ? RoleHierarchyImpl roleHierarchy = new RoleHierarchyImpl();
? ? ? ? roleHierarchy.setHierarchy("ROLE_ADMIN > ROLE_USER");
? ? ? ? return roleHierarchy;
? ? }
? ? @Bean
? ? public PasswordEncoder passwordEncoder() {
? ? ? ? return new BCryptPasswordEncoder();
? ? }
}

在上面的代碼中,使用 @EnableWebSecurity 注解啟用 Spring Security。configure(HttpSecurity http) 方法用于配置訪問控制,指定哪些 URL 需要哪些角色才能訪問,以及任何請求都需要經(jīng)過身份驗證。formLogin() 方法啟用基于表單的身份驗證,logout() 方法啟用注銷支持,csrf().disable() 方法禁用 CSRF 保護(hù),并且使用 accessDeniedPage() 方法指定訪問被拒絕時跳轉(zhuǎn)的頁面。

configure(AuthenticationManagerBuilder auth) 方法用于配置身份驗證,指定使用哪個 UserDetailsService 實現(xiàn)來獲取用戶信息和密碼,以及使用哪個 PasswordEncoder 實現(xiàn)進(jìn)行密碼校驗。

accessDecisionVoter() 方法創(chuàng)建了一個自定義的 AccessDecisionVoter 實例,用于自定義投票邏輯。在這個例子中,我們使用了 RoleHierarchyVoter 類實現(xiàn)了一個基于角色繼承關(guān)系的投票邏輯。RoleHierarchyImpl 類用于定義角色繼承關(guān)系。

passwordEncoder() 方法用于創(chuàng)建一個密碼編碼器實例,這里我們使用了 BCryptPasswordEncoder 類實現(xiàn)密碼編碼。

最后,我們需要實現(xiàn) UserDetailsService 接口,用于獲取用戶信息和密碼。下面是一個簡單的實現(xiàn)示例:

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
? ? @Autowired
? ? private UserRepository userRepository;
? ? @Override
? ? public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
? ? ? ? User user = userRepository.findByUsername(username)
? ? ? ? ? ? ? ? .orElseThrow(() -> new UsernameNotFoundException("User not found with username: " + username));
? ? ? ? return new org.springframework.security.core.userdetails.User(
? ? ? ? ? ? ? ? user.getUsername(),
? ? ? ? ? ? ? ? user.getPassword(),
? ? ? ? ? ? ? ? user.getRoles().stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList()));
? ? }
}

在上面的代碼中,我們使用 UserRepository 類獲取用戶信息和密碼,并將其包裝成一個 UserDetails 實例返回。在這個例子中,我們使用了 org.springframework.security.core.userdetails.User 類實現(xiàn)了 UserDetails 接口。

結(jié)語

Spring Security 是一個非常強(qiáng)大的安全框架,可以為 Spring Boot 應(yīng)用提供完整的身份認(rèn)證和授權(quán)功能。本文介紹了 Spring Security 如何實現(xiàn)身份認(rèn)證和授權(quán),并提供了示例代碼。使用 Spring Security 可以非常方便地保護(hù)應(yīng)用程序,防止惡意攻擊和數(shù)據(jù)泄露。

到此這篇關(guān)于Spring Security實現(xiàn)身份認(rèn)證和授權(quán)的示例代碼的文章就介紹到這了,更多相關(guān)Spring Security身份認(rèn)證和授權(quán)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java 漢諾塔詳解及實現(xiàn)代碼

    java 漢諾塔詳解及實現(xiàn)代碼

    這篇文章主要介紹了java 漢諾塔詳解及實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Spring Boot集成tablesaw插件快速入門示例代碼

    Spring Boot集成tablesaw插件快速入門示例代碼

    Tablesaw是一款Java的數(shù)據(jù)可視化庫,數(shù)據(jù)解析庫,主要用于加載數(shù)據(jù),對數(shù)據(jù)進(jìn)行操作(轉(zhuǎn)化,過濾,匯總等),類比Python中的Pandas庫,本文介紹Spring Boot集成tablesaw插件快速入門Demo,感興趣的朋友一起看看吧
    2024-06-06
  • java對數(shù)組進(jìn)行排序的方法

    java對數(shù)組進(jìn)行排序的方法

    這篇文章主要介紹了java對數(shù)組進(jìn)行排序的方法,涉及java數(shù)組排序的技巧,需要的朋友可以參考下
    2015-03-03
  • java優(yōu)先隊列PriorityQueue中Comparator的用法詳解

    java優(yōu)先隊列PriorityQueue中Comparator的用法詳解

    這篇文章主要介紹了java優(yōu)先隊列PriorityQueue中Comparator的用法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • Java中的多線程一定就快嗎?

    Java中的多線程一定就快嗎?

    這篇文章主要介紹了Java 多線程的相關(guān)資料,幫助大家是否選擇開啟多線程,感興趣的朋友可以了解下
    2020-09-09
  • 通過實例了解Java Integer類和int的區(qū)別

    通過實例了解Java Integer類和int的區(qū)別

    這篇文章主要介紹了通過實例了解Java Integer類和int的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • 詳解領(lǐng)域驅(qū)動設(shè)計之事件驅(qū)動與CQRS

    詳解領(lǐng)域驅(qū)動設(shè)計之事件驅(qū)動與CQRS

    這篇文章分析了如何應(yīng)用事件來分離軟件核心復(fù)雜度。探究CQRS為什么廣泛應(yīng)用于DDD項目中,以及如何落地實現(xiàn)CQRS框架。當(dāng)然我們也要警惕一些失敗的教訓(xùn),利弊分析以后再去抉擇正確的應(yīng)對之道
    2021-06-06
  • Java21之虛擬線程用法實踐指南及常見問題

    Java21之虛擬線程用法實踐指南及常見問題

    虛擬線程是Java19中作為預(yù)覽功能提出,21中正式完成的輕量級線程,旨在減少編寫易于觀察的高吞吐量的并發(fā)程序的工作量,這篇文章主要介紹了Java21之虛擬線程用法的相關(guān)資料,需要的朋友可以參考下
    2026-01-01
  • Java 線程死鎖的問題解決辦法

    Java 線程死鎖的問題解決辦法

    這篇文章主要介紹了 Java 線程死鎖的問題解決辦法的相關(guān)資料,希望通過本大家能幫助到大家,遇到類似問題能夠解決,需要的朋友可以參考下
    2017-09-09
  • springboot集成mybatisplus的方法

    springboot集成mybatisplus的方法

    這篇文章主要為大家詳細(xì)介紹了springboot集成mybatisplus的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-04-04

最新評論

龙游县| 阳原县| 长葛市| 阿拉善右旗| 乌拉特后旗| 阿勒泰市| 仁布县| 上犹县| 盐源县| 肃北| 淳安县| 仲巴县| 汝城县| 通海县| 海安县| 开化县| 无为县| 淮阳县| 永州市| 舟山市| 鸡西市| 南乐县| 北京市| 崇阳县| 南京市| 林州市| 乌鲁木齐县| 六盘水市| 雷波县| 瑞昌市| 广丰县| 长汀县| 法库县| 芷江| 边坝县| 元谋县| 深水埗区| 新营市| 松潘县| 页游| 武义县|