網頁

2018/3/12

Spring MVC整合Spring Security基本設定

本篇介紹Spring Security搭配Spring MVC的基本設定。


首先建立一個Spring MVC專案


接著以下開始加入Spring Security的相關設定。

開啟pom.xml加入Spring Security的依賴設定。Spring MVC要加入spring-security-web還有spring-security-config的Maven設定如下。

<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-web</artifactId>
  <version>4.2.4.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-config</artifactId>
  <version>4.2.4.RELEASE</version>
</dependency>

加入依賴後的pom.xml設定如下。

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>idv.matt</groupId>
  <artifactId>springmvc</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>springmvc Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.14.RELEASE</version>
    </dependency>
    <!-- Spring Security -->
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-web</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-config</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>springmvc</finalName>
  </build>
</project>

接著在web.xml加入filter org.springframework.web.filter.DelegatingFilterProxy及加入ContextLoaderListener來設定Spring Security設定檔的位置。

web.xml

<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_3_1.xsd"
         version="3.1">
  <display-name>Spring MVC Project</display-name>
  
   <welcome-file-list>
     <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>

  <!-- Spring Security config file location -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value> <!-- Spring Security配置檔改在classpath下的applicationContxt.xml -->
  </context-param>

  <!-- Spring Security -->
  <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 MVC DispatcherServlet -->
  <servlet>
    <servlet-name>dispatcher</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> <!-- Spring MVC配置檔改在classpath下的spring-mvc.xml -->
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
</web-app>

接著在classpath中用來存放靜態資源的src/main/resources下新增Spring Security的配置檔applicationContext.xml。注意security schema的宣告。

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:security="http://www.springframework.org/schema/security"
    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/security 
        http://www.springframework.org/schema/security/spring-security.xsd">
        
        
  <!-- Spring Security -->
  <security:http auto-config='true'>
    <security:intercept-url pattern="/**" access="hasRole('ROLE_ADMIN')"/>
  </security:http>
  
  <security:authentication-manager>
    <security:authentication-provider>
      <security:user-service>
        <security:user name="matt" password="matt" authorities="ROLE_ADMIN"/>
      </security:user-service>
    </security:authentication-provider>
  </security:authentication-manager>
  
</beans:beans>

完成以上後的專案目錄結構如下



啟動專案後,就會要求你輸入帳號密碼,根據設定User欄位輸入matt,Password欄位輸入matt



通過驗證後便會導向web.xml<welcome-file>設定的首頁index.jsp


參考:

沒有留言:

張貼留言