eclipse新建一個springsecurity項目

eclipse新建一個springsecurity項目

第一步,打開eclipse,點擊新建-->web項目

第二步,導入jar包

                      1. 加入 Spring 的 jar 包
                 2. 加入 SpringSecurity 的 jar 包
               spring-security-acl-3.1.0.M1.jar
               spring-security-config-3.1.0.M1.jar
               spring-security-core-3.1.0.M1.jar
               spring-security-taglibs-3.1.0.M1.jar
               spring-security-web-3.1.0.M1.jar

第三步,在web.xml中添加contextLoader和springsecurity的配置

<span style="font-size:12px;"> <!-- 配置 ContextLoaderListener 加載applicationContext.xml-->
  <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 配置springsecurity 的filter 其中的helloFilter是spring容器中裝配的bean-->
	<filter>
		<filter-name>helloFilter</filter-name>
		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>helloFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping></span>

第四步,在src下新建一個HelloFilter類,實現Filter接口,用於測試

@Component爲HelloFilter添加註解,將其注入到spring容器中

@Component
public class HelloFilter implements Filter{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("filter destory...");
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
                 System.out.println("[HelloFilter]doFilter...");		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("filter init...");
		
	}

第五步,配置spring的配置文件(eg:applicationContext.xml)

applicationCintext.xml

<span style="font-weight: normal;"><p><span style="font-size:18px;"><beans xmlns="http://www.springframework.org/schema/beans"
</span></p><p><span style="font-size:18px;">	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:security="http://www.springframework.org/schema/security"
	xsi:schemaLocation="http://www.springframework.org/schema/security 
	           http://www.springframework.org/schema/security/spring-security-3.1.xsd
		   http://www.springframework.org/schema/beans
		   http://www.springframework.org/schema/beans/spring-beans.xsd
		   http://www.springframework.org/schema/context 
		   http://www.springframework.org/schema/context/spring-context-4.0.xsd"></span>
</p></span>

a),在applicationcontext.xml中添加掃描註解

<span style="font-weight: normal;"><context:component-scan base-package="com.shawsuper.securityfilter.HelloFilter"></context:component-scan></span>

第六步,在eclipse的tomcat上部署運行,當你訪問你的項目時,如果控制檯控制檯輸出[HelloFilter]doFilter...即爲新建成功






















發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章