3 回答

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊
好的,我設(shè)法使@Ritesh的方法非常接近我想要的工作。我有一個(gè)單選按鈕,可讓用戶(hù)選擇他們是客戶(hù)還是員工??磥?lái)這種方法運(yùn)作良好,有一個(gè)問(wèn)題...
如果員工使用正確的憑據(jù)登錄,則可以在...中按預(yù)期工作。
如果員工使用錯(cuò)誤的憑據(jù)登錄,則不允許他們?cè)?..中工作。
如果客戶(hù)使用正確的憑據(jù)登錄,則可以在...中按預(yù)期工作。
如果顧客有錯(cuò)憑據(jù)登錄,認(rèn)證回落到員工的認(rèn)證...... 不起作用。這是有風(fēng)險(xiǎn)的,因?yàn)槿绻疫x擇客戶(hù)身份驗(yàn)證并將其打入員工證書(shū),它將也允許用戶(hù)進(jìn)入,這不是我想要的。
<sec:http auto-config="false" entry-point-ref="loginUrlAuthenticationEntryPoint">
<sec:logout logout-success-url="/login.jsp"/>
<sec:intercept-url pattern="/employee/**" access="ROLE_EMPLOYEE"/>
<sec:intercept-url pattern="/customer/**" access="ROLE_CUSTOMER"/>
<sec:intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<sec:custom-filter position="FORM_LOGIN_FILTER" ref="myAuthenticationFilter"/>
</sec:http>
<bean id="myAuthenticationFilter" class="ss.MyAuthenticationFilter">
<property name="authenticationManager" ref="authenticationManager"/>
<property name="authenticationFailureHandler" ref="failureHandler"/>
<property name="authenticationSuccessHandler" ref="successHandler"/>
</bean>
<bean id="loginUrlAuthenticationEntryPoint"
class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
<property name="loginFormUrl" value="/login.jsp"/>
</bean>
<bean id="successHandler"
class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
<property name="defaultTargetUrl" value="/welcome.jsp"/>
<property name="alwaysUseDefaultTargetUrl" value="true"/>
</bean>
<bean id="failureHandler"
class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
<property name="defaultFailureUrl" value="/login.jsp?login_error=1"/>
</bean>
<bean id="employeeCustomAuthenticationProvider" class="ss.EmployeeCustomAuthenticationProvider">
<property name="userDetailsService">
<bean class="ss.EmployeeUserDetailsService"/>
</property>
</bean>
<bean id="customerCustomAuthenticationProvider" class="ss.CustomerCustomAuthenticationProvider">
<property name="userDetailsService">
<bean class="ss.CustomerUserDetailsService"/>
</property>
</bean>
<sec:authentication-manager alias="authenticationManager">
<sec:authentication-provider ref="customerCustomAuthenticationProvider"/>
<sec:authentication-provider ref="employeeCustomAuthenticationProvider"/>
</sec:authentication-manager>
</beans>
這是我更新的配置。我必須做一些非常小的調(diào)整,以防止身份驗(yàn)證回退,但我現(xiàn)在似乎無(wú)法弄清楚。
謝謝。

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超3個(gè)贊
好吧,我想我已經(jīng)解決了這個(gè)問(wèn)題。無(wú)需EmployeeCustomAuthenticationProvider依賴(lài)默認(rèn)值UsernamePasswordAuthenticationToken,我EmployeeUsernamePasswordAuthenticationToken為它創(chuàng)建了它,就像為它創(chuàng)建CustomerUsernamePasswordAuthenticationToken的那樣CustomerCustomAuthenticationProvider。這些提供者將覆蓋supports():-
CustomerCustomAuthenticationProvider類(lèi)
@Override
public boolean supports(Class<? extends Object> authentication) {
return (CustomerUsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
}
EmployeeCustomAuthenticationProvider類(lèi)
@Override
public boolean supports(Class<? extends Object> authentication) {
return (EmployeeUsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
}
MyAuthenticationFilter類(lèi)
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
...
UsernamePasswordAuthenticationToken authRequest = null;
if ("customer".equals(request.getParameter("radioAuthenticationType"))) {
authRequest = new CustomerUsernamePasswordAuthenticationToken(username, password);
}
else {
authRequest = new EmployeeUsernamePasswordAuthenticationToken(username, password);
}
setDetails(request, authRequest);
return super.getAuthenticationManager().authenticate(authRequest);
}
...還有WALAA!經(jīng)過(guò)幾天的挫敗,它現(xiàn)在可以正常工作!
希望這篇文章能夠?qū)εc我在這里做同樣事情的人有所幫助。
添加回答
舉報(bào)