javascript
Springsecurity搭建自定义登录页面
Springsecurity搭建自定義登錄頁(yè)面
1.springSecurity的搭建
新建一個(gè)springboot的web項(xiàng)目,我這邊只選中了web,建立后如下:
image.pngpom依賴:
<!-- 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來(lái)繼承WebSecurityConfigurerAdapter
WebSecurityConfigurerAdapter是security中瀏覽器登錄設(shè)置的主類 這里我們繼承后重寫以下的三個(gè)方法:
- HttpSecurity(HTTP請(qǐng)求安全處理)
- AuthenticationManagerBuilder(身份驗(yàn)證管理生成器)
- WebSecurity(WEB安全)
這邊我們指定的登錄頁(yè)的訪問方法為/Hello的方法,這邊我們來(lái)編寫這個(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",否則無(wú)法識(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)目 無(wú)論進(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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Unity 之命名规范(一)
- 下一篇: 关于素数的那些事儿