【问题标题】:Spring Security multiple filterChainProxy mapping/filters, custom filters Json OutputSpring Security 多个 filterChainProxy 映射/过滤器,自定义过滤器 Json 输出
【发布时间】:2012-01-30 00:48:59
【问题描述】:

我的 Web 应用程序的一部分是 RESTfull api,一部分是更标准的网页。

我希望 REST 部分带有一些自定义过滤器,例如 EntryPoint、SuccessHandler 和 FailureHandler。这部分在 /rest/** 映射中。

另一方面,其他一切都需要有更通用的过滤器,并用 /** 映射。

问题是找到一种简单的方法来定义具有不同映射过滤器的 filterChainProxy。

目前这个解决方案不起作用:

    <!-- Normal web app -->
<http auto-config="true" use-expressions="true" authentication-manager-ref="authenticationManager">
    <form-login/>
    <logout/>
    <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
</http>

<!-- Configure RESTfull services -->
<http use-expressions="true" authentication-manager-ref="authenticationManager" entry-point-ref="restAuthenticationEntryPoint" >
    <form-login authentication-success-handler-ref="restAuthenticationSuccessHandler" login-page="/rest/login" username-parameter="username" password-parameter="password" />
    <logout logout-url="/rest/logout" />
    <intercept-url pattern="/rest/**" method="GET" access="hasRole('ROLE_USER')" />
    <intercept-url pattern="/rest/**" method="POST" access="hasRole('ROLE_ADMIN')" />
    <intercept-url pattern="/rest/**" method="PUT" access="hasRole('ROLE_ADMIN')" />
    <intercept-url pattern="/rest/**" method="DELETE" access="hasRole('ROLE_ADMIN')" />
</http>

它抱怨:通用匹配在其他模式之前。

有没有办法在不使用定义的情况下定义 filterChainProxy 的情况下定义这样的东西? http 版本确实有助于减少配置量,因为我将不得不手动设置一个 UsernamePasswordAuthenticationFilter 等。

下一个问题更简单:我必须在使用 JSON 对象进行表单登录身份验证之后做出响应。

到目前为止,我已经实现了一个 SuccessHandler(实际上是 SimpleUrlAuthenticationSuccessHandler 的一个版本,没有重定向部分)。

如何编写我的 JSON 输出?

我必须有这样的东西:

HTTP 200 OK

与:

{"success":true,"customer {"email":"customer@email.com","session_id":"b83a41dfaca6785399f00837888886e646ff9088"}}

和 FailureHandler 类似的事情。它一定很简单,而且肯定是一些非常基本的东西,但是你是怎么做到的呢?重定向到自定义控制器不是一个解决方案,因为我将遇到非常简单的 REST 客户端可能无法理解的 301 重定向状态。

至少,我希望只有 http 标头,根本没有正文。

谢谢!

【问题讨论】:

  • 将 2 个不同的接口(WWW 和 REST)放在一个 ApplicationContext 中的原因是什么?两者之间有一些上下文/会话共享吗?登录 WWW 是否也意味着登录 REST?如果是这样,那么为什么要单独登录操作?如果不是,那为什么要把它们放在一起呢?难道这些不是为单个底层服务层提供不同接口的单独应用程序吗?
  • 这就像在一个普通的网络应用程序中有一个 REST 部分。我不想要另一个身份验证过程,我只想要其他过滤器。 REST 部分只需要在没有身份验证时显示错误,但另一方面,在网页上我必须将用户重定向到登录页面。

标签: json spring-mvc spring-security


【解决方案1】:

如果你可以升级到 Spring Security 3.1,supports multiple chains using namespace configuration。

【讨论】:

猜你喜欢
  • 2011-08-23
  • 2014-07-28
  • 2013-07-22
  • 2019-04-13
  • 2014-10-24
  • 1970-01-01
  • 2017-02-02
  • 1970-01-01
  • 2011-07-23
相关资源
最近更新 更多