我在我的 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
Controller 中提供一些信息,例如用户名、角色、电子邮件等。我怎样才能实现这一目标?
我尝试谷歌搜索,但找不到任何有关此的具体信息。
最佳答案
好吧,你有几个选择:
- 您可以调用静态方法
SecurityContextHolder.getContext().getAuthentication()
(这样您就可以获取整个应用程序的凭据) - 如果你在 Controller 中,你可以有一个
Principle
作为参数来获取信息 - 如果您需要身份验证 token ,可以使用
Authentication
作为参数。
请注意,您必须有某种机制来验证用户身份(例如带有 Activity 目录的 openid)并让您的服务器使用它。
关于java - 用户登录后如何在 Controller 中获取 session 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47846269/