我正在使用 Spring Security 来处理我的 Web 应用程序的所有身份验证。情况如下,假设我有三个 JSP 页面:
1)登录页面
2)默认主页(需要认证)
3)用户账户页面(需要认证)
标准流程工作得很好:
1) 用户从登录页面成功登录
2) 用户被带到默认主页
这是我无法弄清楚的用例
1) 用户尝试访问他们的帐户页面(但它受密码保护) - 有效
2) 用户被重定向以输入他们的凭据 - 有效
3) 用户成功提供凭据 - 有效
4) 用户被重定向到他们的帐户页面 - 不工作(它将它们重定向到默认主页)
我正在使用 custom AuthenticationSuccessHandler
,但问题是,每当我查看请求以查看它们来自哪里时,它总是来自/j_spring_security_check
.
有没有一些简单的方法来隔离它们来自哪个页面?
这是我的 applicationContext.xml 中与我的 Spring Security 设置相关的内容:
<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>
这是我的成功登录服务.java:
@Service("successfulLoginService")
public class SuccessfulLoginService extends SimpleUrlAuthenticationSuccessHandler
{
@Autowired
UserDao userDao;
@Override
public void onAuthenticationSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException, ServletException
{
Users user = null;
String username = ((SpringSecurityUser) authentication.getPrincipal()).getUsername();
try
{
user = userDao.getUserByEmail(username);
} catch (Exception e)
{
e.printStackTrace();
throw new ServletException("Failed to login", e);
}
request.getSession().setAttribute("user", user);
response.sendRedirect("/MyApplication" + determineTargetUrl(request, response));
}
}