SpringSecurity

SpringSecurity是Spring家族中的一個安全管理框架,實際E,在SpringBoot出現之前,SpringSecurity就已經發展了多年了,但是使用的並不多,安全管理這個領域,一直是Shiro的天下。

        相對於Shiro,在SSMISSH中整合Spring Security都是比較麻煩的操作,所以,Spring Security雖然功能比Shiro強大,但是使用反而沒有Shiro多(Shiro雖然功能沒有Spring Security多,但是對於大部分項目而言,Shiro也夠用了)

        自從有了Spring Boot之後,Spring Boot對於Spring Security提供了自動化配置方案,可以零配置使用Spring Security.

         因此,一般來說,常見的安全管理技術棧的組合是這樣的

  SSM/H  + Shiro
         SpringBoot/SpringCloud + SpringSecurity

注意,這只是一個推薦的組合而已,如果單純從技術上來說,無論怎麼組合,都是可以運行的。


SpringSecurity簡介

打開  Spring Security 的官網,從其首頁的預覽上就可以看見如下文字:

Spring Security is a powerful and highly customizable authentication and access-control framework. It is the de-facto standard for securing Spring-based applications.

Spring Security is a framework that focuses on providing both authentication and authorization to Java applications. Like all Spring projects, the real power of Spring Security is found in how easily it can be extended to meet custom requirements.

這段文字的大致意思是:

  • Spring Security 是一個強大的、可高度定製化的身份驗證和訪問控制的框架,它基本上是保護基於 Spring 應用的安全標準。
  • Spring Security 是一個專注於向 Java 應用程序提供身份驗證和授權的框架。像所有的 Spring 項目一樣,Spring Security 的真正威力在於它可以很容易地被擴展以滿足定製需求。

身份驗證和訪問控制是應用安全的兩個重要方面,也常常被稱爲“認證”和“授權”。

  • 認證就是確定主體的過程,當未認證的主體訪問系統資源的時候,系統會對主體的身份進行驗證,確定該主體是否有合法的身份,不合法的主體將被應用拒絕訪問,這一點也很容易理解,比如某電商網站,未登錄的用戶是無法訪問敏感數據資源的,比如訂單信息。
  • 授權是在主體認證結束後,判斷該認證主體是否有權限去訪問某些資源,沒有權限的訪問將被系統拒絕,比如某電商網站的登錄用戶去查看其它用戶的訂單信息,很明顯,系統會拒絕這樣的無理要求。

上面的兩點是應用安全的基本關注點,Spring Security 存在的意義就是幫助開發者更加便捷地實現了應用的認證和授權能力。

SpringSecurity配置

這裏就不展示前端的頁面了,將代碼託管到github上,以便後續學習。

https://github.com/201705010201/SpringSecurity-

package com.cc.config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    //授權
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        //首頁所有人可以訪問,功能頁只有對應有權限的人才能訪問
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

        //沒有權限默認會到登錄頁面,需要開啓登錄的頁面
        http.formLogin().loginPage("/toLogin");

        http.csrf().disable();//關閉

        //註銷
        http.logout().logoutSuccessUrl("/");
    }


    //認證
    //PasswordEncoder(密碼編碼)
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("yue").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2,vip3")
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
                .and()
                .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發佈了213 篇原創文章 · 獲贊 12 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章