Spring Security原理學習--簡介與示例(一)

一、簡介

中文介紹:https://springcloud.cc/spring-security-zhcn.html

Spring Security 提供了基於javaEE的企業應有個你軟件全面的安全服務。這裏特別強調支持使用SPring框架構件的項目,Spring框架是企業軟件開發javaEE方案的領導者。如果你還沒有使用Spring來開發企業應用程序,我們熱忱的鼓勵你仔細的看一看。熟悉Spring特別是一來注入原理兩幫助你更快更方便的使用Spring Security。

人們使用Spring Secruity的原因有很多,單大部分都發現了javaEE的Servlet規範或EJB規範中的安全功能缺乏典型企業應用場景所需的深度。提到這些規範,重要的是要認識到他們在WAR或EAR級別無法移植。因此如果你更換服務器環境,這裏有典型的大量工作去重新配置你的應用程序員安全到新的目標環境。使用Spring Security 解決了這些問題,也爲你提供許多其他有用的,可定製的安全功能。

正如你可能知道的兩個應用程序的兩個主要區域是“認證”和“授權”(或者訪問控制)。這兩個主要區域是Spring Security 的兩個目標。“認證”,是建立一個他聲明的主題的過程(一個“主體”一般是指用戶,設備或一些可以在你的應用程序中執行動作的其他系統)。“授權”指確定一個主體是否允許在你的應用程序執行一個動作的過程。爲了抵達需要授權的店,主體的身份已經有認證過程建立。這個概念是通用的而不只在Spring Security中。

在身份驗證層,Spring Security 的支持多種認證模式。這些驗證絕大多數都是要麼由第三方提供,或由相關的標準組織,如互聯網工程任務組開發。另外Spring Security 提供自己的一組認證功能。具體而言,Spring Security 目前支持所有這些技術集成的身份驗證:

  • HTTP BASIC 認證頭 (基於 IETF RFC-based 標準)

  • HTTP Digest 認證頭 ( IETF RFC-based 標準)

  • HTTP X.509 客戶端證書交換 ( IETF RFC-based 標準)

  • LDAP (一個非常常見的方法來跨平臺認證需要, 尤其是在大型環境)

  • Form-based authentication (用於簡單的用戶界面)

  • OpenID 認證

  • Authentication based on pre-established request headers (such as Computer Associates Siteminder) 根據預先建立的請求有進行驗證

  • JA-SIG Central Authentication Service (CAS,一個開源的SSO系統 )

  • Transparent authentication context propagation for Remote Method Invocation (RMI) and HttpInvoker (Spring遠程協議)

  • Automatic "remember-me" authentication (你可以勾選一個框以避免預定的時間段再認證)

  • Anonymous authentication (讓每一個未經驗證的訪問自動假設爲一個特定的安全標識)

  • Run-as authentication (在一個訪問應該使用不同的安全標識時非常有用)

  • Java Authentication and Authorization Service (JAAS)

  • JEE container autentication (所以如果願你以可以任然使用容器管理的認證)

  • Kerberos

  • Java Open Source Single Sign On (JOSSO) *

  • OpenNMS Network Management Platform *

  • AppFuse *

  • AndroMDA *

  • Mule ESB *

  • Direct Web Request (DWR) *

  • Grails *

  • Tapestry *

  • JTrac *

  • Jasypt *

  • Roller *

  • Elastic Path *

  • Atlassian Crowd *

  • Your own authentication systems (see below)

  • 表示由第三方提供

很多獨立軟件供應商,因爲靈活的身份驗證模式二選擇Spring Security。這樣做允許他們快速的集成到他們的終端客戶需求的解決方案而不用進行大量工程或者改變客戶的環境。如果上面的驗證機制不符合你的需求,Spring Security 是一個開放的平臺,要實現你 自己的驗證機制檢查。Spring Security 的許多企業用戶需要與不遵循任何安全標準的“遺留”系統集成,Spring Security可以很好的與這類系統集成。

無論何種身份驗證機制,Spring Security提供一套的授權功能。這裏有三個主要的熱點區域,授權web請求、授權方法是否可以被調用和授權訪問單個域對象的實例。爲了幫助讓你分別瞭解這些差異,認識在Servlet規範網絡模式安全的授權功能,EJB容器管理的安全性和文件系統的安全。Spring Security在這些重要的區域提供授權功能,我們將在手冊後面進行介紹。

二、示例

1、pom.xml引用spring security相關jar包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

2、Spring security 相關初始化後設置

使用默認登錄地址:/login

登錄成功跳轉地址:/hello

登出地址:/logout

除了登錄地址爲其他都需要校驗:anyRequest().authenticated()

不攔截路徑中包含/test的請求:antMatchers("/test").permitAll()

通過內存方式設置用戶名密碼和角色:admin,admin和USER

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                //不攔截路徑中包含test的請求
                .antMatchers("/", "/test").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().successForwardUrl("/hello").permitAll()
                .and()
                .logout().logoutUrl("/logout").logoutSuccessUrl("/login").permitAll()
                .and()
                .httpBasic();
        http.csrf().disable();
    }

    @Override
    public void configure(WebSecurity web) throws Exception{
        super.configure(web);
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication()
                .withUser("admin").password("admin").roles("USER");
        //在內存中創建了一個用戶,該用戶的名稱爲user,密碼爲password,用戶角色爲USER
    }

}

3、Spring boot應用方式啓動

(1)不用登錄可以直接訪問test接口

(2)用戶名和密碼登錄訪問hello接口,Spring Security自帶的簡單登錄頁面

github地址:Spring Boot

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