我正在嘗試為 /testMVCController/** 端點實作 SessionCreationPolicy.ALWAYS,為其余端點 (/**) 實作 SessionCreationPolicy.STATELESS。
預期場景:
當訪問 /testMVCController/displayUsers 時,用戶登錄一次,我在 UserDetailsS??ervice 中實作的日志記錄了與該用戶關聯的權限。之后,所有對 /testMVCController/displayUsers 或 /testMVCController/** 下的其他 URL 的請求將不會再次登錄權限,因為會話創建策略始終是并且用戶已經登錄。
這在我沒有指定第二個安全配置 (X509ClientSessionCreationPolicyStateless) 時有效,但是當我添加它時,所有請求都變為無狀態會話。
它不適用于當前的安全配置,因為在我使用客戶端證書登錄后,在 /testMVCController/** 端點(例如 /testMVCController/displayUsers)下執行的任何請求中,都會查詢 authenticationUserDetailsS??ervice 并記錄權限串列瀏覽器發出的每個檔案請求(.js 檔案,.css 檔案,...),即使在初始登錄之后也是如此。
因此,如果有 3 個請求(/testMVCController/displayUsers、displayUsers.js、displayUsers.css),authenticationUserDetailsS??ervice 中的權限日志串列會記錄 3 次。
我如下所示配置了 SecurityConfiguration 但它不起作用:
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class SecurityConfiguration {
@Configuration
@Order(1)
public static class X509ClientSessionCreationPolicyAlways extends WebSecurityConfigurerAdapter {
@Autowired
private X509CUDService x509CUDService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/testMVCController/**")
.csrf().disable()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.x509()
.authenticationUserDetailsService(x509CUDService)
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.ALWAYS);
}
}
@Configuration
@Order(2)
public static class X509ClientSessionCreationPolicyStateless extends WebSecurityConfigurerAdapter {
@Autowired
private X509CUDService X509CUDService ;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/**")
.csrf().disable()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.x509()
.authenticationUserDetailsService(X509CUDService);
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
}
我搜索了這個問題,發現了各種鏈接(例如Spring session creation policy per-request?、Spring Session:如何為不同的 URL和Multiple HttpSecurity創建單獨的會話管理策略),但沒有一個有效。
提前致謝!
uj5u.com熱心網友回復:
我錯過了一些關于我的配置的細節。我正在捕獲所有的請求/testMVCController/**并且它正在作業,但是除了捕獲對任何型別端點的請求/testMVCController/**(例如:/testMVCController/usersList),我還必須捕獲這些頁面為獲取它們的腳本而發出的請求( .js 檔案、.css 檔案、.png 檔案)。發生的事情是:對/testMVCController/usersList) 的請求被配置為SessionCreationPolicy.ALWAYS,但隨后的請求(例如usersList.js、usersList.css等)被配置為SessionCreationPolicy.STATELESS,在這些情況下,X509CustomUserDetailsService總是會咨詢 。
示例: GET 請求/testMVCController/usersList有效,但此 usersList 頁面中也有請求usersList.js,usersList.css等。
因此,一旦我將這些資源路徑包含在 antMatchers 中,一切都會完美運行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/420262.html
標籤:
