一、前言
Spring Security系列教程中,前五篇為同一人所寫,而本文是博主依據第三方文章整合而出,與前五篇文章的作者不是同一系列。
但本文以前五篇文章為基礎,在前面文章所建立的Spring Security的基礎上,整合SpringMVC框架。
二、配置文件
spring-mvc.xml配置文件的存放位置可根據實際情況進行調整配置,如可以放在resource文件夾中,對應的web.xml配置classpath:spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 啟動注解驅動的Spring MVC功能,注冊請求url和注解POJO類方法的映射--> <mvc:annotation-driven /> <!-- 啟動包掃描功能,以便注冊帶有@Controller、@Service、@repository、@Component等注解的類成為spring的bean --> <context:component-scan base-package="com.mvc.test" /> <!--這個包根據自己的項目來配置,我的是com.mvc.test--> <!-- 對模型視圖名稱的解析,在請求時模型視圖名稱添加前后綴 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/" p:suffix=".jsp" /> </beans> 然后配置web.xml: <context-param> <param-name>contextConfigLocation</param-name> <!-- 應用上下文配置文件 --> <param-value>/WEB-INF/spring-mvc.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置spring核心servlet --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- url-pattern配置為/ 攔截 --> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
web.xml
然后我們需要一個spring security 的過濾器配置、spring mvc Servlet完整配置的web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <!-- 應用上下文配置文件 --> <param-value>/WEB-INF/spring-servlet.xml,/WEB-INF/applicationContext-security.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- spring securit start --> <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> <!-- spring securit start --> <!-- 配置spring核心servlet --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- url-pattern配置為/,不帶文件后綴,會造成其它靜態文件(js,css等)不能訪問。如配為*.do,則不影響靜態文件的訪問 --> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
三、添加Controller
@Controller @Request("/student") public class StudentController { @RequestMapping("/index") public ModelAndView index() { ModelAndView mav = new ModelAndView(); mav.setViewName("student/index"); return mav; } }
四、添加student的index頁面
在WEB-INF/student文件夾下添加index.jsp頁面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>This is news index Page</h1> </body> </html>
五、在項目首頁index.jsp中添加跳轉到student/index.jsp頁面的連接
<a href="${pageContext.request.contextPath }/student/index">goto student</a>
六、數據庫中輸入數據
在數據庫resc表中,添加數據,其中res_string字段的值為/student/*
insert into resc values(null,'','URL','/student/*','學生頁面');
並在對應的角色-資源中間表中插入數據,關聯user角色和admin角色:
七、測試
輸入:絕對路徑/student/index會自動跳到登錄頁面,必須先登錄才能訪問連接。
Reference:
Beyond-bit, SpringMVC 3.1集成Spring Security 3.1, https://www.cnblogs.com/Beyond-bit/p/SpringMVC_And_SpringSecurity.html