了解了web.xml配置文件中各个节点的作用及运行机制后,从实战入手,解析一下实际项目的基本配置,对项目的整体结构做一些了解,避免闷头开发功能而对项目一知半解。
1、配置web项目名称
<display-name>test-web</display-name>
此配置为web项目的名称,tomcat启动后,可以在管理页面看到应用名称http://localhost:8080/manager/html。
此处还有一个小问题,登陆管理页面需要修改tomcat安装目录下conf/tomcat-user.xml文件,根据页面提示加入如下配置
<role rolename="manager-gui"/>
<user username="" password="" roles="manager-gui"/>
2、配置项目路径
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webApp.root</param-value>
</context-param>
<param-name>中的webAppRootKey为项目绝对路径,默认的<param-value>为webApp.root。部署在同一容器中的不同项目,<param-value>不能相同,否则会报错。
该配置需要org.springframework.web.util.WebAppRootListener监听器来注入项目路径,且需要在监听器org.springframework.web.context.ContextLoaderListener之前,否则ApplicationContext的bean注入根目录值时会发生无法注入异常。
3、配置日志
<context-param>
<param-name>logbackConfigLocation</param-name>
<param-value>classpath:logback.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
该配置为web项目中的日志配置,<param-value>的值即是日志配置文件。
org.springframework.web.util.Log4jConfigListener监听器,除了实现日志功能,还一并实现了监听器org.springframework.web.util.WebAppRootListener的功能,所以只需配置日志监听器即可。
4、配置spring
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:servlet-context.xml,classpath:root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
该配置为spring的相关配置,<param-value>的值即为spring配置文件,包含多个配置文件时用逗号分隔。
监听器org.springframework.web.context.ContextLoaderListener可以在启动容器时读取之前配置的spring配置文件。
监听器org.springframework.web.util.IntrospectorCleanupListener是为了防止发生内存泄漏而做的配置,必须写在读取spring配置文件之前。(没有实际应用过,后续需要看源码)
5、配置编码格式
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
org.springframework.web.filter.CharacterEncodingFilter是spring提供的一种过滤器,用来指定请求或者响应的编码格式。
<param-name>encoding</param-name>下面对应的<param-value>UTF-8</param-value>即是编码格式。
还可以额外设置<param-name>forceEncoding</param-name>,强制response的编码格式也是encoding格式。
过滤器映射中<url-pattern>/*</url-pattern>意为匹配所有请求。
6、配置spring-mvc
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
org.springframework.web.servlet.DispatcherServlet是前置控制器,提供Spring Web MVC的集中访问点,也是spring-mvc的第一步,可以配置<param-value>来设置初始化上下文的文件。
之前已经配置的上下文初始化参数使用了org.springframework.web.context.ContextLoaderListener来读取,并且整个应用程序共享。而此处初始化的上下文加载的bean只对spring web mvc有效,二者之间是继承关系。(不太明白,后续需要查看源码)
7、集成CAS
①配置单点登出
<listener>
<listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
</listener>
<filter>
<filter-name>CAS Single Sign Out Filter</filter-name>
<filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CAS Single Sign Out Filter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CAS Single Sign Out Filter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
监听器org.jasig.cas.client.session.SingleSignOutHttpSessionListener实现了HttpSessionListener接口,用于监听session销毁事件。
如果有配置CAS client Filter,则CAS Single Sign Out Filter 必须要放到CAS client Filter之前。
配置的目的是在CAS server回调所有的application进行单点登出操作的时候,需要这个filter来实现session的清除。
②配置单点登入
<filter>
<filter-name>CAS Filter</filter-name>
<filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
<init-param>
<param-name>casServerLoginUrl</param-name>
<param-value>http://CAS服务端所在服务器IP:8080/cas/login</param-value>
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>http://127.0.0.1:8080</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CAS Filter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CAS Filter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
org.jasig.cas.client.authentication.AuthenticationFilter可以判断客户是否已经登陆,若未登录,则重定向到CAS服务器的登陆地址,即<param-name>casServerLoginUrl</param-name>下<param-value>中的地址。
<param-name>serverName</param-name>下<param-value>中的地址,为具体应用的地址。
③、配置Ticket校验
<filter>
<filter-name>CAS Validation Filter</filter-name>
<filter-class>org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
<init-param>
<param-name>casServerUrlPrefix</param-name>
<param-value>http://CAS服务端所在服务器IP:8080/cas</param-value>
</init-param>
<init-param>
<param-name>serverName</param-name>
<param-value>http://127.0.0.1:8080</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CAS Validation Filter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CAS Validation Filter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
org.jasig.cas.client.util.HttpServletRequestWrapperFilter过滤器负责对Ticket的校验,属于必须启用的过滤器。
④可选配置
<filter>
<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
<filter-class>org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
org.jasig.cas.client.util.HttpServletRequestWrapperFilter过滤器负责实现HttpServletRequest请求的包裹,比如允许开发者通过HttpServletRequest的getRemoteUser()方法获得SSO登录用户的登录名。
<filter>
<filter-name>CAS Assertion Thread Local Filter</filter-name>
<filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CAS Assertion Thread Local Filter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CAS Assertion Thread Local Filter</filter-name>
<url-pattern>/</url-pattern>
</filter-mapping>
org.jasig.cas.client.util.AssertionThreadLocalFilter过滤器使得开发者可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。比如AssertionHolder.getAssertion().getPrincipal().getName()。
8、页面配置
<session-config>
<session-timeout>180</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>/index</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/views/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/views/error.jsp</location>
</error-page>
<session-config>是对session的配置,<session-timeout>为session超时时间。
<welcome-file-list>是欢迎页面,用户在url中输入工程名称或者输入web容器url(如http://localhost:8080/)时直接跳转的页面。
<error-page>是错误页面,可根据不同的<error-code>来判断发生了何种错误,需要跳转到什么错误页面。
9、安全防护
<security-constraint>
<web-resource-collection>
<web-resource-name>cspadmin</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>PUT</http-method>
<http-method>DELETE</http-method>
<http-method>HEAD</http-method>
<http-method>TRACE</http-method>
</web-resource-collection>
<auth-constraint></auth-constraint>
</security-constraint>
<security-constraint>节点可以允许不通过编程就可以限制对某个资源的访问。
<web-resource-collection>标识需要限制访问的资源子集。可以定义URL模式和HTTP方法。如果不存在HTTP方法,就将安全约束应用于所有的方法。<web-resource-name>是与受保护资源相关联的名称。http-method元素可被赋予一个HTTP方法。
<auth-constraint>用于指定可以访问该资源集合的用户角色。如果没有指定auth-constraint元素,就将安全约束应用于所有角色。
此处配置的实际意义是禁用了所有不安全的HTTP请求方式。