Spring Security 一鍵接入驗證碼登錄和小程序登錄

最近實現了一個多端登錄的Spring Security組件,用起來非常絲滑,開箱即用,可插拔,而且靈活性非常強。我覺得能滿足大部分場景的需要。目前完成了手機號驗證碼和微信小程序兩種自定義登錄,加上默認的Form登錄,一共三種,現在開源分享給大家,接下來簡單介紹一下這個插件包。

DSL配置風格

切入正題,先來看看配置:

    @Bean
    SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .mvcMatchers("/foo/**")
                .access("hasAuthority('ROLE_USER')").anyRequest().authenticated()
                .and()
                // 默認form表單登錄
                .formLogin()
                .and()
                .apply(new LoginFilterSecurityConfigurer<>())
                // 驗證碼登錄
                .captchaLogin(captchaLoginConfigurer ->
                                // 驗證碼校驗 1 在此處配置 優先級最高 2 註冊爲Spring Bean 可以免配置
                                captchaLoginConfigurer.captchaService(this::verifyCaptchaMock)
                                        // 根據手機號查詢用戶UserDetials  1 在此處配置 優先級最高 2 註冊爲Spring Bean 可以免配置
                                        .captchaUserDetailsService(this::loadUserByPhoneMock)
                                        // 生成JWT 返回  1 在此處配置 優先級最高 2 註冊爲Spring Bean 可以免配置
                                        .jwtTokenGenerator(this::tokenResponseMock)
                        //todo 其它配置省略……
                )
                // 小程序登錄 同時支持多個小程序
                .miniAppLogin(miniAppLoginConfigurer -> miniAppLoginConfigurer
                                // 實現小程序多租戶
                                // 根據請求攜帶的clientid 查詢小程序的appid和secret 1 在此處配置 優先級最高 2 註冊爲Spring Bean 可以免配置
                                .miniAppClientService(this::miniAppClientMock)
                                // 小程序用戶 自動註冊和檢索  1 在此處配置 優先級最高 2 註冊爲Spring Bean 可以免配置
                                .miniAppUserDetailsService(new MiniAppUserDetailsServiceMock())
                                // 小程序sessionkey緩存 過期時間應該小於微信官方文檔的聲明   1 在此處配置 優先級最高 2 註冊爲Spring Bean 可以免配置
                                .miniAppSessionKeyCache(new MiniAppSessionKeyCacheMock())
                                // 生成JWT 返回  1 在此處配置 優先級最高 2 註冊爲Spring Bean 可以免配置
                                .jwtTokenGenerator(this::tokenResponseMock)
                        //todo 其它配置省略……
                );

        return http.build();
    }

這種風格完全貼合了Spring SecurityDSL配置風格,不僅僅高大上,而且可以按需配置。如果你沒有驗證碼登錄直接刪掉captchaLogin方法;如果你沒有微信小程序登錄直接刪掉miniAppLogin方法。甚至還可以對單種登錄進行細粒度定製化,formLogin有的功能基本驗證碼登錄和微信小程序登錄的都有。

爲什麼這麼靈活?

這裏抽象了一個登錄配置類:

 public abstract class AbstractLoginFilterConfigurer<H extends HttpSecurityBuilder<H>, 
         C extends AbstractLoginFilterConfigurer<H, C, F>, 
         F extends AbstractAuthenticationProcessingFilter>
         extends AbstractHttpConfigurer<AbstractLoginFilterConfigurer<H, C, F>, H> {
             // 省略……
         }

所有額外的登錄渠道大都可以通過這個類來擴展,負責驗證碼登錄的CaptchaLoginFilterConfigurer和微信小程序登錄的MiniAppLoginFilterConfigurer都是該類實現的,基本上你看了源碼也能照葫蘆畫瓢來一個。

另外上面這些配置項接口,都可以放在Spring IoC中,配置類能自動獲取,不過優先級最高的還是通過上面代碼中配置的具體實現,原理參見下面的的樣例:

  @Override
     protected AuthenticationSuccessHandler defaultSuccessHandler(H http) {
         // 如果配置類沒有配置 就嘗試去Spring IoC中發現
         if (this.jwtTokenGenerator == null) {
             ApplicationContext applicationContext = http.getSharedObject(ApplicationContext.class);
             jwtTokenGenerator = getBeanOrNull(applicationContext, JwtTokenGenerator.class);
         }
         Assert.notNull(jwtTokenGenerator, "jwtTokenGenerator is required");
         return new LoginAuthenticationSuccessHandler(jwtTokenGenerator);
     }
 ​
 ​
     public final <T> T getBeanOrNull(ApplicationContext applicationContext, Class<T> beanType) {
         String[] beanNames = applicationContext.getBeanNamesForType(beanType);
         if (beanNames.length == 1) {
             return applicationContext.getBean(beanNames[0], beanType);
         }
         return null;
     }

使用方法

自行使用Maven命令mvn install到本地倉庫,然後引入:

        <dependency>
            <groupId>cn.felord</groupId>
            <artifactId>spring-security-extension</artifactId>
            <version>1.0.0</version>
        </dependency>

然後參考樣例sample項目進行開發,登錄方式有三種。

普通登錄

原生Spring Security接口


POST /login?username=user&password=12345 HTTP/1.1
Host: localhost:8080

驗證碼登錄

需要先實現必須的配置接口

發送驗證碼後調用驗證碼登錄接口:


POST /login/captcha?phone=11111111111&captcha=123123 HTTP/1.1
Host: localhost:8080

小程序登錄

需要先實現必須的配置接口

前端先調用微信授權登錄接口獲取openid:


POST /miniapp/preauth?clientId=wxxda23234&jsCode=051A23234ZHa1tZ5yj3AOlFr HTTP/1.1
Host: localhost:8080

響應:

{
    "code": 200,
    "data": {
        "errcode": null,
        "errmsg": null,
        "sessionKey": null,
        "openid": "oWmZj5QBrZxxxxx8OUxRrZJi4",
        "unionid": "oS-dxxxxxx4w_x7dA-h9MIuA"
    },
    "msg": "",
    "identifier": true
}

然後調用小程序登錄接口:

POST /login/miniapp HTTP/1.1
Host: localhost:8080
Content-Type: application/json

{
    "clientId": "wxd14qr6",
    "openId": "oWmZj5QBrZIBks0xx8OUxRrZJi4",
    "unionId": "oS-dK520tgW8xxxx7dA-h9MIuA",
    "iv":"LQUOt8BSTa7xxxpe1Q==",
    "encryptedData": "10hn3o4xxxxxrO/Ag5nRD3QkLSzduKnWuzN9B/H4Y0G5mDPR8siA7T8yaaqZsrMycLAoe2qrd1J75yYetYuWifiq3jUrcceRZHVxxl9LnQdW8f5+pMTnQtCYiMJ7Jm9paCw2Bh+5Lowkyqkx1q0fALvCQ9LXPPLAbLOB9CavRfKoenAmyyHQjZ/6lz0njzA=="
}

獲取方式

Gitee: felord/spring-security-login-extension

關注公衆號:Felordcn 獲取更多資訊

個人博客:https://felord.cn

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