SpringSecurity在Springboot下使用的初步體驗

  SpringSecurity曾經在十年前很是火熱,只要是作權限系統,當時幾乎非用它不可,記得是在XML文件裏一堆的配置。曾幾什麼時候,Shiro冒了出來,以其簡潔和輕量的風格慢慢地捕獲了衆多碼農的心,今後SpringSecurity彷佛成了歷史文物。 web

  但事物老是在發展變化的,這兩年隨着 SpringBoot的興起,因爲SpringSecurity與SpringBoot都是Spring家族成員,在整合上具有自然優點,且SpringSecurity功能相對Shiro更加完善,對OAUTH認證支持得比較好,因此在微服務架構中又獲得了普遍應用。spring

       在SpringBoot下使用SpringSecurity很是的簡單,只要保證在項目的classpath下引入了相應的jar包就能夠了。啓動類上也無需添加什麼,下面看一個SpringSecurity應用的最簡項目:apache

1. pom配置springboot

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
    </parent>


    <groupId>cn.xxx.yyyy</groupId>
    <artifactId>spring-security-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-security-demo</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

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

    </dependencies>

 

2. 啓動類架構

@SpringBootApplication public class App { public static void main( String[] args ) { SpringApplication.run(App.class, args) ; } }

 

3.application.properties或application.ymlapp

  沒配置任何內容框架

 

運行啓動類App後訪問 127.0.0.1:8080 ,出現以下頁面:maven

這說明請求被springSecurity攔截了,springSecurity開始發揮做用了。spring-boot

一切看起來彷佛很神奇,其實無非springBoot實例啓動時,發現類路徑下有相應的springSecurity的類,而後就自動幫咱們加載了springSecurity的配置。微服務

下面仔細分析下,跟咱們在pom中引入的spring-boot-starter-web依賴有很大關係:

 

   spring-boot-starter-web這個依賴不但引入了相關的 springMvc的jar , 還傳遞引入了 spring-boot-autoconfigure 這個jar ,如圖:

  

因爲spring-boot-autoconfigure的jar中有 spring.factories文件,這個文件中提供了大量的配置類,是會被springboot框架自動解析處理的,其中就有和springSecurity相關的配置類 org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration,它的源碼以下:

@Configuration @ConditionalOnClass(DefaultAuthenticationEventPublisher.class) @EnableConfigurationProperties(SecurityProperties.class) @Import({ SpringBootWebSecurityConfiguration.class, WebSecurityEnablerConfiguration.class, SecurityDataConfiguration.class }) public class SecurityAutoConfiguration { @Bean @ConditionalOnMissingBean(AuthenticationEventPublisher.class) public DefaultAuthenticationEventPublisher authenticationEventPublisher( ApplicationEventPublisher publisher) { return new DefaultAuthenticationEventPublisher(publisher); } }

經過源碼可知:

1. 會在類路徑下查找DefaultAuthenticationEventPublisher類(存在於spring-secrity-core-x.y.z.RELEASE jar中),若是存在這個類 ,SecurityAutoConfiguration這個配置類就會起做用。

2 . 會導入三個配置類  SpringBootWebSecurityConfiguration.class ,  WebSecurityEnablerConfiguration.class, SecurityDataConfiguration.

3. SpringBootWebSecurityConifguration類,這裏真正引入了SpringSecurtiy的默認配置類WebSecurityConfigurerAdapter,固然首先會在類路徑下類路徑下查找是否

WebSecurityConfigurerAdapter的子類的bean的配置,若是沒有就使用WebSecurityConfigurerAdapter的默認配置
@Configuration @ConditionalOnClass(WebSecurityConfigurerAdapter.class) @ConditionalOnMissingBean(WebSecurityConfigurerAdapter.class) @ConditionalOnWebApplication(type = Type.SERVLET) public class SpringBootWebSecurityConfiguration { @Configuration @Order(SecurityProperties.BASIC_AUTH_ORDER) static class DefaultConfigurerAdapter extends WebSecurityConfigurerAdapter { } }

 

此外,spring-boot-starter-security 這個依賴的做用也不可忽視 , 它保證須要的jar被引入:

相關文章
相關標籤/搜索