|
|
@@ -12,12 +12,11 @@ import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
|
|
|
import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl;
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
-import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
|
|
+import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
|
-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.web.SecurityFilterChain;
|
|
|
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
|
|
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
|
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
|
@@ -33,7 +32,7 @@ import org.springframework.security.web.context.SecurityContextPersistenceFilter
|
|
|
*/
|
|
|
@Configuration
|
|
|
@EnableWebSecurity
|
|
|
-public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
+public class WebSecurityConfig {
|
|
|
private final String loginPage = "/login";
|
|
|
private final String loginApi = "/api/auth/signin";
|
|
|
private final String logoutApi = "/api/auth/signout";
|
|
|
@@ -60,6 +59,33 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
this.logoutSuccessHandler = logoutSuccessHandler;
|
|
|
}
|
|
|
|
|
|
+ @Bean
|
|
|
+ public SecurityFilterChain filterChain(HttpSecurity http, AuthenticationManager authenticationManager) throws Exception {
|
|
|
+ http
|
|
|
+ .authorizeHttpRequests(request -> request
|
|
|
+ .requestMatchers("/api/account/**").authenticated()
|
|
|
+ .anyRequest().permitAll())
|
|
|
+ .addFilterAfter(new LoginRedirectFilter(), SecurityContextPersistenceFilter.class)
|
|
|
+ .addFilterBefore(accountAuthFilter(authenticationManager), UsernamePasswordAuthenticationFilter.class)
|
|
|
+ .addFilterBefore(thirdPartyAuthFilter(authenticationManager), UsernamePasswordAuthenticationFilter.class)
|
|
|
+ .securityContext().securityContextRepository(mySecurityContextRepository)
|
|
|
+ .and()
|
|
|
+ .formLogin().disable()
|
|
|
+ .logout().logoutUrl(logoutApi).addLogoutHandler(logoutHandler).logoutSuccessHandler(logoutSuccessHandler)
|
|
|
+ .and()
|
|
|
+ .exceptionHandling().authenticationEntryPoint(new MyAuthenticationEntryPoint())
|
|
|
+ .and()
|
|
|
+ .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.ALWAYS).sessionAuthenticationStrategy(new MySessionAuthenticationStrategy())
|
|
|
+ .and()
|
|
|
+ .rememberMe().disable()
|
|
|
+ .cors().disable()
|
|
|
+ .csrf().disable()
|
|
|
+ .headers().disable()
|
|
|
+ .authenticationProvider(userAuthProvider);
|
|
|
+
|
|
|
+ return http.build();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 放行静态资源
|
|
|
*
|
|
|
@@ -67,15 +93,15 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
* @return
|
|
|
* @date 2023-08-02 11:09:45
|
|
|
*/
|
|
|
- @Override
|
|
|
+ /*@Override
|
|
|
public void configure(WebSecurity web) {
|
|
|
web.ignoring()
|
|
|
.antMatchers("/dist/**")
|
|
|
.antMatchers("/classic/**")
|
|
|
.antMatchers("/swagger-ui.html", "/swagger-resources/**", "/v2/**", "/webjars/**");
|
|
|
- }
|
|
|
+ }*/
|
|
|
|
|
|
- @Override
|
|
|
+ /*@Override
|
|
|
public void configure(HttpSecurity http) throws Exception {
|
|
|
// 在 FilterSecurityInterceptor 中用于权限判定
|
|
|
http.authorizeRequests()
|
|
|
@@ -94,8 +120,8 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
|
// 配置 UsernamePasswordAuthenticationFilter, 禁用 UsernamePasswordAuthenticationFilter
|
|
|
http.formLogin().disable();
|
|
|
- /*.loginPage(loginPage)
|
|
|
- .loginProcessingUrl(loginApi);*/
|
|
|
+ *//*.loginPage(loginPage)
|
|
|
+ .loginProcessingUrl(loginApi);*//*
|
|
|
|
|
|
// 配置 LogoutFilter
|
|
|
http.logout()
|
|
|
@@ -114,13 +140,13 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
.sessionAuthenticationStrategy(new MySessionAuthenticationStrategy());
|
|
|
// spring security 中禁用 session
|
|
|
//.sessionCreationPolicy(SessionCreationPolicy.STATELESS);
|
|
|
- /*.maximumSessions(1)
|
|
|
- .expiredUrl(loginPage);*/
|
|
|
+ *//*.maximumSessions(1)
|
|
|
+ .expiredUrl(loginPage);*//*
|
|
|
|
|
|
// 配置 RememberMeAuthenticationFilter, 禁用 RememberMeAuthenticationFilter
|
|
|
http.rememberMe().disable();
|
|
|
- /*.key("DExNzAyNTQ2Nzo3NDI3MTNhYmM5MGE5")
|
|
|
- .rememberMeParameter("rememberMe");*/
|
|
|
+ *//*.key("DExNzAyNTQ2Nzo3NDI3MTNhYmM5MGE5")
|
|
|
+ .rememberMeParameter("rememberMe");*//*
|
|
|
|
|
|
// 配置 CorsFilter, 禁用 CorsFilter
|
|
|
http.cors().disable();
|
|
|
@@ -130,7 +156,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
|
|
|
// 配置 HeaderWriterFilter, 禁用 HeaderWriterFilter
|
|
|
http.headers().disable();
|
|
|
- }
|
|
|
+ }*/
|
|
|
|
|
|
/**
|
|
|
* 配置认证管理器
|
|
|
@@ -139,10 +165,14 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
* @return
|
|
|
* @date 2021-07-25 下午2:28
|
|
|
*/
|
|
|
- @Bean
|
|
|
+ /*@Bean
|
|
|
@Override
|
|
|
public AuthenticationManager authenticationManagerBean() throws Exception {
|
|
|
return super.authenticationManagerBean();
|
|
|
+ }*/
|
|
|
+ @Bean
|
|
|
+ public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception {
|
|
|
+ return authenticationConfiguration.getAuthenticationManager();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -152,10 +182,10 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
* @return
|
|
|
* @date 2021-07-25 下午2:31
|
|
|
*/
|
|
|
- @Override
|
|
|
+ /*@Override
|
|
|
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
|
|
auth.authenticationProvider(userAuthProvider);
|
|
|
- }
|
|
|
+ }*/
|
|
|
|
|
|
/**
|
|
|
* 配置账号密码登入 filter
|
|
|
@@ -164,9 +194,9 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
* @return
|
|
|
* @date 2022-07-06 上午9:54
|
|
|
*/
|
|
|
- private AccountAuthFilter accountAuthFilter() throws Exception {
|
|
|
+ private AccountAuthFilter accountAuthFilter(AuthenticationManager authenticationManager) throws Exception {
|
|
|
AccountAuthFilter filter = new AccountAuthFilter(loginApi, "POST", accountLoginService);
|
|
|
- filter.setAuthenticationManager(super.authenticationManager());
|
|
|
+ filter.setAuthenticationManager(authenticationManager);
|
|
|
filter.setAuthenticationSuccessHandler(successHandler);
|
|
|
filter.setAuthenticationFailureHandler(failureHandler);
|
|
|
return filter;
|
|
|
@@ -179,9 +209,9 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
* @return
|
|
|
* @date 2023-07-31 16:14:14
|
|
|
*/
|
|
|
- private ThirdPartyAuthFilter thirdPartyAuthFilter() throws Exception {
|
|
|
+ private ThirdPartyAuthFilter thirdPartyAuthFilter(AuthenticationManager authenticationManager) throws Exception {
|
|
|
ThirdPartyAuthFilter filter = new ThirdPartyAuthFilter("/oauth/redirect/**", "GET");
|
|
|
- filter.setAuthenticationManager(super.authenticationManager());
|
|
|
+ filter.setAuthenticationManager(authenticationManager);
|
|
|
filter.setAuthenticationSuccessHandler(successHandler);
|
|
|
filter.setAuthenticationFailureHandler(failureHandler);
|
|
|
return filter;
|