当前位置: 首页 > 面试题库 >

如何在Spring Security / SpringMVC中手动设置经过身份验证的用户

吕俊美
2023-03-14
问题内容

新用户提交“新帐户”表单后,我想手动登录该用户,这样他们就不必登录后续页面。

通过spring安全拦截器的普通表单登录页面工作正常。

在新帐户形式的控制器中,我将创建一个UsernamePasswordAuthenticationToken并在SecurityContext中手动进行设置:

SecurityContextHolder.getContext().setAuthentication(authentication);

稍后,我在同一页面上检查用户的登录身份:

SecurityContextHolder.getContext().getAuthentication().getAuthorities();

这将返回我之前在身份验证中设置的权限。一切都很好。

但是,在我加载的下一页上调用相同的代码时,身份验证令牌只是UserAnonymous。

我不清楚为什么它没有保留我在上一个请求中设置的身份验证。有什么想法吗?

  • 可能与会话ID设置不正确有关吗?
  • 是否有某种方式可能会覆盖我的身份验证?
  • 也许我还需要保存身份验证的另一步骤?
  • 还是我需要做一些事情来在整个会话中声明身份验证,而不是某种程度上的单个请求?

只是寻找一些想法可以帮助我了解这里发生的事情。


问题答案:

不久前,我和您有同样的问题。我不记得详细信息,但是以下代码对我有用。该代码在Spring
Webflow流中使用,因此在RequestContext和ExternalContext类中使用。但是与您最相关的部分是doAutoLogin方法。

public String registerUser(UserRegistrationFormBean userRegistrationFormBean,
                           RequestContext requestContext,
                           ExternalContext externalContext) {

    try {
        Locale userLocale = requestContext.getExternalContext().getLocale();
        this.userService.createNewUser(userRegistrationFormBean, userLocale, Constants.SYSTEM_USER_ID);
        String emailAddress = userRegistrationFormBean.getChooseEmailAddressFormBean().getEmailAddress();
        String password = userRegistrationFormBean.getChoosePasswordFormBean().getPassword();
        doAutoLogin(emailAddress, password, (HttpServletRequest) externalContext.getNativeRequest());
        return "success";

    } catch (EmailAddressNotUniqueException e) {
        MessageResolver messageResolvable 
                = new MessageBuilder().error()
                                      .source(UserRegistrationFormBean.PROPERTYNAME_EMAIL_ADDRESS)
                                      .code("userRegistration.emailAddress.not.unique")
                                      .build();
        requestContext.getMessageContext().addMessage(messageResolvable);
        return "error";
    }

}


private void doAutoLogin(String username, String password, HttpServletRequest request) {

    try {
        // Must be called from request filtered by Spring Security, otherwise SecurityContextHolder is not updated
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
        token.setDetails(new WebAuthenticationDetails(request));
        Authentication authentication = this.authenticationProvider.authenticate(token);
        logger.debug("Logging in with [{}]", authentication.getPrincipal());
        SecurityContextHolder.getContext().setAuthentication(authentication);
    } catch (Exception e) {
        SecurityContextHolder.getContext().setAuthentication(null);
        logger.error("Failure in autoLogin", e);
    }

}


 类似资料:
  • 问题内容: 这是我的情况: 一个Web应用程序对许多应用程序执行某种SSO 登录的用户,而不是单击链接,该应用就会向正确的应用发布包含用户信息(名称,pwd [无用],角色)的帖子 我正在其中一个应用程序上实现SpringSecurity以从其功能中受益(会话中的权限,其类提供的方法等) 因此,我需要开发一个 自定义过滤器 -我猜想-能够从请求中检索用户信息,通过自定义 DetailsUserSe

  • 我使用Firefox WebDriver在Python 2.7与硒。我的python程序启动火狐浏览器,并在运行程序时访问不同的网站。但是,我需要设置具有身份验证的代理,以便当程序访问任何网站时,它将通过代理服务器访问。 关于SO也有一些类似的问题。但是,没有针对Python的Selenium Firefox WebDriver的特定解决方案。 Python Selenium WebDrive-代

  • 我的代码在这里:代码重新发布是因为我想问一个更直接的问题。如何在未经身份验证的用户和经过身份验证的用户之间切换?我的未经验证的文件似乎已缓存,我使用了以下方法: 在我剩下的api代码之前,它仍然不能工作。谢谢你的帮助 注意:我知道它不起作用,因为我在切换配置/凭据提供程序后立即使用lambda进行调用,并且只有授权用户才能调用此方法。 编辑@behrooziAWS答案: API代码: 完整错误:B

  • 在我的REST应用程序中,我使用了基本身份验证(发送用户密码并在每次请求时登录)。对于某些需要,我使用以下方法获得登录用户: 但是后来我实现了Spring-Security-Oaut2,我使用访问令牌代替密码和登录。现在. getMain()方法返回“匿名用户”。所以我的问题:有没有办法像上面在sping-security-oauth中那样以某种方式获取登录用户? 编辑:我发现我的安全性“拦截ur

  • 问题内容: 我正在用Java运行这个简单的硒测试: 但是在我的办公室这里需要代理身份验证,我不知道如何设置它。 我必须将我的用户名和密码放在某个地方。 你能帮我吗? 问题答案: PhantomJS使用从命令行(docs)设置的三个代理选项。 指定要使用的代理服务器(例如)。 指定代理服务器的类型(默认为)。 指定代理的认证信息,例如。 要使用这些功能,您必须将它们添加到DesiredCapabil

  • 我正在研究使用JWT身份验证的Spring Security实现。我不确定如何检查用户角色并在方法级别获得经过身份验证的用户。我在网上看到这个例子: 我需要从JWT令牌中提取用户类型吗?还有其他方法可以实现吗?在我看来,仅使用是不完整的。 看起来这段代码是用来获得用户如果使用会话类型,我得到NPE。你知道对于JWT我如何才能成为用户吗? Github完整来源:https://Github.com/

  • 问题内容: 前言 这是我第一次尝试过滤器,要小心。 项目介绍 我正在尝试为我们的一些应用程序最终确定SSO的构建,而且似乎步履维艰。我尝试连接的Webapp使用“身份验证”标头来确定应用程序内的用户凭据。我构建了一个Filter,希望在将标头传递到Web应用程序之前对其进行设置。 问题 该代码通过Eclipse验证,编译,加载到Tomcat,然后传递到Webapp。唯一缺少的是Authentica

  • 我目前正在开发一个 API 授权。所以基本上我有一个过滤器。例如,在我的 RestController 中,我想注释应该通过 过滤的请求。所以我的问题是:我如何设置(或任何其他东西)来将注释与JwtAuthorizationFilter链接? 谢谢!向塞巴斯蒂安问好