SpringSecurity中怎么自定義表單登錄

這篇文章給大家介紹SpringSecurity中怎么自定義表單登錄,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

10年積累的網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有東洲免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

1.創(chuàng)建SpringSecurity項(xiàng)目

1.1 使用IDEA

先通過IDEA 創(chuàng)建一個(gè)SpringBoot項(xiàng)目 并且依賴SpringSecurity,Web依賴

此時(shí)pom.xml會(huì)自動(dòng)添加

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

2.擴(kuò)展 WebSecurityConfigurerAdapter

WebSecurityConfigurerAdapter 是SpringSecurity 提供的用于我們擴(kuò)展自己的配置

?實(shí)現(xiàn)WebSecurityConfigurerAdapter經(jīng)常需要重寫的:

1、configure(AuthenticationManagerBuilder auth);

2、configure(WebSecurity web);

3、configure(HttpSecurity http);

2.1 默認(rèn) WebSecurityConfigurerAdapter 為我們提供了一些基礎(chǔ)配置如下

protected void configure(HttpSecurity http) throws Exception {  logger.debug("Using default configure(HttpSecurity). If subclassed this will potentially override subclass configure(HttpSecurity).");  http    .authorizeRequests()      .anyRequest().authenticated()      .and()    .formLogin().and()    .httpBasic();}

2.2 創(chuàng)建自定義的 WebSecurityConfigurer

1.formLogin() 開啟表單登錄,該方法會(huì)應(yīng)用 FormLoginConfigurer 到HttpSecurity上,后續(xù)會(huì)被轉(zhuǎn)換為對(duì)應(yīng)的Filter2.loginPage() 配置自定義的表單頁(yè)面 3.authorizeRequests().anyRequest().authenticated(); 表示任何請(qǐng)求接口都要認(rèn)證**

@Configuration@Slf4jpublic class MyWebSecurityConfig extends WebSecurityConfigurerAdapter { @Overrideprotected void configure(HttpSecurity http) throws Exception {    http.csrf().disable()      .formLogin()      .loginPage("/mylogin.html")      .and()      .authorizeRequests().anyRequest().authenticated();        }}

2.3 mylogin.html

<!DOCTYPE html><html lang="en"> <head>   <meta charset="UTF-8">   <title>Title</title> </head> <body>  <h2>標(biāo)準(zhǔn)登錄頁(yè)面</h2>  <h4>表單登錄</h4> <form action="/login" method="post">  <table>  <tr>    <td>用戶名:</td>    <td><input type="text" name="username"/></td>  </tr>  <tr>    <td>密碼:</td>    <td><input type="password" name="password"/></td>  </tr>  <tr>    <td colspan="2">      <button type="submit">登錄</button>    </td>  </tr>  </table> </form></body></html>

3.訪問自定義登錄頁(yè)面(注意有重定向過多問題)

啟動(dòng)項(xiàng)目 并且直接訪問

http://localhost:8080

會(huì)發(fā)現(xiàn)瀏覽器報(bào) 重定向次數(shù)過多,這是什么原因呢?

這是因?yàn)?我們上面配置了 loginPage("/mylogin.html") ,但是這個(gè)路徑它沒有被允許訪問,也就是當(dāng)重定向到/mylogin.html路徑后,還是會(huì)因?yàn)樾枰J(rèn)證 被重定向道 /mylogin.html 導(dǎo)致該錯(cuò)誤

4.允許登錄頁(yè)面路徑訪問 antMatchers("/mylogin.html").permitAll()

只需要在配置的地方 添加 .antMatchers("/mylogin.html").permitAll() 允許這個(gè)路徑

http.csrf().disable()      .formLogin()      .loginPage("/mylogin.html")      .and()      .authorizeRequests()      .antMatchers("/mylogin.html").permitAll()      .anyRequest().authenticated();

再次訪問,我們自定義的表單就顯示出來(lái)了(忽略樣式。。。)

此時(shí)我們輸入用戶名 user 密碼 : 控制臺(tái)打印

Using generated security password: 6bf253eb-c785-42b6-b147-b0fe2971586e

發(fā)現(xiàn)又跳轉(zhuǎn)到 /mylogin.html頁(yè)面,這是因?yàn)?當(dāng)我們配置了 loginPage("/mylogin.html")之后 處理表單登錄的過濾器它所攔截的請(qǐng)求就不再是 /login (默認(rèn)是 /login) ,攔截的登錄請(qǐng)求地址變成了 和 loginPage一樣的 mylogin.html

此時(shí)如果將 action地址改成 /mylogin.html ,那么再登錄 就能成功

<form action="/mylogin.html" method="post">

5.配置自定義登錄接口路徑 loginProcessingUrl

由于我們上面配置了 loginPage ,則對(duì)應(yīng)登錄接口路徑也變成了 loginPage所配置的 mylogin.html,但是當(dāng)我們不想使用這個(gè)作為接口路徑的時(shí)候,可以通過以下配置來(lái)修改

通過 loginProcessingUrl 類配置處理登錄請(qǐng)求的路徑

http.csrf().disable()      .formLogin()      .loginPage("/mylogin.html")      .loginProcessingUrl("/auth/login")      .and()      .authorizeRequests()      .antMatchers("/mylogin.html").permitAll()      .anyRequest().authenticated();

記得和action 對(duì)應(yīng)

<form action="/auth/login" method="post">

關(guān)于SpringSecurity中怎么自定義表單登錄就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

本文名稱:SpringSecurity中怎么自定義表單登錄
分享網(wǎng)址:http://muchs.cn/article0/ghsgoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、用戶體驗(yàn)定制開發(fā)、品牌網(wǎng)站制作電子商務(wù)、自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作