1
我建立一個項目,提供2個登錄表單,一個用於member
,另一個用於agent
,他們兩人都是不同的,所以,我有2個表(member
和agent
)。我想用不同的映射爲他們製作登錄表單。 /pages/agent
是表格agent
和/
(index.html
的項目)是member
。 /pages
是我的調度員。我嘗試了很多方法,但都沒有成功。春季安全3.1多形式的登錄和認證
這裏是我的安全上下文:
<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">
<http pattern="/pages/*" authentication-manager-ref="agentAuth">
<intercept-url pattern="/pages/agentprofile*" access="ROLE_AGENT" />
<form-login login-page="/pages/agent" default-target-url="/pages/agentprofile" />
<logout logout-success-url="/pages/logout" logout-url="/pages/j_spring_security_logout" delete-cookies="JSESSIONID" />
</http>
<authentication-manager alias="agentAuth" id="agentAuth">
<authentication-provider >
<password-encoder hash="md5" />
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query="select username, password, enable from agent where USERNAME=?"
authorities-by-username-query="select a.username, r.namaRole from agent a, role r where r.idRole = a.idRole and a.username = ?" />
</authentication-provider>
</authentication-manager>
<http authentication-manager-ref="memberAuth">
<intercept-url pattern="/pages/member*" access="ROLE_USER" />
<intercept-url pattern="/pages/myorder*" access="ROLE_USER" />
<intercept-url pattern="/pages/voucherbelanja*" access="ROLE_USER" />
<intercept-url pattern="/pages/rewardpoint*" access="ROLE_USER" />
<intercept-url pattern="/pages/myreview*" access="ROLE_USER" />
<intercept-url pattern="/pages/voucherhotel*" access="ROLE_USER" />
<form-login login-page="/" default-target-url="/pages/member"
authentication-failure-url="/pages/loginfailed" />
<form-login login-page="/pages/loginfailed"
default-target-url="/pages/member" authentication-failure-url="/pages/loginfailed"
authentication-success-handler-ref="loginSucessHandler" />
<logout logout-success-url="/pages/logout" logout-url="/pages/j_spring_security_logout" delete-cookies="JSESSIONID"/>
</http>
<authentication-manager alias="memberAuth" id="memberAuth">
<authentication-provider>
<password-encoder hash="md5" />
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query="select username, password, enable from member where USERNAME=?"
authorities-by-username-query="select m.username, r.namaRole from member m, role r where r.idRole = m.idRole and m.username = ?" />
</authentication-provider>
</authentication-manager>
<beans:bean id="loginSuccessHandler" class="com.klik.service.LoginSuccessHandler" />
下面是在web.xml我的春季安全過濾器:
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
<url-pattern>/pages/*</url-pattern>
</filter-mapping>
我認爲問題是JSESSIONID剛創建一次,當你成功登錄,而不是每個標籤。所以當我嘗試打開url/agentprofile與成員已成功登錄時,彈簧安全不會攔截它,並給予訪問被拒絕的頁面,因爲它被檢測爲role_member而不是role_agent。真實的條件不會被拒絕,但它應該給代理人的登錄表單。問題仍未解決。 – user2732201
我的最終工作解決方案是爲2個http配置設置不同的領域,如「代理」和「用戶」。對於用戶,默認情況下,登錄網址爲「user/j _ *** _ check」,登出網址爲'user/j *** _ logout'。如果我想獲取登錄用戶的用戶信息,則URL應以「/ user」開頭。而且我還需要爲2個http配置設置模式。您爲代理設置** pattern =「/ pages/*」**。並且此網址也可以匹配用戶的網址。所以我認爲用戶相關的安全配置不能工作。 – Mavlarn