在 http 元素中使用特定的 url 模式时不调用 j_spring_security_check
Posted
技术标签:
【中文标题】在 http 元素中使用特定的 url 模式时不调用 j_spring_security_check【英文标题】:j_spring_security_check not invoke when use specific url pattern in http element 【发布时间】:2014-02-20 19:19:12 【问题描述】:我正在尝试使用 Spring Security 实现两个安全领域。我正在使用 Spring security 3.1.4 RELEASE 和 Spring 3.2.0 RELEASE。在我的 Web 应用程序中有两个用户,他们应该分别进行身份验证。因此我尝试使用多个 http 元素来过滤 url 模式并重定向到相应的登录页面。
这是我的 Spring-security.xml。
<beans:beans xmlns="http://www.springframework.org/schema/beans"
xmlns:security="http://www.springframework.org/schema/security"
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.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd" xmlns:beans="http://www.springframework.org/schema/beans">
<security:http pattern="/admin/**" auto-config="true" use-expressions="true">
<security:form-login login-page="/admin/login" default-target-url="/admin/dashboard"
authentication-failure-url="/admin/loginfailed"/>
<security:logout logout-success-url="/admin/logout"/>
<security:intercept-url pattern="/admin/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<security:intercept-url pattern="/admin/login" access="permitAll"/>
<security:intercept-url pattern="/admin/*" access="hasRole('ROLE_ADMIN')"/>
</security:http>
<security:http pattern="/customer/**" auto-config="true" use-expressions="true">
<security:form-login login-page="/customer/login" default-target-url="/customer/reports"
authentication-failure-url="/customer/loginfailed"/>
<security:logout logout-success-url="/customer/logout"/>
<security:intercept-url pattern="/customer/j_spring_security_check" access="permitAll"/>
<security:intercept-url pattern="/customer/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<security:intercept-url pattern="/customer/login" access="permitAll"/>
<security:intercept-url pattern="/customer/*" access="hasRole('ROLE_ADMIN')"/>
</security:http>
<beans:bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<beans:property name="jndiName">
<beans:value>java:/myDS</beans:value>
</beans:property>
</beans:bean>
<security:authentication-manager>
<security:authentication-provider>
<security:jdbc-user-service data-source-ref="dataSource"
users-by-username-query="SELECT login_name AS username, password, 1 AS enabled
FROM tbl_user WHERE login_name=?"
authorities-by-username-query="SELECT login_name , CASE role_id WHEN 2 THEN 'ROLE_USER' WHEN 1 THEN 'ROLE_ADMIN'ELSE '' END AS authority
FROM tbl_user WHERE login_name=?"
/>
</security:authentication-provider>
</security:authentication-manager>
</beans:beans>
这是我的 web.xml
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这是我的 login.jsp
enter code here
<c:url value="/j_spring_security_check" var="url" />
<form c role="form" action="$url" method='POST'>
<div>
<label>Email</label>
<div >
<input type="email" name="j_username" id="inputEmail3"
placeholder="Email">
</div>
</div>
<div >
<labe>Password</label>
<div>
<input type="password" name="j_password" id="inputPassword3"
placeholder="Password">
</div>
</div>
<div class="form-group">
<div>
<button type="submit">Sign in</button>
</div>
</div>
</form>
当我删除 http 元素中的 url 模式时,它非常有效。实际上我无法删除这两种 url 模式。我尝试删除“/customer/**”,它适用于客户登录。但是当存在 url 模式时,会发生 j_spring_security_check 404 not fount 错误。
根据spring security文档,我们可以添加多个不同url模式的http元素。
请帮我找到解决办法。
【问题讨论】:
【参考方案1】:您可以根据需要添加任意数量的 http 元素,但您还必须相应地更改登录 URL。目前,您还没有更改任何保留默认 /j_spring_security_check
的内容。而您想要/admin/j_spring_security_check
和/customer/j_spring_security_check
。
要启用此功能,您需要在 <form-login />
元素上配置 login-processing-url
,就像您指定 login-page
属性一样。对每个 http
元素执行此操作。
<security:form-login login-page="/admin/login" login-processing-url="/admin/j_spring_security_check" default-target-url="/admin/dashboard" authentication-failure-url="/admin/loginfailed" />
【讨论】:
对不起.. :) 我还有一个问题。我需要使用两个身份验证管理器对用户进行身份验证。这意味着管理员应该使用“authenticateManager1”进行身份验证,而客户应该使用“authenticateManager2”进行身份验证。当我添加两个身份验证管理器时,只有底部的一个可以正常工作。如果我将“authenticateManager1”放在“authenticateManager2”之后,则只有管理员身份验证成功。我可以为此做些什么?-以上是关于在 http 元素中使用特定的 url 模式时不调用 j_spring_security_check的主要内容,如果未能解决你的问题,请参考以下文章
配置web.config以将具有特定模式的URL重定向/转发到另一种模式(使用冒号和双斜杠)