From 1b4d9b2dd4ed07758241d1143890b3a86b3c0641 Mon Sep 17 00:00:00 2001 From: zhoushihao <zsh19950802@163.com> Date: 星期日, 28 四月 2024 14:15:49 +0800 Subject: [PATCH] 权限管理开发基本功能已完成,待前端对接 --- hangzhoumesParent/common/springsecurity/src/main/java/com/mes/common/config/TokenWebSecurityConfig.java | 64 ++++++++++++++++++++++---------- 1 files changed, 44 insertions(+), 20 deletions(-) diff --git a/hangzhoumesParent/common/springsecurity/src/main/java/com/mes/common/config/TokenWebSecurityConfig.java b/hangzhoumesParent/common/springsecurity/src/main/java/com/mes/common/config/TokenWebSecurityConfig.java index cfe6197..e5f3071 100644 --- a/hangzhoumesParent/common/springsecurity/src/main/java/com/mes/common/config/TokenWebSecurityConfig.java +++ b/hangzhoumesParent/common/springsecurity/src/main/java/com/mes/common/config/TokenWebSecurityConfig.java @@ -2,6 +2,7 @@ import com.mes.common.filter.JwtAuthenticationTokenFilter; +import com.mes.common.handler.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -13,8 +14,6 @@ import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; -import org.springframework.security.web.AuthenticationEntryPoint; -import org.springframework.security.web.access.AccessDeniedHandler; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; @Configuration @@ -23,12 +22,20 @@ public class TokenWebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter; + @Autowired + LoginFailureHandler loginFailureHandler; @Autowired - private AuthenticationEntryPoint authenticationEntryPoint; + LoginSuccessHandler loginSuccessHandler; @Autowired - private AccessDeniedHandler accessDeniedHandler; + JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint; + + @Autowired + JwtAccessDeniedHandler jwtAccessDeniedHandler; + + @Autowired + JwtLogoutSuccessHandler jwtLogoutSuccessHandler; @Bean public PasswordEncoder passwordEncoder() { @@ -40,25 +47,42 @@ */ @Override protected void configure(HttpSecurity http) throws Exception { - http - //鍏抽棴csrf - .csrf().disable() - //涓嶉�氳繃Session鑾峰彇SecurityContext - .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) + + http.cors().and().csrf().disable() + + // 鐧诲綍閰嶇疆 + .formLogin() + .successHandler(loginSuccessHandler) + .failureHandler(loginFailureHandler) + + .and() + .logout() + .logoutSuccessHandler(jwtLogoutSuccessHandler) + + // 绂佺敤session + .and() + .sessionManagement() + .sessionCreationPolicy(SessionCreationPolicy.STATELESS) + + // 閰嶇疆鎷︽埅瑙勫垯 .and() .authorizeRequests() - // 瀵逛簬鐧诲綍鎺ュ彛 鍏佽鍖垮悕璁块棶 - .antMatchers("/userinfo/login").anonymous() - // 闄や笂闈㈠鐨勬墍鏈夎姹傚叏閮ㄩ渶瑕侀壌鏉冭璇� - .anyRequest().authenticated(); - http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class); + .antMatchers("/sys/user/login").anonymous() + .antMatchers("/swagger-ui.html").permitAll() + .antMatchers("/webjars/**").permitAll() + .antMatchers("/v2/**").permitAll() + .antMatchers("/swagger-resources/**").permitAll() + .anyRequest().authenticated() - //閰嶇疆寮傚父澶勭悊鍣� - http.exceptionHandling() - .authenticationEntryPoint(authenticationEntryPoint) - .accessDeniedHandler(accessDeniedHandler); - //鍏佽璺ㄥ煙 - http.cors(); + // 寮傚父澶勭悊鍣� + .and() + .exceptionHandling() + .authenticationEntryPoint(jwtAuthenticationEntryPoint) + .accessDeniedHandler(jwtAccessDeniedHandler) + + // 閰嶇疆鑷畾涔夌殑杩囨护鍣� + .and() + .addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class); } @Bean -- Gitblit v1.8.0