From 95688e400b6974953505c159f8fbf9d65784c724 Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: 星期一, 13 三月 2023 09:45:00 +0800 Subject: [PATCH] 初步实现登录返回token --- src/main/java/com/genersoft/iot/vmp/conf/security/WebSecurityConfig.java | 95 +++++++++++++++++++++++++++-------------------- 1 files changed, 55 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/conf/security/WebSecurityConfig.java b/src/main/java/com/genersoft/iot/vmp/conf/security/WebSecurityConfig.java index cce0d11..c700b8c 100644 --- a/src/main/java/com/genersoft/iot/vmp/conf/security/WebSecurityConfig.java +++ b/src/main/java/com/genersoft/iot/vmp/conf/security/WebSecurityConfig.java @@ -15,7 +15,9 @@ import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.config.http.SessionCreationPolicy; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import java.util.List; @@ -56,22 +58,14 @@ */ @Autowired private AnonymousAuthenticationEntryPoint anonymousAuthenticationEntryPoint; -// /** -// * 瓒呮椂澶勭悊 -// */ -// @Autowired -// private InvalidSessionHandler invalidSessionHandler; + @Autowired + private JwtAuthenticationFilter jwtAuthenticationFilter; -// /** -// * 椤跺彿澶勭悊 -// */ -// @Autowired -// private SessionInformationExpiredHandler sessionInformationExpiredHandler; -// /** -// * 鐧诲綍鐢ㄦ埛娌℃湁鏉冮檺璁块棶璧勬簮 -// */ -// @Autowired -// private LoginUserAccessDeniedHandler accessDeniedHandler; +// @Bean +// JwtAuthenticationFilter jwtAuthenticationFilter() throws Exception { +// JwtAuthenticationFilter jwtAuthenticationFilter = new JwtAuthenticationFilter(authenticationManager()); +// return jwtAuthenticationFilter; +// } /** @@ -126,35 +120,56 @@ @Override protected void configure(HttpSecurity http) throws Exception { - http.cors().and().csrf().disable(); - // 璁剧疆鍏佽娣诲姞闈欐�佹枃浠� - http.headers().contentTypeOptions().disable(); - http.authorizeRequests() - // 鏀捐鎺ュ彛 + http.headers().contentTypeOptions().disable() + .and().cors() + .and().csrf().disable() + .sessionManagement() + .sessionCreationPolicy(SessionCreationPolicy.STATELESS) + + // 閰嶇疆鎷︽埅瑙勫垯 + .and() + .authorizeRequests() .antMatchers("/api/user/login","/index/hook/**").permitAll() - // 闄や笂闈㈠鐨勬墍鏈夎姹傚叏閮ㄩ渶瑕侀壌鏉冭璇� .anyRequest().authenticated() - // 寮傚父澶勭悊(鏉冮檺鎷掔粷銆佺櫥褰曞け鏁堢瓑) - .and().exceptionHandling() - //鍖垮悕鐢ㄦ埛璁块棶鏃犳潈闄愯祫婧愭椂鐨勫紓甯稿鐞� + // 寮傚父澶勭悊鍣� + .and() + .exceptionHandling() .authenticationEntryPoint(anonymousAuthenticationEntryPoint) -// .accessDeniedHandler(accessDeniedHandler)//鐧诲綍鐢ㄦ埛娌℃湁鏉冮檺璁块棶璧勬簮 - // 鐧诲叆 鍏佽鎵�鏈夌敤鎴� - .and().formLogin().permitAll() - //鐧诲綍鎴愬姛澶勭悊閫昏緫 - .successHandler(loginSuccessHandler) - //鐧诲綍澶辫触澶勭悊閫昏緫 - .failureHandler(loginFailureHandler) - // 鐧诲嚭 - .and().logout().logoutUrl("/api/user/logout").permitAll() - //鐧诲嚭鎴愬姛澶勭悊閫昏緫 - .logoutSuccessHandler(logoutHandler) - .deleteCookies("JSESSIONID") - // 浼氳瘽绠$悊 -// .and().sessionManagement().invalidSessionStrategy(invalidSessionHandler) // 瓒呮椂澶勭悊 -// .maximumSessions(1)//鍚屼竴璐﹀彿鍚屾椂鐧诲綍鏈�澶х敤鎴锋暟 -// .expiredSessionStrategy(sessionInformationExpiredHandler) // 椤跺彿澶勭悊 +// .accessDeniedHandler(jwtAccessDeniedHandler) + // 閰嶇疆鑷畾涔夌殑杩囨护鍣� +// .and() +// .addFilter(jwtAuthenticationFilter) + // 楠岃瘉鐮佽繃婊ゅ櫒鏀惧湪UsernamePassword杩囨护鍣ㄤ箣鍓� +// .addFilterBefore(captchaFilter, UsernamePasswordAuthenticationFilter.class) ; + http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class); +// // 璁剧疆鍏佽娣诲姞闈欐�佹枃浠� +// http.headers().contentTypeOptions().disable(); +// http.authorizeRequests() +// // 鏀捐鎺ュ彛 +// .antMatchers("/api/user/login","/index/hook/**").permitAll() +// // 闄や笂闈㈠鐨勬墍鏈夎姹傚叏閮ㄩ渶瑕侀壌鏉冭璇� +// .anyRequest().authenticated() +// // 绂佺敤session +// .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) +// // 寮傚父澶勭悊(鏉冮檺鎷掔粷銆佺櫥褰曞け鏁堢瓑) +// .and().exceptionHandling() +// // 鍖垮悕鐢ㄦ埛璁块棶鏃犳潈闄愯祫婧愭椂鐨勫紓甯稿鐞� +// .authenticationEntryPoint(anonymousAuthenticationEntryPoint) +// // 鐧诲綍 鍏佽鎵�鏈夌敤鎴� +// .and().formLogin() +// // 鐧诲綍鎴愬姛澶勭悊閫昏緫 鍦ㄨ繖閲岀粰鍑篔WT +// .successHandler(loginSuccessHandler) +// // 鐧诲綍澶辫触澶勭悊閫昏緫 +// .failureHandler(loginFailureHandler) +// // 鐧诲嚭 +// .and().logout().logoutUrl("/api/user/logout").permitAll() +// // 鐧诲嚭鎴愬姛澶勭悊閫昏緫 +// .logoutSuccessHandler(logoutHandler) +// // 閰嶇疆鑷畾涔夌殑杩囨护鍣� +// .and() +// .addFilter(jwtAuthenticationFilter()) +// ; } -- Gitblit v1.8.0