【问题标题】:Get initial requested url in spring security 4在 Spring Security 4 中获取初始请求的 url
【发布时间】:2016-05-04 10:34:31
【问题描述】:

最近我升级了我所有的项目 API,如 Spring、Spring Security、Hibernate、Maven、Java。在升级之前,我使用的是 Spring 3 和 Spring Security 2。 现在我在我的项目中使用 Spring 4 和 Spring Security 4,并且我还使用 cas 身份验证进行登录。 当用户登录我的应用程序时,根据初始请求的 URL,我想设置用户的目标 URL。在升级之前它工作正常。

我使用这个SPRING_SECURITY_SAVED_REQUEST_KEY 来获取初始请求 URL。

现在我正在使用 -

savedRequest = new HttpSessionRequestCache().getRequest(request, response);

获取初始请求,但它总是返回null。

有什么方法可以在登录后获取用户的初始请求?

【问题讨论】:

    标签: java spring spring-mvc spring-security


    【解决方案1】:

    你可以使用SavedRequestAwareAuthenticationSuccessHandler

    【讨论】:

      【解决方案2】:

      这是我使用SavedRequestAwareAuthenticationSuccessHandler的方法。

      在我的例子中,我在localhost:8081 有一个授权服务器,在localhost:8083/app 有一个安全的用户界面。

      在授权服务器中,我创建了以下类:

      @Component
      public class AuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {   
      
          @Override
          public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                                              Authentication authentication) throws IOException, ServletException {
              setDefaultTargetUrl("http://localhost:8083/app");
              super.onAuthenticationSuccess(request, response, authentication);
          }
      }
      

      在我的例子中,有 3 种用户登录场景:

      1. 用户导航到http://localhost:8081/login
      2. 用户导航到http://localhost:8083/app
      3. 用户导航到应用程序中的任何安全 URL,例如 http://localhost:8083/app/files

      上面的类通过将用户重定向到原始 url 来涵盖所有这些场景,或者如果用户选择通过 http://localhost:8081/login 登录,它会将他们重定向到默认目标 url,即 http://localhost:8083/app。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-08
        • 2017-02-01
        • 1970-01-01
        • 2019-03-21
        • 2013-05-07
        • 1970-01-01
        • 2015-01-14
        相关资源
        最近更新 更多