在spring mvc web module中添加使用spring security 模塊,需要注入用戶驗證模塊,可是加入配置的驗證模塊後,其中獲取用戶驗證的部分沒有被執行,反而老是執行匿名過濾,想問一下這個到底是什麼問題?
配置代碼:
<security:http auto-config="true" use-expressions="true" access-denied-page="/auth/denied" >
<security:intercept-url pattern="/services/auth/admin" access="hasRole('ROLE_ADMIN')"/>
<security:intercept-url pattern="/services/auth/userauth" access="hasRole('ROLE_USER')"/>
<security:form-login
login-page="/services/auth/login"
authentication-failure-url="/services/auth/login_error?error=true"
default-target-url="/services/auth/admin"/>
<security:logout
invalidate-session="true"
logout-success-url="/auth/login"
logout-url="/auth/logout"/>
</security:http>
<security:authentication-manager>
<security:authentication-provider user-service-ref="userService">
</security:authentication-provider>
</security:authentication-manager>
<bean id="userService"
class="org.test.spring.mvc.web.impl.UserServiceImpl"/>
其中的userservice類是implement UserDetailsService 接口的
public class UserServiceImpl implements UserDetailsService{
}
按照步驟來說在登錄頁面,輸入username 和password以後,應該會進入此類進行用戶判斷,但是不知為何,沒有進入此類。login 頁面的提交是post請求到../services/auth/userauth.
請大家幫忙看一下原因,謝謝。
沒人回答,只能自己debug,最終發現負責用戶驗證的那個過濾器:org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter,在實現接口方法authenticate的時候,url模式必須嚴格符合/contextPath/j_spring_security_check.
這樣的話我在web.xml中聲明的filter 也會因為這個url格式不起作用,最初的設置是
<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>/services/*</url-pattern>
</filter-mapping>
要修改pattern為
<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>
當然可以自定制自己的providerManager 來實現url重新判定,但是我只想用一下其中的用戶驗證功能,太麻煩,下次再看。