日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

安全策略_Spring Security 实战干货:如何实现不同的接口不同的安全策略

發布時間:2024/9/27 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 安全策略_Spring Security 实战干货:如何实现不同的接口不同的安全策略 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 前言

歡迎閱讀 Spring Security 實戰干貨 系列文章 。最近有開發小伙伴提了一個有趣的問題。他正在做一個項目,涉及兩種風格,一種是給小程序出接口,安全上使用無狀態的JWT Token;另一種是管理后臺使用的是Freemarker,也就是前后端不分離的Session機制。用Spring Security該怎么辦?

2. 解決方案

我們可以通過多次繼承WebSecurityConfigurerAdapter構建多個HttpSecurity。HttpSecurity 對象會告訴我們如何驗證用戶的身份,如何進行訪問控制,采取的何種策略等等。

如果你看過之前的教程,我們是這么配置的:

/**
?*?單策略配置
?*
?*?@author?felord.cn
?*?@see?org.springframework.boot.autoconfigure.security.servlet.SpringBootWebSecurityConfiguration
?*?@since?14?:58?2019/10/15
?*/
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled?=?true,?jsr250Enabled?=?true,?securedEnabled?=?true)
@EnableWebSecurity
@ConditionalOnClass(WebSecurityConfigurerAdapter.class)
@ConditionalOnWebApplication(type?=?ConditionalOnWebApplication.Type.SERVLET)
public?class?CustomSpringBootWebSecurityConfiguration?{

????/**
?????*?The?type?Default?configurer?adapter.
?????*/
????@Configuration
????@Order(SecurityProperties.BASIC_AUTH_ORDER)
????static?class?DefaultConfigurerAdapter?extends?WebSecurityConfigurerAdapter?{

????????@Override
????????protected?void?configure(AuthenticationManagerBuilder?auth)?throws?Exception?{
????????????super.configure(auth);
????????}

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

????????@Override
????????protected?void?configure(HttpSecurity?http)?throws?Exception?{
????????????//?配置?httpSecurity

????????}
????}
}

上面的配置了一個HttpSecurity,我們如法炮制再增加一個WebSecurityConfigurerAdapter的子類來配置另一個HttpSecurity。伴隨而來的還有不少的問題要解決。

2.1 如何路由不同的安全配置

我們配置了兩個HttpSecurity之后,程序如何讓小程序接口和后臺接口走對應的HttpSecurity?

HttpSecurity.antMatcher(String antPattern)可以提供過濾機制。比如我們配置:

?????@Override
????????protected?void?configure(HttpSecurity?http)?throws?Exception?{
????????????//?配置?httpSecurity
????????????http.antMatcher("/admin/v1");

????????}

那么該HttpSecurity將只提供給以/admin/v1開頭的所有URL。這要求我們針對不同的客戶端指定統一的URL前綴。

舉一反三只要HttpSecurity提供的功能都可以進行個性化定制。比如登錄方式,角色體系等。

2.2 如何指定默認的 HttpSecurity

我們可以通過在WebSecurityConfigurerAdapter實現上使用@Order注解來指定優先級,數值越大優先級越低,沒有@Order注解將優先級最低。

2.3 如何配置不同的 UserDetailsService

很多情況下我們希望普通用戶和管理用戶完全隔離,我們就需要多個UserDetailsService,你可以在下面的方法中對AuthenticationManagerBuilder進行具體的設置來配置UserDetailsService,同時也可以配置不同的密碼策略。

@Override
protected?void?configure(AuthenticationManagerBuilder?auth)?throws?Exception?{
????DaoAuthenticationProvider?daoAuthenticationProvider?=?new?DaoAuthenticationProvider();
????daoAuthenticationProvider.setUserDetailsService(new?UserDetailsService()?{
????????@Override
????????public?UserDetails?loadUserByUsername(String?username)?throws?UsernameNotFoundException?{
????????????//?自行實現
????????????return??null?;
????????}
????});
????//?也可以設計特定的密碼策略
????BCryptPasswordEncoder?bCryptPasswordEncoder?=?new?BCryptPasswordEncoder();
????daoAuthenticationProvider.setPasswordEncoder(bCryptPasswordEncoder);
????auth.authenticationProvider(daoAuthenticationProvider);
}

2.4 最終的配置模板

上面的幾個問題解決之后,我們基本上掌握了在一個應用中執行多種安全策略。配置模板如下:

/**
?*?多個策略配置
?*
?*?@author?felord.cn
?*?@see?org.springframework.boot.autoconfigure.security.servlet.SpringBootWebSecurityConfiguration
?*?@since?14?:58?2019/10/15
?*/
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled?=?true,?jsr250Enabled?=?true,?securedEnabled?=?true)
@EnableWebSecurity
@ConditionalOnClass(WebSecurityConfigurerAdapter.class)
@ConditionalOnWebApplication(type?=?ConditionalOnWebApplication.Type.SERVLET)
public?class?CustomSpringBootWebSecurityConfiguration?{

????/**
?????*?后臺接口安全策略.?默認配置
?????*/
????@Configuration
????@Order(1)
????static?class?AdminConfigurerAdapter?extends?WebSecurityConfigurerAdapter?{

????????@Override
????????protected?void?configure(AuthenticationManagerBuilder?auth)?throws?Exception?{
????????????DaoAuthenticationProvider?daoAuthenticationProvider?=?new?DaoAuthenticationProvider();
????????????//用戶詳情服務個性化
????????????daoAuthenticationProvider.setUserDetailsService(new?UserDetailsService()?{
????????????????@Override
????????????????public?UserDetails?loadUserByUsername(String?username)?throws?UsernameNotFoundException?{
????????????????????//?自行實現
????????????????????return?null;
????????????????}
????????????});
????????????//?也可以設計特定的密碼策略
????????????BCryptPasswordEncoder?bCryptPasswordEncoder?=?new?BCryptPasswordEncoder();
????????????daoAuthenticationProvider.setPasswordEncoder(bCryptPasswordEncoder);
????????????auth.authenticationProvider(daoAuthenticationProvider);
????????}

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

????????@Override
????????protected?void?configure(HttpSecurity?http)?throws?Exception?{
????????????//?根據需求自行定制
????????????http.antMatcher("/admin/v1")
????????????????????.sessionManagement(Customizer.withDefaults())
????????????????????.formLogin(Customizer.withDefaults());


????????}
????}

????/**
?????*?app接口安全策略.?沒有{@link?Order}注解優先級比上面低
?????*/
????@Configuration
????static?class?AppConfigurerAdapter?extends?WebSecurityConfigurerAdapter?{

????????@Override
????????protected?void?configure(AuthenticationManagerBuilder?auth)?throws?Exception?{
????????????DaoAuthenticationProvider?daoAuthenticationProvider?=?new?DaoAuthenticationProvider();
????????????//用戶詳情服務個性化
????????????daoAuthenticationProvider.setUserDetailsService(new?UserDetailsService()?{
????????????????@Override
????????????????public?UserDetails?loadUserByUsername(String?username)?throws?UsernameNotFoundException?{
????????????????????//?自行實現
????????????????????return?null;
????????????????}
????????????});
????????????//?也可以設計特定的密碼策略
????????????BCryptPasswordEncoder?bCryptPasswordEncoder?=?new?BCryptPasswordEncoder();
????????????daoAuthenticationProvider.setPasswordEncoder(bCryptPasswordEncoder);
????????????auth.authenticationProvider(daoAuthenticationProvider);
????????}

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

????????@Override
????????protected?void?configure(HttpSecurity?http)?throws?Exception?{
????????????//?根據需求自行定制
????????????http.antMatcher("/app/v1")
????????????????????.sessionManagement(Customizer.withDefaults())
????????????????????.formLogin(Customizer.withDefaults());


????????}
????}
}

3. 總結

今天我們解決了如何針對不同類型接口采取不同的安全策略的方法,希望對你有用,如果你有什么問題可以留言。多多關注:碼農小胖哥,更多干貨奉上。

往期推薦:

Java開發常用技術棧盤點

訂閱發布模式到底是不是觀察者模式?

總結

以上是生活随笔為你收集整理的安全策略_Spring Security 实战干货:如何实现不同的接口不同的安全策略的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 99精品在线视频观看 | 精品人妻一区二区三区潮喷在线 | 亚洲AV无码一区二区三区蜜桃 | 三级全黄视频 | 一曲二曲三曲在线观看中文字幕动漫 | 国产精品午夜无码专区 | 亚洲欧洲精品一区 | 日韩人妻无码一区二区三区99 | 天海翼视频在线观看 | 超碰人操 | 国产又黄又粗 | 日日噜噜噜噜久久久精品毛片 | 午夜男人天堂 | 欧美日韩视频免费观看 | 国产 一二三四五六 | 最近中文字幕第一页 | 日本a级在线 | 亚洲成av人片一区二区梦乃 | 国产一级免费在线观看 | 四虎精品欧美一区二区免费 | 天天狠天天操 | 免费精品在线观看 | 国产人妖一区二区 | 成人性生交生交视频 | www国产无套内射com | 69人妻精品久久无人专区 | 玩偶姐姐在线看 | 亚洲欧美在线不卡 | 看毛片视频 | 亚洲福利视频一区 | 人人草超碰 | 国产福利91精品一区二区三区 | 天堂中文在线观看 | 国产精品suv一区二区 | 精品国产AV色欲天媒传媒 | 黄色一级片免费观看 | 伊人久久青青 | 在线观看免费中文字幕 | 国产高清在线观看 | 欧美激情在线免费 | 国产精品久久久久久一区二区三区 | 欧美一区二区三区在线 | 99色综合网 | 97国产高清| 涩涩av| 中文字幕第一页在线 | 激情五月婷婷丁香 | 久久一久久 | 毛片网站在线播放 | 免费萌白酱国产一区二区三区 | 欧美成人综合网站 | 日本精品免费视频 | 天天综合色 | 狠狠干超碰 | 亚洲综合免费视频 | 香蕉成人在线视频 | 欧美日韩在线视频一区二区 | 牛牛免费视频 | 亚洲激情久久 | 日韩欧美成人网 | 黄色片网站国产 | 色婷婷久久 | 一区二区三区久久久 | 国产人人射 | 自拍偷拍色图 | 亚洲精品乱码久久久久久蜜桃麻豆 | 最好看的中文字幕国语电影mv | 亚州| 天天射天天色天天干 | 国产一区二区三区四区五区在线 | 国产人妖在线视频 | 香蕉大人久久国产成人av | 亚洲av综合色区无码一二三区 | 色偷偷综合网 | 伊人久久大香线蕉av一区 | 成人一区av| 国产亚洲第一页 | 美女精品 | 国产精品久久久久久一区二区 | 欧美涩色 | 国产精品乱码久久久久久久久 | 插我舔内射18免费视频 | 小宵虎南在线观看 | 91国产丝袜在线播放 | 日韩在线网址 | 涩涩在线播放 | 国产一级一级片 | www.色图 | 少妇激情偷人三级 | 完全免费av | 国产免费叼嘿网站免费 | 日韩福利视频一区 | 黑人导航 | 午夜高清视频 | 国产乱子伦视频一区二区三区 | 91超薄肉色丝袜交足高跟凉鞋 | 国产精品成人国产乱 | av自拍一区 | 男阳茎进女阳道视频大全 |