簡體   English   中英

用戶登錄后,如何在控制器中獲取會話數據

[英]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谷歌搜索,但找不到有關此的任何具體信息。

好吧,您有幾種選擇:

  1. 您可以調用靜態方法SecurityContextHolder.getContext().getAuthentication() (通過這種方式,您可以在整個應用程序中獲取憑據)
  2. 如果您在控制器中,則可以將Principle作為參數來獲取信息
  3. 如果需要身份驗證令牌,可以將“ Authentication作為參數。

請注意,您必須具有某種機制來對用戶進行身份驗證(例如,使用活動目錄的openid),並使服務器與之配合使用。

有關更多信息,請參見http://www.baeldung.com/get-user-in-spring-security

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM