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...即为新建成功