iT邦幫忙

2026 iThome 鐵人賽

DAY 15
0
自我挑戰組

Java Spring專案的兩大元件 - Security、Schedule系列 第 15

Day15 : Spring Security - 探討AuthenticationManager與實作UserDetails

  • 分享至 

  • xImage
  •  

Spring Security - 探討AuthenticationManager與實作UserDetails

  • 完善Security Config ⭐⭐⭐⭐⭐⭐
    • 首先先把 PasswordEncoder 以及 AuthenticationManager 作為Bean丟給Spring容器管理!
    • 而這邊就不得不提到 AuthenticatoinManager 的驗證實作流程拉~

    ProviderManager、AuthenticationProvider

    • 簡單來說,在AuthenticationManager這個介面中,最常實作的類別是 ProviderManager,也是預設使用的類別。
    • 它專門維護Provider,而Provider就是 驗證方式,由於驗證方式有很多種,如 : 「帳密登入第三方登入指紋登入...」,因此若都塞在AuthenticationManager,會變得不好維護。
    • 在ProviderManager中,其委派 AuthenticationProvider 介面做驗證處理
    public interface AuthenticationProvider {
    
        // 驗證邏輯
        Authentication authenticate(Authentication authentication) throws AuthenticationException;
    
        // 是否能處理XXX驗證方式的集合
        boolean supports(Class<?> authentication);
    }
    
    • 因此實作AuthenticationProvider的類別才是真正處理驗證邏輯的地方!
    • 而最常使用的Provider則是 DaoAuthenticationProvider
    • 以下是來自 Spring官網的流程圖 :
      image
    • 因此可以知道 DaoAuthenticationProvider 就是結合UserDetailsService以及PasswordEncoder去做驗證。
  • UserDetails、UserDetailsService實作 ⭐⭐⭐⭐
    • 簡單來說就是在原本我設計的User、UserService後面加上 implements,然後覆寫方法!
    • 比較酷的是關於User的Authorties覆寫方式 :
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (role == null) return List.of();
        return List.of(new SimpleGrantedAuthority("ROLE_" + role.getName()));
    }
    
    • 由於我本來就有設定Role類別,因此可以直接以role.getName來設計Authorities。
    • 而UserDetails的部分則是實作loadUserByUsername :
    @Override
    public UserDetails loadUserByUsername(String account) throws UsernameNotFoundException{
        return userDao.findByAccount(account).orElseThrow(() -> new UsernameNotFoundException("User not found: " + account));
    }
    
    • 這樣寫,當我們在做login驗證時,就可以使用 AuthenticationManager 在Controller做驗證啦!

本篇文章出自《每天學Java直到今年結束》Day230,大家可以到我的網站上查看~


上一篇
Day14 : Spring Security - 解析SecurityConfig
下一篇
Day16 : Spring Security - 實作JwtAuthenticationFilter
系列文
Java Spring專案的兩大元件 - Security、Schedule16
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言