Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在使用Spring Security成功登录后将用户转发回所需的受保护页面_Java_Jsp_Spring Security - Fatal编程技术网

Java 如何在使用Spring Security成功登录后将用户转发回所需的受保护页面

Java 如何在使用Spring Security成功登录后将用户转发回所需的受保护页面,java,jsp,spring-security,Java,Jsp,Spring Security,我正在使用Spring Security来处理我的web应用程序的所有身份验证。情况是这样的,假设我有三个JSP页面: 1) 登录页面 2) 默认主页(需要验证) 3) 用户帐户页(需要身份验证) 标准流程工作正常: 1) 用户从登录页面成功登录 2) 用户被带到默认主页 以下是我无法理解的用例 1) 用户尝试访问其帐户页(但受密码保护)-有效 2) 用户被重定向以输入其凭据-works 3) 用户成功提供凭据-有效 4) 用户被重定向到其帐户页-不工作(它将用户重定向到默认主页) 我正在使用一

我正在使用Spring Security来处理我的web应用程序的所有身份验证。情况是这样的,假设我有三个JSP页面:

1) 登录页面
2) 默认主页(需要验证)
3) 用户帐户页(需要身份验证)

标准流程工作正常:

1) 用户从登录页面成功登录
2) 用户被带到默认主页

以下是我无法理解的用例

1) 用户尝试访问其帐户页(但受密码保护)-有效
2) 用户被重定向以输入其凭据-works
3) 用户成功提供凭据-有效
4) 用户被重定向到其帐户页-不工作(它将用户重定向到默认主页)

我正在使用一个自定义的
AuthenticationSuccessHandler
,但问题是,每当我查看请求以查看它们来自何处时,总是来自
/j\u spring\u security\u check

有没有简单的方法来隔离他们来自哪一页

以下是我的applicationContext.xml中与Spring安全设置相关的内容:

<bean id="successfulLoginService" class="com.service.SuccessfulLoginService">
    <property name="defaultTargetUrl" value="/listings/add_listing.htm"/>
  </bean>

  <security:http pattern="/**.html" security="none" />
  <security:http pattern="/login/*.htm" security="none" />
  <security:http pattern="/listings/listing.htm" security="none" />
  <security:http pattern="/listings/viewListing.htm" security="none" />
  <security:http pattern="/search/keyword/*.htm" security="none" />

  <security:http auto-config="true">
    <security:intercept-url pattern="/**.htm" access="ROLE_USER" />
    <security:intercept-url pattern="/**/**.htm" access="ROLE_USER" />
    <security:form-login login-page="/login/login.htm" authentication-failure-handler-ref="failedLoginService" authentication-success-handler-ref="successfulLoginService"/>
  </security:http>

  <security:authentication-manager>
    <security:authentication-provider
      user-service-ref="userDetailsService" />
  </security:authentication-manager>

你在找什么?成功登录后,您可以使用它将用户重定向到某个位置。

您可以从
org.springframework.security.web.savedrequest.RequestCache
获取保存的请求

在自定义成功处理程序中:

RequestCache requestCache = new HttpSessionRequestCache();
SavedRequest savedRequest = requestCache.getRequest(request, response);
String targetUrl = savedRequest.getRedirectUrl();

查看default
SavedRequestAwareAuthenticationSuccessHandler
了解更多详细信息。

我不这么认为,我只是尝试将其插入,但没有成功。我还使用了身份验证成功处理程序ref,这可能会在代码中造成一些混乱。我尝试在我的身份验证成功处理程序ref中连接一个默认的目标url,但它总是导航到该url(即使这不是我在一开始没有访问权限时尝试访问的url),我不知道为什么我以前不知道如何做。我已经寻找这个答案好几个月了,当我想不出来的时候,我一直把它放在次要位置。非常感谢你抽出时间回答我的问题,Aleksandr,你是本周我的英雄。
RequestCache requestCache = new HttpSessionRequestCache();
SavedRequest savedRequest = requestCache.getRequest(request, response);
String targetUrl = savedRequest.getRedirectUrl();