[英]How to get the role that a user logged in with in my controller using Spring Security
[英]How can I get session data in my controller after user is logged in
我在Spring Boot
MVC
應用程序中擴展了WebSecurityConfigurerAdapter
。 我還創建了以下方法
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/")
.permitAll()
.antMatchers("/login")
.permitAll()
.antMatchers("/registration")
.permitAll()
.antMatchers("/dashboard")
.hasAuthority("ADMIN")
.anyRequest()
.authenticated()
.and()
.csrf()
.disable()
.formLogin()
.loginPage("/login")
.failureUrl("/login?error=true")
.defaultSuccessUrl("/dashboard")
.usernameParameter("email")
.passwordParameter("password")
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/home")
.and()
.exceptionHandling()
.accessDeniedPage("/access-denied");
}
用戶登錄后,我需要在/dashboard
控制器中輸入一些信息,例如用戶名,角色,電子郵件等。 我該如何實現?
我嘗試使用Google谷歌搜索,但找不到有關此的任何具體信息。
好吧,您有幾種選擇:
SecurityContextHolder.getContext().getAuthentication()
(通過這種方式,您可以在整個應用程序中獲取憑據) Principle
作為參數來獲取信息 Authentication
作為參數。 請注意,您必須具有某種機制來對用戶進行身份驗證(例如,使用活動目錄的openid),並使服務器與之配合使用。
有關更多信息,請參見http://www.baeldung.com/get-user-in-spring-security
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.