|
|
@@ -11,17 +11,21 @@ import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.security.access.expression.SecurityExpressionHandler;
|
|
|
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.method.configuration.EnableGlobalMethodSecurity;
|
|
|
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.FilterInvocation;
|
|
|
import org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler;
|
|
|
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
|
|
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
|
|
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
|
|
+import org.springframework.security.web.authentication.logout.LogoutHandler;
|
|
|
+import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
|
|
|
import org.springframework.security.web.context.SecurityContextPersistenceFilter;
|
|
|
|
|
|
/**
|
|
|
@@ -34,19 +38,26 @@ import org.springframework.security.web.context.SecurityContextPersistenceFilter
|
|
|
@EnableWebSecurity
|
|
|
@EnableGlobalMethodSecurity(prePostEnabled = true) // 调用方法时检查权限
|
|
|
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
+ private final String loginPage = "/login";
|
|
|
private final String loginApi = "/login";
|
|
|
+ private final String logoutApi = "/logout";
|
|
|
|
|
|
private final AccountAuthProvider userAuthProvider;
|
|
|
private final AccountAuthService accountAuthService;
|
|
|
private final AuthenticationSuccessHandler successHandler;
|
|
|
private final AuthenticationFailureHandler failureHandler;
|
|
|
+ private final LogoutHandler logoutHandler;
|
|
|
+ private final LogoutSuccessHandler logoutSuccessHandler;
|
|
|
|
|
|
public WebSecurityConfig(AccountAuthProvider userAuthProvider, AccountAuthService accountAuthService,
|
|
|
- AuthenticationSuccessHandler successHandler, AuthenticationFailureHandler failureHandler) {
|
|
|
+ AuthenticationSuccessHandler successHandler, AuthenticationFailureHandler failureHandler,
|
|
|
+ LogoutHandler logoutHandler, LogoutSuccessHandler logoutSuccessHandler) {
|
|
|
this.userAuthProvider = userAuthProvider;
|
|
|
this.accountAuthService = accountAuthService;
|
|
|
this.successHandler = successHandler;
|
|
|
this.failureHandler = failureHandler;
|
|
|
+ this.logoutHandler = logoutHandler;
|
|
|
+ this.logoutSuccessHandler = logoutSuccessHandler;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -69,35 +80,37 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
@Override
|
|
|
protected void configure(HttpSecurity http) throws Exception {
|
|
|
http.authorizeRequests()
|
|
|
- .expressionHandler(webExpressionHandler())
|
|
|
.antMatchers("/login").permitAll()
|
|
|
- // TODO 处理 WebSocket 的认证
|
|
|
- .antMatchers("/webssh").permitAll()
|
|
|
// 放行所有以 /api 为前缀的请求
|
|
|
- .antMatchers("/api/**").permitAll()
|
|
|
+ //.antMatchers("/api/**").permitAll()
|
|
|
.anyRequest().authenticated();
|
|
|
- http.exceptionHandling().accessDeniedPage("/deny");
|
|
|
|
|
|
- // 处理 X-Frame-Options header
|
|
|
- http.headers().frameOptions().sameOrigin();
|
|
|
-
|
|
|
- // 在 UsernamePasswordAuthenticationFilter 后添加 filter
|
|
|
+ // 配置 FilterChainProxy 过滤器链
|
|
|
http.addFilterAfter(new LoginRedirectFilter(), SecurityContextPersistenceFilter.class);
|
|
|
http.addFilterBefore(accountAuthFilter(), UsernamePasswordAuthenticationFilter.class);
|
|
|
|
|
|
- // 基于表单的认证
|
|
|
- http.formLogin()
|
|
|
- // 登录页面(GET 请求)
|
|
|
- .loginPage("/login")
|
|
|
- // 登录接口(POST 请求)
|
|
|
- .loginProcessingUrl("/login")
|
|
|
- .successHandler(new AuthSuccessHandlerImpl())
|
|
|
- .failureHandler(new AuthFailHandlerImpl())
|
|
|
- .and()
|
|
|
- .logout()
|
|
|
- .logoutSuccessUrl("/")
|
|
|
- .and()
|
|
|
- .httpBasic().disable();
|
|
|
+ // 禁用 UsernamePasswordAuthenticationFilter, 使用自定义的 AccountAuthFilter
|
|
|
+ http.formLogin().disable();
|
|
|
+ /*.loginPage(loginPage)
|
|
|
+ .loginProcessingUrl(loginApi);*/
|
|
|
+
|
|
|
+ // 配置 LogoutFilter
|
|
|
+ http.logout()
|
|
|
+ .logoutUrl(logoutApi)
|
|
|
+ .addLogoutHandler(logoutHandler)
|
|
|
+ //.logoutSuccessUrl(loginPage)
|
|
|
+ .logoutSuccessHandler(logoutSuccessHandler);
|
|
|
+
|
|
|
+ // 配置 ExceptionTranslationFilter, 登录认证接口失败时的处理, 不会重定向到 loginPage
|
|
|
+ http.exceptionHandling()
|
|
|
+ .authenticationEntryPoint(new ExceptionAuthenticationEntryPoint());
|
|
|
+
|
|
|
+ // 配置 SessionManagementFilter 和 ConcurrentSessionFilter
|
|
|
+ http.sessionManagement()
|
|
|
+ .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
|
|
|
+ //.sessionAuthenticationStrategy(new MySessionAuthenticationStrategy(sessionRegistry()));
|
|
|
+ .maximumSessions(1)
|
|
|
+ .expiredUrl(loginPage);
|
|
|
|
|
|
// 配置 RememberMeAuthenticationFilter, 禁用 RememberMeAuthenticationFilter
|
|
|
http.rememberMe().disable();
|
|
|
@@ -115,14 +128,27 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 使用数据库存储
|
|
|
+ * 配置认证管理器
|
|
|
*
|
|
|
* @param
|
|
|
* @return
|
|
|
- * @date 2019-07-05 上午11:25
|
|
|
+ * @date 2021-07-25 下午2:28
|
|
|
*/
|
|
|
+ @Bean
|
|
|
@Override
|
|
|
- public void configure(AuthenticationManagerBuilder auth) throws Exception {
|
|
|
+ public AuthenticationManager authenticationManagerBean() throws Exception {
|
|
|
+ return super.authenticationManagerBean();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 配置认证使用的 provider
|
|
|
+ *
|
|
|
+ * @param
|
|
|
+ * @return
|
|
|
+ * @date 2021-07-25 下午2:31
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
|
|
auth.authenticationProvider(userAuthProvider);
|
|
|
}
|
|
|
|
|
|
@@ -145,8 +171,6 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
* 角色继承
|
|
|
* ADMIN 可以访问 USER 的权限,反之不可
|
|
|
*
|
|
|
- * @param
|
|
|
- * @return
|
|
|
* @date 2019-07-05 上午11:18
|
|
|
*/
|
|
|
@Bean
|
|
|
@@ -155,12 +179,4 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|
|
roleHierarchy.setHierarchy("ROLE_ADMIN > ROLE_USER");
|
|
|
return roleHierarchy;
|
|
|
}
|
|
|
-
|
|
|
- private SecurityExpressionHandler<FilterInvocation> webExpressionHandler() {
|
|
|
- DefaultWebSecurityExpressionHandler defaultWebSecurityExpressionHandler =
|
|
|
- new DefaultWebSecurityExpressionHandler();
|
|
|
- defaultWebSecurityExpressionHandler.setRoleHierarchy(roleHierarchy());
|
|
|
-
|
|
|
- return defaultWebSecurityExpressionHandler;
|
|
|
- }
|
|
|
}
|