自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring Boot Security防重登錄及在線總數(shù)

開發(fā) 前端
本篇給大家介紹Spring Boot Security防重登錄及在線總數(shù)的相關(guān)知識,希望對你有所幫助!

[[395788]]

 環(huán)境:Spring Boot 2.2.11.RELEASE + JPA2

Security流程處理

Security的核心是Filter,下圖是Security的執(zhí)行流程

詳細步驟:

1.1

UsernamePasswordAuthenticationFilter的父類是AbstractAuthenticationProcessingFilter首先執(zhí)行父類中的doFilter方法。

1.2 執(zhí)行

UsernamePasswordAuthenticationFilter中的attemptAuthentication方法

這里實例化

UsernamePasswordAuthenticationToken對象存入用戶名及密碼進行接下來的驗證

1.3 進入驗證

this.getAuthenticationManager().authenticate(authRequest) 這里使用的是系統(tǒng)提供的ProviderManager對象進行驗證

關(guān)鍵是下面的這個for循環(huán)

 

這里先判斷AuthenticationProvider是否被支持

  1. Class<? extends Authentication> toTest = authentication.getClass(); 

這里的toTest就是

UsernamePasswordAuthenticationFilter類中調(diào)用的如下對象

1.4 既然要驗證用戶名密碼,那我們肯定地提供一個AuthenticationProvider對象同時必須還得要支持

UsernamePasswordAuthenticationToken對象類型的。所以我們提供如下一個DaoAuthenticationProvider子類,查看該類


關(guān)鍵在這個父類中,該父類中如下方法:

  1. public boolean supports(Class<?> authentication) { 
  2.         return (UsernamePasswordAuthenticationToken.class 
  3.                 .isAssignableFrom(authentication)); 
  4.     } 

 也就說明我們只需要提供DaoAuthenticationProvider一個子類就能對用戶進行驗證了。

1.5 自定義DaoAuthenticationProvider子類

  1. @Bean 
  2.     public DaoAuthenticationProvider daoAuthenticationProvider() { 
  3.         DaoAuthenticationProvider daoAuthen = new DaoAuthenticationProvider() ; 
  4.         daoAuthen.setPasswordEncoder(passwordEncoder()); 
  5.         daoAuthen.setUserDetailsService(userDetailsService()); 
  6.         daoAuthen.setHideUserNotFoundExceptions(false) ; 
  7.         return daoAuthen ; 
  8.     } 

 1.6 執(zhí)行前面for中的如下代碼

  1. result = provider.authenticate(authentication); 

這里進入了DaoAuthenticationProvider的父類

AbstractUserDetailsAuthenticationProvider中的authenticate方法

該方法的核心方法

retrieveUser方法在子類DaoAuthenticationProvider中實現(xiàn)

如果這里返回了UserDetails(查詢到用戶)將進入下一步

1.7 進入密碼的驗證

這里調(diào)用子類DaoAuthenticationProvider的方法

剩下的就是成功后的事件處理,如果有異常進行統(tǒng)一的異常處理

Security登錄授權(quán)認證

  • 實體類
  1. @Entity 
  2. @Table(name = "T_USERS"
  3. public class Users implements UserDetails, Serializable { 
  4.   private static final long serialVersionUID = 1L; 
  5.     @Id 
  6.   @GeneratedValue(generator = "system-uuid"
  7.   @GenericGenerator(name = "system-uuid", strategy = "uuid"
  8.   private String id ; 
  9.   private String username ; 
  10.   private String password ; 
  •  DAO
  1. public interface UsersRepository extends JpaRepository<Users, String>, JpaSpecificationExecutor<Users> { 
  2.     Users findByUsernameAndPassword(String username, String password) ; 
  3.     Users findByUsername(String username) ; 
  •  Security 配置
  1. @Configuration 
  2. public class SecurityConfig extends WebSecurityConfigurerAdapter { 
  3.      
  4.     @Resource 
  5.     private UsersRepository ur ; 
  6.     @Resource 
  7.     private LogoutSuccessHandler logoutSuccessHandler ; 
  8.      
  9.     @Bean 
  10.     public UserDetailsService userDetailsService() { 
  11.         return username -> { 
  12.             Users user = ur.findByUsername(username) ; 
  13.             if (user == null) { 
  14.                 throw new UsernameNotFoundException("用戶名不存在") ; 
  15.             } 
  16.             return user ; 
  17.         }; 
  18.     } 
  19.      
  20.     @Bean 
  21.     public PasswordEncoder passwordEncoder() { 
  22.         return new PasswordEncoder() { 
  23.             @Override 
  24.             public boolean matches(CharSequence rawPassword, String encodedPassword) { 
  25.                 return rawPassword.equals(encodedPassword) ; 
  26.             } 
  27.             @Override 
  28.             public String encode(CharSequence rawPassword) { 
  29.                 return rawPassword.toString() ; 
  30.             } 
  31.         }; 
  32.     } 
  33.      
  34.     @Bean 
  35.     public DaoAuthenticationProvider daoAuthenticationProvider() { 
  36.         DaoAuthenticationProvider daoAuthen = new DaoAuthenticationProvider() ; 
  37.         daoAuthen.setPasswordEncoder(passwordEncoder()); 
  38.         daoAuthen.setUserDetailsService(userDetailsService()); 
  39.         daoAuthen.setHideUserNotFoundExceptions(false) ; 
  40.         return daoAuthen ; 
  41.     } 
  42.      
  43.     @Bean 
  44.     public SessionRegistry sessionRegistry() { 
  45.         return new SessionRegistryImpl() ; 
  46.     } 
  47.      
  48.     // 這個不配置sessionRegistry中的session不失效 
  49.     @Bean 
  50.     public HttpSessionEventPublisher httpSessionEventPublisher() { 
  51.         return new HttpSessionEventPublisher(); 
  52.     } 
  53.      
  54.     @Override 
  55.     protected void configure(HttpSecurity http) throws Exception { 
  56.         http 
  57.             .csrf().disable() 
  58.             .authorizeRequests() 
  59.             .antMatchers("/pos/**"
  60.             .authenticated() 
  61.         .and() 
  62.             .formLogin() 
  63.             .loginPage("/sign/login"
  64.         .and() 
  65.             .logout() 
  66.             .logoutSuccessHandler(logoutSuccessHandler) 
  67.             .logoutUrl("/sign/logout"); 
  68.     // 這里配置最大同用戶登錄個數(shù) 
  69.         http.sessionManagement().maximumSessions(1).expiredUrl("/sign/login?expired").sessionRegistry(sessionRegistry()) ; 
  70.     } 
  71.      
  •  Controller相關(guān)接口
  1. @Controller 
  2. public class LoginController { 
  3.      
  4.     @RequestMapping("/sign/login"
  5.     public String login() { 
  6.         return "login" ; 
  7.     } 
  8.      
  9. @RestController 
  10. @RequestMapping("/sign"
  11. public class LogoutController { 
  12.      
  13.     @GetMapping("/logout"
  14.     public Object logout(HttpServletRequest request) { 
  15.         HttpSession session = request.getSession(false); 
  16.         if (session != null) { 
  17.             session.invalidate(); 
  18.         } 
  19.         SecurityContext context = SecurityContextHolder.getContext(); 
  20.         context.setAuthentication(null); 
  21.         SecurityContextHolder.clearContext(); 
  22.         return "success" ; 
  23.     } 
  24.      
  25. @RestController 
  26. @RequestMapping("/pos"
  27. public class PosController { 
  28.      
  29.     @GetMapping(""
  30.     public Object get() { 
  31.         return "pos success" ; 
  32.     } 
  33.      
  34. // 通過下面接口獲取在線人數(shù) 
  35. @RestController 
  36. @RequestMapping("/sessions"
  37. public class SessionController { 
  38.      
  39.     @Resource 
  40.     private SessionRegistry sessionRegistry ; 
  41.      
  42.     @GetMapping(""
  43.     public Object list() { 
  44.         return sessionRegistry.getAllPrincipals() ; 
  45.     } 
  46.      

 測試:

在chrome瀏覽器用zs用戶登錄

用360瀏覽器也用zs登錄

360登錄后刷新chrome瀏覽器

登錄已經(jīng)失效了,配置的最大登錄個數(shù)也生效了。

完畢!!!

 

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2025-02-28 13:00:00

SpringBoot接口接口安全

2024-07-26 07:59:25

2021-12-28 11:13:05

安全認證 Spring Boot

2022-09-06 08:40:33

應(yīng)用系統(tǒng)登錄方式Spring

2022-11-26 00:00:02

2021-04-28 06:26:11

Spring Secu功能實現(xiàn)源碼分析

2023-12-08 12:12:21

2025-02-04 11:18:49

Spring安全應(yīng)用

2021-05-12 08:32:53

Spring Secu 自定義session

2021-03-04 11:50:48

微信Spring Secu登錄

2021-07-13 14:05:37

單點登錄頁面

2021-01-06 10:09:05

Spring Boothttps sslhttps

2021-04-23 07:33:10

SpringSecurity單元

2021-08-29 18:36:57

項目

2025-02-21 12:00:00

SpringBoot防重復(fù)提交緩存機制

2022-06-12 06:45:26

高并發(fā)防重

2022-01-26 07:01:00

開源社區(qū)項目

2021-06-29 12:27:19

Spring BootCAS 登錄

2022-02-15 07:35:12

服務(wù)器KeycloakOAuth2

2024-06-14 09:30:58

點贊
收藏

51CTO技術(shù)棧公眾號