我有一個使用 Session 的非常基本的 Spring Security 設定。我的問題是我找不到使用任何型別的會話偵聽器(Spring 和 Servlet API 版本)來偵聽 SessionCreated 事件的方法。登錄正在作業并且會話正在正確創建。
我需要偵聽器的原因是因為我想初始化某些會話屬性(例如購物卡丁車、最近商品串列),以便我可以從@Controller請求映射中無縫訪問它們,而不必擔心會話屬性是否已初始化。
安全配置代碼:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
DataSource dataSource;
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.ALWAYS)
.and()
.authorizeRequests()
.antMatchers("/secured/**").authenticated()
.anyRequest().permitAll()
.and()
.formLogin()
.loginPage("/login")
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/")
.and()
.rememberMe().key("unique");
}
...
}
首先,我嘗試了最基本的會話偵聽器:
@Component
public class InitHttpSessionListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent event) {
...
}
}
我也試過這里的答案,但也沒有用
uj5u.com熱心網友回復:
從您的評論中可以清楚地看出,您正在使用Spring Session JDBC。由于 JDBC 的性質,它不支持會話事件的發布,因此您無法偵聽這些事件。
作為一種解決方法,您可以創建自己的AuthenticationSuccessHandler并將用于填充的邏輯Session放在那里。或者AuthenticationSuccessEvent使用 Spring 事件偵聽器收聽(進入會話會有點困難,但可行)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/370023.html
上一篇:根據前一天的值計算引數
