springSecurity小试牛刀

1、创建Maven工程

暂时打成war包

2、导入pom.xml文件约束


  4.0.0
  com.yyd.demo
  spring-security-demo
  0.0.1-SNAPSHOT
  war
 	 
4.2.4.RELEASE 
 
 	 
 	 	 
 	 	 	org.springframework 
 	 	 	spring-core  	 	 	
 	 	 	${spring.version} 
 	 	 
 	 	 
 	 	 	org.springframework 
 	 	 	spring-web 
 	 	 	${spring.version} 
 	 	 
 	 	 
 	 	 	org.springframework 
 	 	 	spring-webmvc 
 	 	 	${spring.version} 
 	 	 
 	 	 
 	 	 	org.springframework 
 	 	 	spring-context-support 
	 	${spring.version} 
 
 
 
	 	 	org.springframework 
 	 	 	spring-test  	 	 	
 	 	 	${spring.version} 
 	 	 
 	 	 
 	 	 	org.springframework 
 	 	 	spring-jdbc  	 	 	
 	 	 	${spring.version} 
 	 	 
 	 	 
 	 	 	org.springframework.security 
 	 	 	spring-security-web 
 	 	 	4.1.0.RELEASE 
 	 	 
 	 	 
 	 	 	org.springframework.security 
 	 	 	spring-security-config 
 	 	 	4.1.0.RELEASE 
 	 	 
 	 	 
	 	javax.servlet 
 			servlet-api  	
 			2.5 
	 	 	provided 
 	 	 
 	 
 	 
 	    	 
 	       
 	 	   
 	 	 	 	org.apache.maven.plugins 
 	 	 	 	maven-compiler-plugin 
 	 	 	 	3.2 
 	 	 	 	 
 	 	 	 	 	1.7 
 	 	 	 	 	1.7 
 	 	 	 	 	UTF-8 
 	 	 	 	 
 	 	         
 	       
 	 	 	 	org.apache.tomcat.maven 
 	 	 	 	tomcat7-maven-plugin 
 	 	 	 	 
	 	 	 	 
	 	 	 	9090 
 	 	 	 	 	 
 	 	 	 	 	/ 
 	 	 	 	 
 	      
 	      
     
  

创建web.xml文件

springSecurity小试牛刀_第1张图片


	
   
	
		CharacterEncodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			utf-8
		
		  
            forceEncoding  
            true  
          
	
	
		CharacterEncodingFilter
		/*
		
	
  
  	springmvc
  	org.springframework.web.servlet.DispatcherServlet
  	
  	
  		contextConfigLocation
  		classpath:spring/*.xml
  	
  	1
  
  
  
  	springmvc
  	/
  
  
  
  	springSecurityFilterChain
  	org.springframework.web.filter.DelegatingFilterProxy
  
  
  	springSecurityFilterChain
  	/*
  
	

创建配置文件

springmvc.xml



	
	

spring-security.xml








	
		
		
		
		
		
	


	
		
			
		
	


form-login属性详解

  1. login-page 自定义登录页url,默认为/login
  2. login-processing-url 登录请求拦截的url,也就是form表单提交时指定的action
  3. default-target-url 默认登录成功后跳转的url
  4. always-use-default-target 是否总是使用默认的登录成功后跳转url
  5. authentication-failure-url 登录失败后跳转的url
  6. username-parameter 用户名的请求字段 默认为userName
  7. password-parameter 密码的请求字段 默认为password
  8. authentication-success-handler-ref 指向一个AuthenticationSuccessHandler用于处理认证成功的请求,不能和default-target-url还有 always-use-default-target同时使用
  9. authentication-success-forward-url 用于authentication-failure-handler-ref
  10. authentication-failure-handler-ref 指向一个AuthenticationFailureHandler用于处理失败的认证请求
  11. authentication-failure-forward-url 用于authentication-failure-handler-ref
  12. authentication-details-source-ref 指向一个AuthenticationDetailsSource,在认证过滤器中使用

创建html文件

1、index.html





欢迎访问首页


欢迎,欢迎,热烈欢迎!!!

2、login.html





品优购登录页面


	
用户名:
密 码:

3、error.html





恭喜你,你访问到错误页面了


亲,你访问页面丢失了!

导入favicon.ico

问题解决

严重: 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#2' while setting bean property 'sourceList' with key [2]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#2': Cannot resolve reference to bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0' while setting constructor argument with key [3]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0': Cannot create inner bean '(inner bean)#5b2617e6' of type [org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler] while setting bean property 'authenticationSuccessHandler'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#5b2617e6': Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'defaultTargetUrl' threw exception; nested exception is java.lang.IllegalArgumentException: defaultTarget must start with '/' or with 'http(s)'
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:382)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:157)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1481)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:753)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
	at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
	at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
	at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
	at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
	at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
	at javax.servlet.GenericServlet.init(GenericServlet.java:160)
	at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
	at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
	at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
	at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#2': Cannot resolve reference to bean 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0' while setting constructor argument with key [3]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0': Cannot create inner bean '(inner bean)#5b2617e6' of type [org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler] while setting bean property 'authenticationSuccessHandler'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#5b2617e6': Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'defaultTargetUrl' threw exception; nested exception is java.lang.IllegalArgumentException: defaultTarget must start with '/' or with 'http(s)'
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:382)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:157)
	at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:634)
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:140)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
	... 33 more

springSecurity小试牛刀_第2张图片
springSecurity小试牛刀_第3张图片
在spring-security.xml文件中加入

你可能感兴趣的:(springSecurity小试牛刀)