SpringSecurity自定義登錄界面
為什么需要自定義登錄界面?
答:因為SpringBoot整合SpringSecurity時,只需要一個依賴,無需其他配置,就可以實現(xiàn)認證功能。但是它的認證登錄界面是固定那樣的,如下圖所示,但是我們希望自己搞個好看的登錄界面,所以需要自定義登錄界面。
第一步:創(chuàng)建springboot項目
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ? ? ? ? ?xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> ? ? <modelVersion>4.0.0</modelVersion> ? ? <parent> ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? <artifactId>spring-boot-starter-parent</artifactId> ? ? ? ? <version>2.6.6</version> ? ? ? ? <relativePath/> <!-- lookup parent from repository --> ? ? </parent> ? ? <groupId>com.example</groupId> ? ? <artifactId>spring-security-03</artifactId> ? ? <version>0.0.1-SNAPSHOT</version> ? ? <name>spring-security-03</name> ? ? <description>Demo project for Spring Boot</description> ? ? <properties> ? ? ? ? <java.version>1.8</java.version> ? ? </properties> ? ? <dependencies> ? ? ? ? <!--thymeleaf--> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-starter-thymeleaf</artifactId> ? ? ? ? </dependency> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-starter-security</artifactId> ? ? ? ? </dependency> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-starter-web</artifactId> ? ? ? ? </dependency> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? <artifactId>spring-boot-starter-test</artifactId> ? ? ? ? ? ? <scope>test</scope> ? ? ? ? </dependency> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.springframework.security</groupId> ? ? ? ? ? ? <artifactId>spring-security-test</artifactId> ? ? ? ? ? ? <scope>test</scope> ? ? ? ? </dependency> ? ? </dependencies> ? ? <build> ? ? ? ? <plugins> ? ? ? ? ? ? <plugin> ? ? ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ? ? ? ? ? ? ? ? <artifactId>spring-boot-maven-plugin</artifactId> ? ? ? ? ? ? </plugin> ? ? ? ? </plugins> ? ? </build> </project>
第二步:添加配置application.properties
#修改springSecurity默認用戶名和密碼 spring.security.user.name=root spring.security.user.password=root #設(shè)置 thymeleaf 緩存為false,表示立即生效 spring.thymeleaf.cache=false
第三步:Controller
package com.example.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { ? ? @RequestMapping("/hello") ? ? public String hello(){ ? ? ? ? System.out.println("hello spring security"); ? ? ? ? return "hello spring security"; ? ? } }
package com.example.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class IndexController { ? ? @RequestMapping("/index") ? ? public String hello(){ ? ? ? ? System.out.println("hello index"); ? ? ? ? return "hello index"; ? ? } }
package com.example.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class LoginController { ? ? @RequestMapping("/loginHtml") ? ? public String loginHtml(){ ? ? ? ? return "login"; ? ? } }
第四步:login.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org/" lang="en"> <head> ? ? <meta charset="UTF-8"> ? ? <title>用戶登錄</title> </head> <body> ? ? <h1>用戶登錄</h1> ? ? <form th:action="@{/doLogin}" method="post"> ? ? ? ? 用戶名:<input type="text" name="username"> <br> ? ? ? ? 密碼:<input type="text" name="password"><br> ? ? ? ? <input type="submit" value="登錄"> ? ? </form> </body> </html>
第五步:配置自定義登錄界面
package com.example.config; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { ? ? @Override ? ? public void configure(HttpSecurity http) throws Exception { ? ? ? ? //【注意事項】放行資源要放在前面,認證的放在后面 ? ? ? ? http.authorizeRequests() ? ? ? ? ? ? ? ? .mvcMatchers("/index").permitAll() //代表放行index的所有請求 ? ? ? ? ? ? ? ? .mvcMatchers("/loginHtml").permitAll() //放行l(wèi)oginHtml請求 ? ? ? ? ? ? ? ? .anyRequest().authenticated()//代表其他請求需要認證 ? ? ? ? ? ? ? ? .and() ? ? ? ? ? ? ? ? .formLogin()//表示其他需要認證的請求通過表單認證 ? ? ? ? ? ? ? ? //loginPage 一旦你自定義了這個登錄頁面,那你必須要明確告訴SpringSecurity日后哪個url處理你的登錄請求 ? ? ? ? ? ? ? ? .loginPage("/loginHtml")//用來指定自定義登錄界面,不使用SpringSecurity默認登錄界面 ?注意:一旦自定義登錄頁面,必須指定登錄url ? ? ? ? ? ? ? ? //loginProcessingUrl ?這個doLogin請求本身是沒有的,因為我們只需要明確告訴SpringSecurity,日后只要前端發(fā)起的是一個doLogin這樣的請求, ? ? ? ? ? ? ? ? //那SpringSecurity應(yīng)該把你username和password給捕獲到 ? ? ? ? ? ? ? ? .loginProcessingUrl("/doLogin")//指定處理登錄的請求url ? ? ? ? ? ? ? ? .and() ? ? ? ? ? ? ? ? .csrf().disable(); //禁止csrf 跨站請求保護 ? ? } }
5.1 請求參數(shù)名修改
上面的login.html用戶名必須為username,密碼必須為password,如果我們想要使用自定義的屬性名,按照如下修改
5.1.1 修改login.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org/" lang="en"> <head> ? ? <meta charset="UTF-8"> ? ? <title>用戶登錄</title> </head> <body> ? ? <h1>用戶登錄</h1> ? ? <form th:action="@{/doLogin}" method="post"> ? ? ? ? 用戶名:<input type="text" name="uname"> <br> ? ? ? ? 密碼:<input type="text" name="passwd"><br> ? ? ? ? <input type="submit" value="登錄"> ? ? </form> </body> </html>
5.1.2 修改配置類
package com.example.config; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { ? ? @Override ? ? public void configure(HttpSecurity http) throws Exception { ? ? ? ? //【注意事項】放行資源要放在前面,認證的放在后面 ? ? ? ? http.authorizeRequests() ? ? ? ? ? ? ? ? .mvcMatchers("/index").permitAll() //代表放行index的所有請求 ? ? ? ? ? ? ? ? .mvcMatchers("/loginHtml").permitAll() //放行l(wèi)oginHtml請求 ? ? ? ? ? ? ? ? .anyRequest().authenticated()//代表其他請求需要認證 ? ? ? ? ? ? ? ? .and() ? ? ? ? ? ? ? ? .formLogin()//表示其他需要認證的請求通過表單認證 ? ? ? ? ? ? ? ? //loginPage 一旦你自定義了這個登錄頁面,那你必須要明確告訴SpringSecurity日后哪個url處理你的登錄請求 ? ? ? ? ? ? ? ? .loginPage("/loginHtml")//用來指定自定義登錄界面,不使用SpringSecurity默認登錄界面 ?注意:一旦自定義登錄頁面,必須指定登錄url ? ? ? ? ? ? ? ? //loginProcessingUrl ?這個doLogin請求本身是沒有的,因為我們只需要明確告訴SpringSecurity,日后只要前端發(fā)起的是一個doLogin這樣的請求, ? ? ? ? ? ? ? ? //那SpringSecurity應(yīng)該把你username和password給捕獲到 ? ? ? ? ? ? ? ? .loginProcessingUrl("/doLogin")//指定處理登錄的請求url ? ? ? ? ? ? ? ? .usernameParameter("uname") //指定登錄界面用戶名文本框的name值,如果沒有指定,默認屬性名必須為username ? ? ? ? ? ? ? ? .passwordParameter("passwd")//指定登錄界面密碼密碼框的name值,如果沒有指定,默認屬性名必須為password ? ? ? ? ? ? ? ? .and() ? ? ? ? ? ? ? ? .csrf().disable(); //禁止csrf 跨站請求保護 ? ? } }
5.1 認證成功跳轉(zhuǎn)路徑
修改配置類successForwardUrl
package com.example.config; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { ? ? @Override ? ? public void configure(HttpSecurity http) throws Exception { ? ? ? ? //【注意事項】放行資源要放在前面,認證的放在后面 ? ? ? ? http.authorizeRequests() ? ? ? ? ? ? ? ? .mvcMatchers("/index").permitAll() //代表放行index的所有請求 ? ? ? ? ? ? ? ? .mvcMatchers("/loginHtml").permitAll() //放行l(wèi)oginHtml請求 ? ? ? ? ? ? ? ? .anyRequest().authenticated()//代表其他請求需要認證 ? ? ? ? ? ? ? ? .and() ? ? ? ? ? ? ? ? .formLogin()//表示其他需要認證的請求通過表單認證 ? ? ? ? ? ? ? ? //loginPage 一旦你自定義了這個登錄頁面,那你必須要明確告訴SpringSecurity日后哪個url處理你的登錄請求 ? ? ? ? ? ? ? ? .loginPage("/loginHtml")//用來指定自定義登錄界面,不使用SpringSecurity默認登錄界面 ?注意:一旦自定義登錄頁面,必須指定登錄url ? ? ? ? ? ? ? ? //loginProcessingUrl ?這個doLogin請求本身是沒有的,因為我們只需要明確告訴SpringSecurity,日后只要前端發(fā)起的是一個doLogin這樣的請求, ? ? ? ? ? ? ? ? //那SpringSecurity應(yīng)該把你username和password給捕獲到 ? ? ? ? ? ? ? ? .loginProcessingUrl("/doLogin")//指定處理登錄的請求url ? ? ? ? ? ? ? ? .usernameParameter("uname") //指定登錄界面用戶名文本框的name值,如果沒有指定,默認屬性名必須為username ? ? ? ? ? ? ? ? .passwordParameter("passwd")//指定登錄界面密碼密碼框的name值,如果沒有指定,默認屬性名必須為password ? ? ? ? ? ? ? ? .successForwardUrl("/index")//認證成功 forward 跳轉(zhuǎn)路徑 ? ? ? ? ? ? ? ? .and() ? ? ? ? ? ? ? ? .csrf().disable(); //禁止csrf 跨站請求保護 ? ? } }
修改配置類defaultSuccessUrl
package com.example.config; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter { ? ? @Override ? ? public void configure(HttpSecurity http) throws Exception { ? ? ? ? //【注意事項】放行資源要放在前面,認證的放在后面 ? ? ? ? http.authorizeRequests() ? ? ? ? ? ? ? ? .mvcMatchers("/index").permitAll() //代表放行index的所有請求 ? ? ? ? ? ? ? ? .mvcMatchers("/loginHtml").permitAll() //放行l(wèi)oginHtml請求 ? ? ? ? ? ? ? ? .anyRequest().authenticated()//代表其他請求需要認證 ? ? ? ? ? ? ? ? .and() ? ? ? ? ? ? ? ? .formLogin()//表示其他需要認證的請求通過表單認證 ? ? ? ? ? ? ? ? //loginPage 一旦你自定義了這個登錄頁面,那你必須要明確告訴SpringSecurity日后哪個url處理你的登錄請求 ? ? ? ? ? ? ? ? .loginPage("/loginHtml")//用來指定自定義登錄界面,不使用SpringSecurity默認登錄界面 ?注意:一旦自定義登錄頁面,必須指定登錄url ? ? ? ? ? ? ? ? //loginProcessingUrl ?這個doLogin請求本身是沒有的,因為我們只需要明確告訴SpringSecurity,日后只要前端發(fā)起的是一個doLogin這樣的請求, ? ? ? ? ? ? ? ? //那SpringSecurity應(yīng)該把你username和password給捕獲到 ? ? ? ? ? ? ? ? .loginProcessingUrl("/doLogin")//指定處理登錄的請求url ? ? ? ? ? ? ? ? .usernameParameter("uname") //指定登錄界面用戶名文本框的name值,如果沒有指定,默認屬性名必須為username ? ? ? ? ? ? ? ? .passwordParameter("passwd")//指定登錄界面密碼密碼框的name值,如果沒有指定,默認屬性名必須為password // ? ? ? ? ? ? ? ?.successForwardUrl("/index")//認證成功 forward 跳轉(zhuǎn)路徑,forward代表服務(wù)器內(nèi)部的跳轉(zhuǎn)之后,地址欄不變 始終在認證成功之后跳轉(zhuǎn)到指定請求 ? ? ? ? ? ? ? ? .defaultSuccessUrl("/index")//認證成功 之后跳轉(zhuǎn),重定向 redirect 跳轉(zhuǎn)后,地址會發(fā)生改變 ?根據(jù)上一保存請求進行成功跳轉(zhuǎn) ? ? ? ? ? ? ? ? .and() ? ? ? ? ? ? ? ? .csrf().disable(); //禁止csrf 跨站請求保護 ? ? } }
訪問http://localhost:8080/hello,認證后
發(fā)現(xiàn)并沒有到/index的情況,這是defaultSuccessUrl一特性,如果你想硬跳到/index,修改java defaultSuccessUrl("/index",true)即可
訪問http://localhost:8080/loginHtml,認證后
defaultSuccessUrl和successForwardUrl區(qū)別
1、successForwardUrl是forward跳轉(zhuǎn),defaultSuccessUrl是重定向redirect跳轉(zhuǎn)
2、successForwardUrl始終在認證成功之后跳轉(zhuǎn)到指定請求,defaultSuccessUrl根據(jù)上一保存請求進行成功跳轉(zhuǎn)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合SpringDataRedis的示例代碼
這篇文章主要介紹了SpringBoot整合SpringDataRedis的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05SpringBoot返回統(tǒng)一的JSON標(biāo)準(zhǔn)格式實現(xiàn)步驟
這篇文章主要介紹了SpringBoot返回統(tǒng)一的JSON標(biāo)準(zhǔn)格式,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08commons fileupload實現(xiàn)文件上傳的實例代碼
這篇文章主要介紹了commons fileupload實現(xiàn)文件上傳的實例代碼,包括文件上傳的原理分析等相關(guān)知識點,本文給大家介紹的非常詳細,具有參考借鑒價值,感興趣的朋友一起看看吧2016-10-10java讀寫ini文件、FileOutputStream問題
這篇文章主要介紹了java讀寫ini文件、FileOutputStream問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04IDEA實現(xiàn)序列化時如何自動生成serialVersionUID的步驟
這篇文章主要介紹了IDEA實現(xiàn)序列化時如何自動生成serialVersionUID的步驟,首先安裝GenerateSerialVersionUID插件,當(dāng)出現(xiàn)添加serialVersionUID選項,選中則會自動生成serialVersionUID,感興趣的朋友一起學(xué)習(xí)下吧2024-02-02Java并發(fā)編程中的CompletableFuture使用詳解
這篇文章主要介紹了Java并發(fā)編程中的CompletableFuture使用詳解,Future接口定義了操作異步任務(wù)執(zhí)行的一些方法,如獲取異步任務(wù)執(zhí)行的結(jié)果、取消任務(wù)的執(zhí)行、判斷任務(wù)是否被取消,判斷任務(wù)是否執(zhí)行完畢等,需要的朋友可以參考下2023-12-12