首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在用户登录后在我的控制器中获取会话数据?

如何在用户登录后在我的控制器中获取会话数据?
EN

Stack Overflow用户
提问于 2017-12-16 13:39:45
回答 1查看 48关注 0票数 1

我在我的Spring Boot MVC应用程序中扩展了MVC。我还创建了以下方法

代码语言:javascript
运行
复制
@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控制器。我怎样才能做到这一点?

我试着在谷歌上搜索,但没有找到任何有关这方面的具体信息。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-16 14:02:38

嗯,你没有什么选择:

  1. 您可以调用静态方法SecurityContextHolder.getContext().getAuthentication() (通过这种方式,您可以在整个应用程序中获取凭证)
  2. 如果您在控制器中,可以使用Principle作为参数来获取信息。
  3. 如果您想要身份验证令牌,可以使用一个Authentication作为参数。

请注意,您必须有某种机制来对用户进行身份验证(例如,使用活动目录的openid),并让您的服务器使用它。

有关更多信息,请参见http://www.baeldung.com/get-user-in-spring-security

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47846269

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档