Spring Security框架入門

1.Spring Security框架入門

1.1 Spring Security簡介

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

1.2 Spring Security入門小Demo

1.2.1最簡單Demo

1)建立工程spring-security-demo ,pom.xml內容java

<properties>web

<spring.version>4.2.4.RELEASE</spring.version>spring

</properties>express

<dependencies>apache

<dependency>編程

<groupId>org.springframework</groupId>api

<artifactId>spring-core</artifactId>tomcat

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

<artifactId>spring-test</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>${spring.version}</version>

</dependency>

<dependency>

<groupId>org.springframework.security</groupId>

<artifactId>spring-security-web</artifactId>

<version>4.1.0.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework.security</groupId>

<artifactId>spring-security-config</artifactId>

<version>4.1.0.RELEASE</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<version>2.5</version>

<scope>provided</scope>

</dependency>

</dependencies>

<build>

  <plugins>

      <!-- java編譯插件 -->

  <plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.2</version>

<configuration>

<source>1.7</source>

<target>1.7</target>

<encoding>UTF-8</encoding>

</configuration>

  </plugin>      

      <plugin>

<groupId>org.apache.tomcat.maven</groupId>

<artifactId>tomcat7-maven-plugin</artifactId>

<configuration>

<!-- 指定端口 -->

<port>9090</port>

<!-- 請求路徑 -->

<path>/</path>

</configuration>

    </plugin>

   </plugins>  

    </build>

 

2)建立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_2_5.xsd"

version="2.5">

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

 

3)建立index.html  

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>首頁</title>

</head>

<body>

歡迎進入神奇的spring security世界~~~

</body>

</html>

 

4)建立spring 配置文件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">

<!-- 頁面攔截規則 -->

<http use-expressions="false">

<intercept-url pattern="/**" access="ROLE_USER" />

<form-login/>

</http>

<!-- 認證管理器 -->

<authentication-manager>

<authentication-provider>

<user-service>

<!-- 登陸帳戶密碼 -->

<user name="admin" password="123456" authorities="ROLE_USER"/>

</user-service>

</authentication-provider>

</authentication-manager>

</beans:beans>

 

IDEA啓動tomcat顯示build success但沒法繼續啓動的解決方法

 

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <configuration>
        <ignorePackaging>true</ignorePackaging>
        <!-- 指定端口 -->
        <port>9090</port>
        <!-- 請求路徑 -->
        <path>/</path>
    </configuration>
</plugin>

 

告訴Tomcat插件,當前不是使用war包裝。在pom.xmltomcat pluginconfiguration中加入<ignorePackaging>true</ignorePackaging>,即告訴tomcat啓動時忽略當前是否有war包,再次運行tomcat7:run命令,tomcat便可正常啓動。

 

access="ROLE_USER" access 必須是ROLE_開頭

此案例咱們沒有登陸頁,而是使用了系統自動生成的登錄頁,效果以下:

 

 

 

 

配置說明:

intercept-url 表示攔截頁面   

/*  表示的是該目錄下的資源,只包括本級目錄不包括下級目錄

/** 表示的是該目錄以及該目錄下全部級別子目錄的資源

form-login  爲開啓表單登錄

use-expressions 爲是否使用使用 Spring 表達式語言( SpEL ,默認爲true ,若是開啓,則攔截的配置應該寫成如下形式

login-processing-url=」/login2」 指定其餘登陸頁

username-parameter="" password-parameter="" 來改寫login的 用戶名和屬性名

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

 

1.2.2用戶自定義登陸頁

實際開發中,咱們不可能使用系統生成的登陸頁,而是使用咱們本身的登陸頁。

(1)構建登錄頁login.html

 <!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>登錄</title>

</head>

<body>

 

--歡迎的登錄個人系統--

<form action="/login" method="post">

用戶名:<input name="username"><br>

密碼:<input name="password"><br>

<button>登錄</button>

</form>

</body>

</html>

 

(2)構建登錄失敗頁  login_error.html

 <!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>首頁</title>

</head>

<body>

用戶名或密碼錯誤~~~

</body>

</html>

 

3)修改 spring 配置文件spring-security.xml  

<!-- 如下頁面不被攔截 -->

<http pattern="/login.html" security="none"></http>

<http pattern="/login_error.html" security="none"></http>

<!-- 頁面攔截規則 -->

<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_error.html"/>

<csrf disabled="true"/>

</http>

security="none"  設置此資源不被攔截
若是你沒有設置登陸頁security="none"  ,將會出現如下錯誤

 

 

 

由於登陸頁會被反覆重定向。

login-page:指定登陸頁面。
authentication-failure-url:指定了身份驗證失敗時跳轉到的頁面。
default-target-url:指定了成功進行身份驗證和受權後默認呈現給用戶的頁面。

csrf disabled="true"  關閉csrf ,若是不加會出現錯誤

 

CSRFCross-site request forgery)跨站請求僞造,也被稱爲「One Click Attack」或者Session Riding,一般縮寫爲CSRF或者XSRF,是一種對網站的惡意利用。

相關文章
相關標籤/搜索