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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

Springsecurity搭建自定义登录页面

發(fā)布時(shí)間:2025/3/17 javascript 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Springsecurity搭建自定义登录页面 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Springsecurity搭建自定義登錄頁(yè)面

1.springSecurity的搭建

新建一個(gè)springboot的web項(xiàng)目,我這邊只選中了web,建立后如下:

image.png

pom依賴:

<!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper --><!--配置支持jsp--><dependency><groupId>org.apache.tomcat.embed</groupId><artifactId>tomcat-embed-jasper</artifactId><version>8.5.12</version></dependency><!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version></dependency><!-- https://mvnrepository.com/artifact/javax.servlet/jstl --><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!--添加static和templates的依賴--><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.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><!-- 由于我使用的spring boot所以我是引入spring-boot-starter-security而且我使用了spring io所以不需要填寫依賴的版本號(hào) --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>

以上的jsp依賴如果用不上可以不加哦

2.編寫SecurityConfiguration來繼承WebSecurityConfigurerAdapter

WebSecurityConfigurerAdapter是security中瀏覽器登錄設(shè)置的主類 這里我們繼承后重寫以下的三個(gè)方法:

  • HttpSecurity(HTTP請(qǐng)求安全處理)
  • AuthenticationManagerBuilder(身份驗(yàn)證管理生成器)
  • WebSecurity(WEB安全)
import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@Configuration public class SecurityConfiguration extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {super.configure(auth);}@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().antMatchers("/hello","/login.html").permitAll().anyRequest().authenticated().and().formLogin()//指定登錄頁(yè)的路徑.loginPage("/hello")//指定自定義form表單請(qǐng)求的路徑.loginProcessingUrl("/authentication/form").failureUrl("/login?error").defaultSuccessUrl("/success")//必須允許所有用戶訪問我們的登錄頁(yè)(例如未驗(yàn)證的用戶,否則驗(yàn)證流程就會(huì)進(jìn)入死循環(huán))//這個(gè)formLogin().permitAll()方法允許所有用戶基于表單登錄訪問/login這個(gè)page。.permitAll();//默認(rèn)都會(huì)產(chǎn)生一個(gè)hiden標(biāo)簽 里面有安全相關(guān)的驗(yàn)證 防止請(qǐng)求偽造 這邊我們暫時(shí)不需要 可禁用掉http .csrf().disable();}@Overridepublic void configure(WebSecurity web) throws Exception {super.configure(web);}}

這邊我們指定的登錄頁(yè)的訪問方法為/Hello的方法,這邊我們來編寫這個(gè)Controller層:

@Controller public class LoginController {@RequestMapping("/hello")public String hello() {//這邊我們,默認(rèn)是返到templates下的login.htmlreturn "login";} }

login.html:

<!DOCTYPE html> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <head><meta charset="UTF-8"><title>第一個(gè)HTML頁(yè)面</title> </head> <html lang="en"> <head><meta charset="UTF-8"><title>Title</title> </head> <body> 自定義表單驗(yàn)證: <!--<form name="f" action="/login" method="post">--><form name="f" action="/authentication/form" method="post"> <br/> 用戶名: <input type="text" name="username" placeholder="name"><br/> 密碼: <input type="password" name="password" placeholder="password"><br/> <input name="submit" type="submit" value="提交"> </form> </body> </html>

這里值的注意的是表單的用戶名name和password輸入框的name=""要和security里面的驗(yàn)證的對(duì)應(yīng):

name="username";name="password",否則無法識(shí)別,另外action="/authentication/form"要與.loginProcessingUrl("/authentication/form")相對(duì)應(yīng),原因?yàn)?

由于security是由UsernamePasswordAuthenticationFilter這個(gè)類定義登錄的,里面默認(rèn)是/login路徑,我們要讓他用我們的/authentication/form路徑,就需要配置.loginProcessingUrl("/authentication/form")

3.項(xiàng)目啟動(dòng)

我們現(xiàn)在啟動(dòng)項(xiàng)目 無論進(jìn)入哪個(gè)網(wǎng)址都會(huì)被攔截返回到登錄頁(yè)面,如下所示:

image.png

這時(shí)我們用戶名:user(默認(rèn)) password:會(huì)在啟動(dòng)時(shí)候生成 如下:

image.png

這個(gè)時(shí)候我們登錄就成功了 ,否則不正確會(huì)返回到error頁(yè)面

總結(jié)

以上是生活随笔為你收集整理的Springsecurity搭建自定义登录页面的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。