我有一个在Tomcat 7上运行的java Web应用程序 . 我在后端使用带有Spring Security 3.1的Spring 3.2,并且在/ api / **模式之后通过RESTful URL公开API .
Web应用程序的UI使用BackboneJS构建 . 我正在使用直接映射到RESTful URLS的Backbone模型 .
使用表单登录身份验证锁定UI,因此如果用户当前未经过身份验证,则始终会将用户重定向到登录屏幕 .
我现在正尝试使用http-basic身份验证将相同的RESTful URL公开给另一个外部服务 . 不幸的是,在保护相同的URL模式时,Spring似乎不允许我使用多个过滤器链 . 在配置文件中首先定义的那个似乎优先 .
我不想为同一个RESTful资源映射到单独的URL模式,但似乎我可能没有选择 .
以下是我(当前已损坏) spring 安全配置的重要示例:
<!-- configure basic http authentication -->
<http pattern="/api/**" create-session="stateless">
<intercept-url pattern="/**" access="ROLE_USER"/>
<http-basic/>
</http>
<!-- configure form-login authentication -->
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/ui/login" access="permitAll" />
<intercept-url pattern="/ui/logout" access="permitAll" />
<intercept-url pattern="/ui/loginfailed" access="permitAll" />
<intercept-url pattern="/**" access="ROLE_USER" />
<custom-filter ref="ajaxTimeoutRedirectFilter" after="EXCEPTION_TRANSLATION_FILTER" />
<form-login login-page="/ui/login" default-target-url="/" authentication-failure-url="/ui/loginfailed" />
<logout logout-success-url="/ui/logout" />
<session-management invalid-session-url="/ui/login"/>
</http>
我的问题是: Is it possible to configure two different types of security (http-basic and form-login) for the same URL patterns using Spring Security? Is there a best practice for this type of scenario?
谢谢 .
3 回答
为什么不像这样合并两个
<http>
元素:这将在同一过滤器链中设置
UsernamePasswordAuthenticationFilter
和BasicAuthenticationFilter
,它们可以为ui客户端和外部服务提供服务 .开箱即用,不能为单个URL模式应用2个不同的过滤器链 .
但建议将唯一的URL模式作为UI和API,因为将来必须应用完全不同的过滤器链 .
例如,SecurityContextRepository保存会话信息,并为每个请求检索 . 您不希望通过基本身份验证对UI和API访问应用相同的操作
尝试在API配置中替换pattern =“/ " by pattern="/api/ ”: