使用Spring Security進行http Basic認證很是簡單,直接配置便可使用,以下:ide
<security:http> <security:http-basic></security:http-basic> <security:intercept-url pattern="/**" access="ROLE_USER"/> </security:http> <!--使用AuthenticationManager 進行認證相關配置--> <!--authentication-manager元素指定了一個AuthenticationManager,其須要一個AuthenticationProvider(對應authentication-provider元素)來進行真正的認證--> <security:authentication-manager> <security:authentication-provider> <security:user-service> <security:user name="user" password="user" authorities="ROLE_USER"/> <security:user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN"/> </security:user-service> </security:authentication-provider> </security:authentication-manager>