2024-01-26 Security + oauth2 登錄1

官網(wǎng)
https://docs.spring.io/spring-authorization-server

1.概念理解
https://zhuanlan.zhihu.com/p/496695229?utm_id=0
2.最新實用
https://mp.weixin.qq.com/s?__biz=Mzg3Njc1NjAxMw==&mid=2247484868&idx=1&sn=6ea61f66cf57badc4b2434dfa84aa745&chksm=cf2c2886f85ba190808469a0c7f2b526808553c917922ca680a0f0bcf9ed2787893b18124c20&cur_album_id=2823815577264685059&scene=189#wechat_redirect

在Spring Security Oauth2中, 獲取token整個流程為tokenEndpoint-->tokenGranter-->AuthenticationManager-->ProviderManager-->Tokenservice -->AccessTokenConverter-->token碟刺。

1.1 tokenEndpoint對比

token轉化accessTokenRequestConvertes
認證管理器AuthenticationProvider
請求匹配器RequestMatcher

tokenEndpoint
獲取token的端口 /oauth2/token
token自省端口/oauth2/introspect

1.2 tokenEndpoint token端口相關配置


image.png

當然如果你想修改這些端口锁保,只需自定義即可。如改成/oauth2/token/test
/***
@Bean
public AuthorizationServerSettings authorizationServerSettings() {
return AuthorizationServerSettings
.builder()
.tokenEndpoint("/oauth2/token/test")
.build();
}
*/

1.3.1 資源服務器配置
只需配置過濾器鏈SecurityFilterChain
/***
@Bean
SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
http
// 開放自定義的部分端點
.authorizeRequests(authorizeRequests -> authorizeRequests.antMatchers("/token/*").permitAll()
.anyRequest().authenticated())
.headers()
.frameOptions()
.sameOrigin()// 避免iframe同源無法登錄
.and()
// 表單登錄個性化
.apply(new FormIdentityLoginConfigurer());
// 處理 UsernamePasswordAuthenticationToken
http.authenticationProvider(new PigDaoAuthenticationProvider());
return http.build();
}

public final class FormIdentityLoginConfigurer
extends AbstractHttpConfigurer<FormIdentityLoginConfigurer, HttpSecurity> {

@Override
public void init(HttpSecurity http) throws Exception {
    http.formLogin(formLogin -> {
        formLogin.loginPage("/token/login");
        formLogin.loginProcessingUrl("/token/form");
        formLogin.failureHandler(new FormAuthenticationFailureHandler());

    }).logout() // SSO登出成功處理
            .logoutSuccessHandler(new SsoLogoutSuccessHandler()).deleteCookies("JSESSIONID")
            .invalidateHttpSession(true).and().csrf().disable();
}

}
/
1.3.2 認證服務器配置
只需配置過濾器鏈SecurityFilterChain
/
**
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer = new OAuth2AuthorizationServerConfigurer();
// 個性化認證授權端點
http.apply(authorizationServerConfigurer.tokenEndpoint((tokenEndpoint) -> {
// 注入自定義的授權認證Converter
tokenEndpoint.accessTokenRequestConverter(accessTokenRequestConverter())
// 登錄成功處理器
.accessTokenResponseHandler(new PigAuthenticationSuccessEventHandler())
// 登錄失敗處理器
.errorResponseHandler(new PigAuthenticationFailureEventHandler());
// 個性化客戶端認證
}).clientAuthentication(oAuth2ClientAuthenticationConfigurer ->
// 處理客戶端認證異常
oAuth2ClientAuthenticationConfigurer.errorResponseHandler(new PigAuthenticationFailureEventHandler()))
// 授權碼端點個性化confirm頁面
.authorizationEndpoint(authorizationEndpoint -> authorizationEndpoint
.consentPage(SecurityConstants.CUSTOM_CONSENT_PAGE_URI)));

RequestMatcher endpointsMatcher = authorizationServerConfigurer.getEndpointsMatcher();
DefaultSecurityFilterChain securityFilterChain = http.requestMatcher(endpointsMatcher)
        .authorizeRequests(authorizeRequests -> authorizeRequests.anyRequest().authenticated())
        // redis存儲token的實現(xiàn)
        .apply(authorizationServerConfigurer.authorizationService(authorizationService)
                //token端口配置
                .authorizationServerSettings(AuthorizationServerSettings.builder()
                        .issuer(SecurityConstants.PROJECT_LICENSE).build()))
        // 授權碼登錄的登錄頁個性化
        .and().apply(new FormIdentityLoginConfigurer()).and().build();

// 注入自定義授權模式實現(xiàn)
addCustomOAuth2GrantAuthenticationProvider(http);
return securityFilterChain;

}
*/

1.4.許可類型對比

1.4.1 密碼模式默認不支持
https://docs.spring.io/spring-authorization-server/docs/current/reference/html/overview.html

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市爽柒,隨后出現(xiàn)的幾起案子吴菠,更是在濱河造成了極大的恐慌,老刑警劉巖浩村,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件做葵,死亡現(xiàn)場離奇詭異,居然都是意外死亡心墅,警方通過查閱死者的電腦和手機酿矢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來怎燥,“玉大人瘫筐,你說我怎么就攤上這事☆硪Γ” “怎么了策肝?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長隐绵。 經(jīng)常有香客問我之众,道長,這世上最難降的妖魔是什么氢橙? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任酝枢,我火速辦了婚禮,結果婚禮上悍手,老公的妹妹穿的比我還像新娘帘睦。我一直安慰自己,他們只是感情好坦康,可當我...
    茶點故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布竣付。 她就那樣靜靜地躺著,像睡著了一般滞欠。 火紅的嫁衣襯著肌膚如雪古胆。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天筛璧,我揣著相機與錄音逸绎,去河邊找鬼。 笑死夭谤,一個胖子當著我的面吹牛棺牧,可吹牛的內容都是我干的。 我是一名探鬼主播朗儒,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼颊乘,長吁一口氣:“原來是場噩夢啊……” “哼参淹!你這毒婦竟也來了?” 一聲冷哼從身側響起乏悄,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤浙值,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后檩小,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體开呐,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年识啦,在試婚紗的時候發(fā)現(xiàn)自己被綠了负蚊。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡颓哮,死狀恐怖家妆,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情冕茅,我是刑警寧澤伤极,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站姨伤,受9級特大地震影響哨坪,放射性物質發(fā)生泄漏。R本人自食惡果不足惜乍楚,卻給世界環(huán)境...
    茶點故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一当编、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧徒溪,春花似錦忿偷、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至渠概,卻和暖如春茶凳,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背播揪。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工贮喧, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人猪狈。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓箱沦,卻偏偏與公主長得像,于是被迫代替她去往敵國和親罪裹。 傳聞我的和親對象是個殘疾皇子饱普,可洞房花燭夜當晚...
    茶點故事閱讀 44,611評論 2 353

推薦閱讀更多精彩內容