Spring Security 3.0:认证后的 GET 请求

Posted

技术标签:

【中文标题】Spring Security 3.0:认证后的 GET 请求【英文标题】:Spring Security 3.0: GET request after authentication 【发布时间】:2012-03-13 23:44:55 【问题描述】:

我有一个类似于 Spring Security 3.0 SSO 的内置 Spring 应用程序和一个针对我的 SSO 服务器进行身份验证的外部 php 应用程序。

当用户登录此应用程序时,我也想在 php 应用程序中对他/她进行身份验证。

所以我这样做了:

……

public class CustomAuthenticationHandler extends SavedRequestAwareAuthenticationSuccessHandler 

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException 

    DefaultHttpClient httpclient = new DefaultHttpClient();
    try 
        HttpGet httpget = new HttpGet("http://localhost/phpapp/signin");
        httpget.setHeader("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:10.0.2) Gecko/20100101 Firefox/10.0.2");
        httpget.setHeader("Connection", "keep-alive");
        httpget.setHeader("Referer", "http://localhost:8080/;jsessionid="+request.getSession().getId());
        httpget.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        httpget.setHeader("Accept-Language", "en-gb,en;q=0.5");
        httpget.setHeader("Accept-Encoding", "gzip, deflate");
        httpget.setHeader("Cookie", "JSESSIONID="+request.getSession().getId());

        HttpResponse res = httpclient.execute(httpget);

     finally 
        httpclient.getConnectionManager().shutdown();
    


在身份验证后处理程序中,我针对 php 应用程序的登录创建了一个“GET”请求。 当它被执行时,在请求正文中返回“登录页面”,就像用户没有记录一样。注意我用过:

httpget.setHeader("Referer", "http://localhost:8080/;jsessionid="+request.getSession().getId());

作为当前经过身份验证的 jsessionid。

但是如果我把相同的链接:http://localhost/phpapp/signin作为正常的href链接放在页面中,然后点击它就可以正常工作。

知道为什么不能在过滤器中处理相同类型的请求吗?

【问题讨论】:

【参考方案1】:

这很可能与 Cookie 路径有关。 Cookie 与子域、域、路径和端口相关联,因此除非两个应用程序都在 http://localhost/phpapp/ 上运行,否则您将遇到问题。您可以尝试修改 Spring 以不设置 cookie 的路径或端口元素,即仅将其绑定到 localhost。

但是,我强烈建议您采用另一种方法,例如真正的单点登录系统,而不是尝试在应用之间代理 cookie。

【讨论】:

以上是关于Spring Security 3.0:认证后的 GET 请求的主要内容,如果未能解决你的问题,请参考以下文章

Grails 3.0 和 Spring Security

Spring Security SAML Extension 将 ADFS 3.0 与其他声明提供程序集成

Security安全认证 | Spring Boot如何集成Security实现安全认证

配置 ADFS 3.0 / SAML 2.0 以使用 Spring Security 进行 SSO 集成

带有 jdbc 的 Spring Security 3.0

Spring-Security:认证后调用方法