spring_security的demo演示

spring security簡介

Spring Security是一個能夠爲基於Spring的企業應用系統提供聲明式的安全訪問控制解決方案的安全框架。它提供了一組可以在Spring應用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反轉Inversion of Control ,DI:Dependency Injection 依賴注入)和AOP(面向切面編程)功能,爲應用系統提供聲明式的安全訪問控制功能,減少了爲企業系統安全控制編寫大量重複代碼的工作。
創建一個war工程spring_security_demo

使用spring_security生成的登陸頁面

配置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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.senqi</groupId>
  <artifactId>spring_security_demo</artifactId>
  <version>1.0.0</version>
  <packaging>war</packaging>

  <properties>
    <webVersion>3.0</webVersion>
    <spring.version>4.2.4.RELEASE</spring.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-web</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-config</artifactId>
      <version>${spring.version}</version>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <port>9000</port>
          <path>/</path>
          <uriEncoding>utf-8</uriEncoding>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>springwithspringmvc</display-name>
  <!-- 指定spring-security的配置文件的路徑和名稱  -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-security.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<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>

配置spring-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
             xmlns:beans="http://www.springframework.org/schema/beans"
             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">

    <!-- 配置 favicon.ico不進行安全攔截 -->
    <http pattern="/favicon.ico" security="none" />

    <!-- 頁面攔截規則 -->
    <http use-expressions="false">
        <intercept-url pattern="/**" access="ROLE_USER" />
        <form-login /><!--spring-security創建一個登陸頁面-->
    </http>

    <!-- 認證管理器 -->
    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="admin" password="123456" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>

</beans:beans>

配置說明:
intercept-url 表示攔截頁面
/* 表示的是該目錄下的資源,只包括本級目錄不包括下級目錄
/** 表示的是該目錄以及該目錄下所有級別子目錄的資源
form-login 開啓表單登陸,可以設置一些屬性
login-processing-url:設置頁面登錄的url
password-parameter:表單密碼的name
username-parameter:表單用戶名的name
use-expressions 爲是否使用使用 Spring 表達式語言( SpEL ),默認爲true , 如果開啓,則攔截的配置應該寫成以下形式

<intercept-url pattern="/**" access="hasRole(‘ROLE_USER’)" />

ROLE_USER:必須有ROLE_USER角色纔可以訪問根目錄及子目錄(角色大寫,以ROLE_開頭)

再創建一個登陸成功的index.html
到此爲止spring_security_demo就創建完成,我們運行項目,訪問http://localhost:9000/index.html會被攔截,轉發到spring_security創建的登陸頁面
在這裏插入圖片描述
隨意輸入用戶名和密碼,登陸失敗
在這裏插入圖片描述
在這裏插入圖片描述
我們輸入spring-security.xml裏配置好的用戶名和密碼進行測試,登陸成功跳轉到index.html頁面
在這裏插入圖片描述
在這裏插入圖片描述

使用自己創建的登陸頁面

創建一個login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登陸頁面</title>
</head>
<body>
<!-- login:spring security生成 -->
<form action='/login' method='post'>
    <table>
        <tr>
            <td>用戶名:</td>
            <td><input type='text' name='username'></td>
        </tr>
        <tr>
            <td>密碼:</td>
            <td><input type='password' name='password'/></td>
        </tr>
        <tr>
            <td colspan='2'><input name="submit" type="submit" value="登陸"/></td>
        </tr>
    </table>
</form>
</body>
</html>

在這裏插入圖片描述代碼中的/login請求路徑是spring-security生成的,相當於spring-security爲我們寫好了一個controller,訪問路徑是/login提交方式爲post,提交的形參爲username和password
類似於

@RequestMapping("/login")
public User login(String username,String password){
spring-security方法
}

重新配置spring-security.xml的form-login 標籤

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
             xmlns:beans="http://www.springframework.org/schema/beans"
             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">

    <!-- 配置 favicon.ico不進行安全攔截 -->
    <http pattern="/favicon.ico" security="none" />
    <!--放行自定義登錄頁面-->
    <http pattern="/login.html" security="none" />

    <!-- 頁面攔截規則 -->
    <http use-expressions="false">
        <intercept-url pattern="/**" access="ROLE_USER" />
        <form-login login-page="/login.html" default-target-url="/index.html"
                    authentication-failure-url="/login.html"/><!--自定義登錄頁面-->
        <csrf disabled="true"/>
    </http>

    <!-- 認證管理器 -->
    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="admin" password="123456" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>

</beans:beans>

security=“none” 設置此資源不被攔截
如果你沒有設置登錄頁security=“none” ,將會出現以下錯誤
在這裏插入圖片描述
因爲登錄頁會被反覆重定向。
login-page:指定登錄頁面。
authentication-failure-url:指定了身份驗證失敗時跳轉到的頁面。
default-target-url:指定了成功進行身份驗證和授權後默認呈現給用戶的頁面。
csrf disabled=“true” 關閉csrf ,如果不關閉會出現錯誤(我們使用的是html頁面)

CSRF(Cross-site request forgery)跨站請求僞造,也被稱爲“One Click Attack”或者Session Riding,通常縮寫爲CSRF或者XSRF,是一種對網站的惡意利用。spring security默認開啓該功能,在jsp頁面利用csrf_token來防止跨站僞造。
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章