Spring Security 在其餘服務中獲取經過身份驗證和未經過身份驗證的用戶的用戶信息
在開發網頁應用程式時,安全性是一個重要的考慮因素。為了保護使用者資料和防止未經授權的訪問,我們需要使用一種可靠的身份驗證和授權機制。 Spring Security是一個功能強大且廣泛使用的安全框架,它提供了一套完整的解決方案來保護我們的應用程式。在本文中,我們將探討如何在Spring Security中取得經過身份驗證和未經過身份驗證的使用者的使用者資訊。 php小編百草將向您展示如何利用Spring Security的功能,獲取用戶資訊以及在不同服務之間共享用戶資訊的方法。無論您是初學者還是有經驗的開發人員,本文都將為您提供有關Spring Security的詳細信息,並幫助您提升應用程式的安全性。
問題內容
我有一個 spring rest 服務,我想將它用於經過身份驗證和未經身份驗證的用戶。如果使用者經過身份驗證,我想從 securitycontextholder.getcontext().getauthentication()
取得使用者資訊。
- 如果我使用
.antmatchers("/app/rest/question/useroperation/list/**").permitall()
在 ouath2 配置中,如下所示,然後我可以獲取用戶信息 經過身份驗證的用戶,但未經過身份驗證的用戶會出現 401 錯誤。 - 如果我
.antmatchers("/app/rest/question/useroperation/list/**").permitall()
並忽略 websecurity 中的 urlweb.ignoring()..antmatchers("/app/rest/question/useroperation/list/**")
在securityconfiguration
中如下所示,然後所有使用者都可以調用 服務,但我無法從 securitycontext 取得使用者資訊。
如何設定我的 spring security 來呼叫經過驗證和未經驗證的使用者的 url,並在使用者登入時從 securitycontext 取得使用者資訊。
@configuration @enableresourceserver protected static class resourceserverconfiguration extends resourceserverconfigureradapter { @inject private http401unauthorizedentrypoint authenticationentrypoint; @inject private ajaxlogoutsuccesshandler ajaxlogoutsuccesshandler; @override public void configure(httpsecurity http) throws exception { http .exceptionhandling() .authenticationentrypoint(authenticationentrypoint) .and() .logout() .logouturl("/app/logout") .logoutsuccesshandler(ajaxlogoutsuccesshandler) .and() .csrf() .requirecsrfprotectionmatcher(new antpathrequestmatcher("/oauth/authorize")) .disable() .headers() .frameoptions().disable() .sessionmanagement() .sessioncreationpolicy(sessioncreationpolicy.stateless) .and() .authorizerequests() .antmatchers("/views/**").permitall() .antmatchers("/app/rest/authenticate").permitall() .antmatchers("/app/rest/register").permitall() .antmatchers("/app/rest/question/useroperation/list/**").permitall() .antmatchers("/app/rest/question/useroperation/comment/**").authenticated() .antmatchers("/app/rest/question/useroperation/answer/**").authenticated() .antmatchers("/app/rest/question/definition/**").hasanyauthority(authoritiesconstants.admin) .antmatchers("/app/rest/logs/**").hasanyauthority(authoritiesconstants.admin) .antmatchers("/app/**").authenticated() .antmatchers("/websocket/tracker").hasauthority(authoritiesconstants.admin) .antmatchers("/websocket/**").permitall() .antmatchers("/metrics/**").hasauthority(authoritiesconstants.admin) .antmatchers("/health/**").hasauthority(authoritiesconstants.admin) .antmatchers("/trace/**").hasauthority(authoritiesconstants.admin) .antmatchers("/dump/**").hasauthority(authoritiesconstants.admin) .antmatchers("/shutdown/**").hasauthority(authoritiesconstants.admin) .antmatchers("/beans/**").hasauthority(authoritiesconstants.admin) .antmatchers("/info/**").hasauthority(authoritiesconstants.admin) .antmatchers("/autoconfig/**").hasauthority(authoritiesconstants.admin) .antmatchers("/env/**").hasauthority(authoritiesconstants.admin) .antmatchers("/trace/**").hasauthority(authoritiesconstants.admin) .antmatchers("/api-docs/**").hasauthority(authoritiesconstants.admin) .antmatchers("/protected/**").authenticated(); } }
安全配置
@Configuration @EnableWebSecurity public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Inject private UserDetailsService userDetailsService; @Bean public PasswordEncoder passwordEncoder() { return new StandardPasswordEncoder(); } @Inject public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .userDetailsService(userDetailsService) .passwordEncoder(passwordEncoder()); } @Override public void configure(WebSecurity web) throws Exception { web.ignoring() .antMatchers("/bower_components/**") .antMatchers("/fonts/**") .antMatchers("/images/**") .antMatchers("/scripts/**") .antMatchers("/styles/**") .antMatchers("/views/**") .antMatchers("/i18n/**") .antMatchers("/swagger-ui/**") .antMatchers("/app/rest/register") .antMatchers("/app/rest/activate") .antMatchers("/app/rest/question/useroperation/list/**") .antMatchers("/console/**"); } @EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true) private static class GlobalSecurityConfiguration extends GlobalMethodSecurityConfiguration { @Override protected MethodSecurityExpressionHandler createExpressionHandler() { return new OAuth2MethodSecurityExpressionHandler(); } } }
解決方法
permitall()
仍然需要 authentication
物件出現在 securitycontext
中。
對於非 oauth 用戶,這可以透過啟用匿名存取來實現:
@override public void configure(httpsecurity http) throws exception { http //some configuration .and() .anonymous() //allow anonymous access .and() .authorizerequests() .antmatchers("/views/**").permitall() //other security settings
匿名存取將會新增額外的篩選器:anonymousauthenticationfilter
到填入anonymousauthenticationtoken
作為驗證資訊的篩選器鏈,以防securitycontext
中沒有authentication
物件
我有這個安全配置用於透過/public/authphpcnendcphp檢查authuser中文:
@Override protected void configure(HttpSecurity http) throws Exception { http.cors().and().authorizeRequests() .antMatchers("/api/skills/**", "/api/profile/**", "/api/info/**").authenticated() .antMatchers("/api/**").hasAuthority(Role.ROLE_ADMIN.getAuthority()) .antMatchers("/public/auth").permitAll() .and().httpBasic() .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) .and().csrf().disable(); } @GetMapping(value = "/public/auth") private ResponseEntity<User> getAuthUser(@AuthenticationPrincipal AuthUser authUser) { return authUser == null ? ResponseEntity.notFound().build() : ResponseEntity.ok(authUser.getUser()); }
以上是Spring Security 在其餘服務中獲取經過身份驗證和未經過身份驗證的用戶的用戶信息的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

我有下面的程式碼:publicSecurityFilterChainsecurityFilterChain(HttpSecurityhttp)throwsException{returnhttp.httpBasic().disable().cors().and().csrf().disable().authorizeHttpRequests().requestMatchers("

如何使用Java開發一個基於SpringSecuritySAML的單一登入系統引言:隨著網路的快速發展,越來越多的應用程式被開發出來。在這些應用程式中,使用者登入是最常見的功能之一。然而,對於企業級應用程序,用戶需要在多個系統中進行登錄,這將導致用戶的登入體驗非常糟糕。為了解決這個問題,單一登入系統(SingleSign-On,簡稱SSO)應運而生。簡

我正在嘗試使用GO實現存取令牌驗證。但我在網路上看到的例子似乎只是用TOKEN_SECRET來驗證它。但是我習慣了在Javaspring中編程,並且不需要使用TOKEN_SECRET。我只是提供jwk-set-uri,它會檢查有效性(自動-安全過濾器等),我知道它與oauth伺服器通訊並進行此驗證。 Go中是否沒有函式庫可以透過向oauth伺服器發出請求來檢查令牌是否有效?我知道我知道我可以透過向oauth伺服器的用戶資訊端點發出請求來手動進行此操作:http://localh

在後台管理系統中,通常需要存取權限控制,以限制不同使用者對介面的存取能力。如果使用者缺乏特定權限,則無法存取某些介面。本文將用waynboot-mall專案舉例,跟大家介紹常見後管系統如何引入權限控制框架SpringSecurity。大綱如下:waynboot-mall專案網址:https://github.com/wayn111/waynboot-mall一、什麼是SpringSecuritySpringSecurity是一個基於Spring框架的開源項目,旨在為Java應用程式提供強大且靈活的安

如何使用Java開發一個基於SpringSecurityOAuth2的單點登錄系統引言:隨著互聯網的高速發展,越來越多的網站和應用程式需要用戶進行登錄,而用戶卻不希望為每個網站或應用程式記住一個帳號和密碼。單一登入系統(SingleSign-On,簡稱SSO)能夠解決這個問題,允許使用者在一次登入後,無需重複認證即可存取多個網站和應用程式。本文將介紹

我有一個springrest服務,我想將它用於經過身份驗證和未經身份驗證的用戶。如果使用者經過身份驗證,我想從securitycontextholder.getcontext().getauthentication()取得使用者資訊。如果我使用.antmatchers("/app/rest/question/useroperation/list/**").permitall()在ouath2配置中,如下所示,然後我可以獲取用戶資訊經過身份驗證的用戶,但未經過身份驗證的用戶會出現40

引言在當今互聯互通的世界中,RESTfulapi已成為應用程式之間進行通訊的關鍵機制。透過Java這款強大的程式語言,您可以建立高效、可擴充且維護良好的RESTfulAPI。第1章:RESTfulAPI基礎RESTful架構的原則和最佳實踐Http方法、狀態代碼和回應標頭JSON和XML等資料格式第2章:設計和建模RESTfulAPIRESTfulAPI設計原則資源建模和URI設計版本控制和HATEOAS第3章:使用SpringBoot建立RESTfulAPISpringBoot簡介和入門建置和

Vue.js 和 Spring Boot 透過以下方式互動:RESTful API:Vue.js 使用 Axios 傳送非同步 HTTP 請求,Spring Boot 提供 RESTful API 實作。資料傳遞:資料透過請求和回應傳遞,如請求正文或查詢參數。請求方法:GET、POST、PUT 和 DELETE 等 HTTP 請求方法用於指定操作。路由:Spring Boot @RequestMapping 註解定義控制器路由,Vue.js 使用 Vue Router 定義介面路由。狀態管理:Vu