<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd "> <!-- auto-config:表示自动加载SpringSecurity的配置文件 use-expressions:表示使用Spring的EL表达式 --> <security:http auto-config="true" use-expressions="true"> <!-- 配置匿名访问登录页面(须在拦截资源之前配置)--> <security:intercept-url pattern="/login.jsp" access="permitAll()"/> <!-- 拦截资源 pattern="/**" 表示拦截所有的资源 access="hasAnyRole(‘ROLE_USER‘)" 表示只有ROLE_USER这个角色可以访问资源 --> <security:intercept-url pattern="/**" access="hasAnyRole(‘ROLE_USER‘)" /> <!--配置认证信息--> <!--<security:form-login login-page="/login.jsp" login-processing-url="/login" default-target-url="/home.jsp" authentication-failure-url="/error.jsp" />--> <!--配置注销--> <security:logout logout-url="/logout" logout-success-url="/login.jsp" /> <!--关闭CSRF拦截--> <!--<security:csrf disabled="true" />--> </security:http> <!-- 认证用户信息 --> <security:authentication-manager> <security:authentication-provider> <security:user-service > <security:user name="zhangsan" authorities="ROLE_USER" password="{noop}123" /> <security:user name="lisi" authorities="ROLE_USER" password="{noop}456" /> </security:user-service> </security:authentication-provider> </security:authentication-manager> </beans>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- 初始化spring容器 --> <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> <!-- post乱码过滤器 --> <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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 前端控制器 --> <servlet> <servlet-name>dispatcherServletb</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServletb</servlet-name> <!-- 拦截所有请求jsp除外 --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置过滤器链 filter-name名称固定为springSecurityFilterChain --> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
1