【发布时间】:2014-02-14 17:15:16
【问题描述】:
我一直在寻找与我很接近的问题,但似乎找不到我正在寻找的确切答案。我猜我想要做的解决方案相当简单。
我有一个带有默认页面 (index.jsp) 的网站。有一个从索引页面到管理页面 (admin.jsp) 的链接。此页面已使用 Spring 进行了很长时间的密码保护,没有问题。这是我一直在使用的安全上下文文件:
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<global-method-security pre-post-annotations="enabled" secured-annotations="enabled"/>
<http use-expressions="true">
<intercept-url pattern="/admin.jsp" access="hasRole('administrator')" />
<intercept-url pattern="/**" access="permitAll" />
<form-login login-page="/login.jsp" default-target-url="/admin.jsp" />
<logout />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="${admin.username}" password="${admin.password}" authorities="administrator" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
我现在也需要密码保护 index.jsp。如果我复制元素的子元素(并且我创建了单独的 login.jsp 文件,不确定是否有必要),如下所示:
<http use-expressions="true">
<intercept-url pattern="/index.jsp" access="hasRole('user')" />
<form-login login-page="/indexLogin.jsp" default-target-url="/index.jsp" />
<logout />
<intercept-url pattern="/admin.jsp" access="hasRole('administrator')" />
<form-login login-page="/adminLogin.jsp" default-target-url="/admin.jsp" />
<logout />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="${admin.username}" password="${admin.password}" authorities="administrator" />
<user name="${user.username}" password="${user.password}" authorities="user" />
</user-service>
</authentication-provider>
</authentication-manager>
那么 index.jsp 就像预期的那样受到密码保护。但是,如果我单击在新选项卡中打开 admin.jsp 页面的按钮,我会收到 403 - Access is denied 错误。我假设这是因为它会自动尝试使用管理页面上的用户凭据。
然后我尝试创建两个单独的元素,每个元素都有每个不同页面的元素:
<http use-expressions="true">
<intercept-url pattern="/index.jsp" access="hasRole('user')" />
<form-login login-page="/indexLogin.jsp" default-target-url="/index.jsp" />
<logout />
</http>
<http use-expressions="true">
<intercept-url pattern="/admin.jsp" access="hasRole('administrator')" />
<form-login login-page="/adminLogin.jsp" default-target-url="/admin.jsp" />
<logout />
</http>
然后我收到一条错误消息:
java.lang.IllegalArgumentException:通用匹配模式('/**')在过滤器链中的其他模式之前定义,导致它们被忽略。请检查您的命名空间或 FilterChainProxy bean 配置中的排序
谁能告诉我如何强制 Spring 在每次访问 index.jsp 或 admin.jsp 时提示输入凭据,而不是假定应该使用现有凭据?
【问题讨论】:
标签: java spring jsp spring-mvc spring-security