學習Spring Security的過程中遇到的問題彙總
首先分享兩個學習Spring security的資源:http://www.mossle.com/docs/auth/html/index.html (寫成於2009年,較老,但是對於初學者還是很有幫助的)
http://www.iteye.com/blogs/subjects/spring_security (完成於2014年-2016年間,且博主仍在更新,比較詳細,適合有一定基礎的朋友)
以上兩個資源都是對Spring security成體系學習的好東西,分享給有意學習的朋友。
1、打出的錯誤資訊如下:
解決辦法如下:專案的properties -> Deployment Assembly -> Add -> Java Build Path Entries -> next -> Maven Dependencies -> apply ,問題解決。嚴重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4153) at org.apache.catalina.core.StandardContext.start(StandardContext.java:4709) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1060) at org.apache.catalina.core.StandardHost.start(StandardHost.java:822) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1060) at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463) at org.apache.catalina.core.StandardService.start(StandardService.java:525) at org.apache.catalina.core.StandardServer.start(StandardServer.java:759) at org.apache.catalina.startup.Catalina.start(Catalina.java:595) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
在解決問題的過程中發現有很多情況會導致如上問題,但是大多數情況下還是在build和deploy階段沒有正確的姿勢導包。
2、錯誤資訊如下:
嚴重: Context initialization failed org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: You cannot use a spring-security-2.0.xsd or spring-security-3.0.xsd or spring-security-3.1.xsd schema with Spring Security 3.2. Please update your schema declarations to the 3.2 schema. Offending resource: ServletContext resource [/WEB-INF/config/spring-security.xml]
我的spring-security.xml檔案如下:
Spring 和 Spring Security版本資訊如下:<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.1.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"> <security:http auto-config="true"> <security:intercept-url pattern="/**" access="ROLE_USER" /> </security:http> <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> </beans>
<properties>
<org.springframework.security.version>3.2.3.RELEASE</org.springframework.security.version>
<org.springframework.version>4.0.4.RELEASE</org.springframework.version>
</properties>
stackoverflow 大神的說法如下:
The schema you are pointing to is spring-security-3.1.xsd, but the pom.xml declares spring-security-config-3.0.1.RELEASE.
You should update your version of Spring Security or downgrade the version of the spring security namespace. Note when changing versions ensure that all Spring versions match and all Spring Security versions match (this includes transitive dependencies) otherwise
you will get strange errors.
http://stackoverflow.com/questions/8916993/spring-security3-you-cannot-use-a-spring-security-2-0-xsd-schema
解決辦法如下:在pom檔案中,將spring-security版本降至至多3.2.0(不包含),或者提升spring-security 頭部中的schema版本,但是沒試過這個方法,反正我把spring security的版本降至3.1.3就可以了。
3、安裝mysql後報錯:ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)
進入計算機 右鍵 -> 管理 -> 服務和應用程式 -> 服務如果找不到mysql的話是沒安裝成功,可以試試mysql -install。如果找到mysql點選啟動的時候報錯如下:
4.報錯資訊如下:
嚴重: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.filterChains': Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#0' while setting bean property 'sourceList' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#0': Cannot resolve reference to bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0' while setting constructor argument with key [2]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0': Cannot resolve reference to bean 'org.springframework.security.authentication.ProviderManager#0' while setting bean property 'authenticationManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.ProviderManager#0': Cannot resolve reference to bean 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.authentication.AuthenticationManagerFactoryBean#0': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authenticationManager': Cannot resolve reference to bean 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0' while setting constructor argument with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authentication.dao.DaoAuthenticationProvider#0': Cannot resolve reference to bean 'org.springframework.security.provisioning.JdbcUserDetailsManager#0' while setting bean property 'userDetailsService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.provisioning.JdbcUserDetailsManager#0': Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/config/spring-security.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.jdbc.Driver]
解決辦法:在pom.xml中加入如下資訊:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>