我在WebSecurityConfigurerAdapter
Spring Boot
应用程序中扩展了MVC
。我还创建了以下方法
@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
控制器中输入用户名,角色,电子邮件等信息。我怎样才能做到这一点?
我试过谷歌搜索,但没有找到任何具体的信息。
嗯,你有几个选择:
SecurityContextHolder.getContext().getAuthentication()
(这样您就可以获得遍布应用程序的凭据)Principle
作为参数来获取信息Authentication
作为参数。请注意,您必须具有某种机制来对用户进行身份验证(例如,使用active-directory打开openid)并让服务器使用它。
有关更多信息,请参阅http://www.baeldung.com/get-user-in-spring-security