您好,登錄后才能下訂單哦!
小編給大家分享一下spring中如何自定義登陸頁(yè)面和主頁(yè),希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
1、添加模版引擎
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2、配置登陸頁(yè)面路徑
在WebSecurityConfig復(fù)寫configure(HttpSecurity http),復(fù)寫登陸頁(yè)面的路徑。 @Override protected void configure (HttpSecurity http) throws Exception{ http.formLogin() .loginPage("/login"); }
3、新建登陸頁(yè)面,并跳轉(zhuǎn)到登陸頁(yè)
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>login333</title> </head> <body> <div th:if="${param.error}"> 用戶名密碼錯(cuò)誤,要不去<a th:href="@{/}">首頁(yè)</a>看看? </div> <div th:if="${param.logout}"> 您已經(jīng)登陸 </div> <form th:action="@{/login}" method="post"> <div><label>用戶名:<input type="text" name="username"></label></div> <div><label>密碼: <input type="password" name="password"></label></div> <div><input type="submit" value="登陸"></div> </form> </body> </html> @GetMapping("/login") public String login() { return "/login"; }
This application has no explicit mapping for /error, so you are seeing this as a fallback.
語(yǔ)法錯(cuò)誤
高亮語(yǔ)法
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
4、新建主頁(yè),并跳轉(zhuǎn)到主頁(yè)
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>首頁(yè)</title> </head> <body> <h2>歡迎光臨</h2> <p>點(diǎn)擊<a th:href="@{/hello}">這里</a>打個(gè)招呼吧</p> <form th:action="@{/logout}" method="post"> <input type="submit" value="退出登陸"> </form> <p><a th:href="@{/hello/helloAdmin}">管理員</a></p> <p><a th:href="@{/hello/helloUser}">普通用戶</a></p> </body> </html> @GetMapping({"","/","/index"}) public String index() { return "/index"; }
5、沒(méi)有權(quán)限頁(yè)面顯示,及所有人可以訪問(wèn)登陸頁(yè),所有請(qǐng)求都要登陸后才能訪問(wèn)
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>沒(méi)有權(quán)限</title> </head> <body> <h2 th:inline="text">不好意思,您沒(méi)有訪問(wèn)權(quán)限</h2> <p><a th:href="@{/hello/helloUser}">前往普通用戶界面</a></p> <form th:if="${param.logout}}" method="退出登陸"> <input type="submit" value="退出登陸"> </form> </body> </html> @Override protected void configure (HttpSecurity http) throws Exception{ http.formLogin() .loginPage("/login") .and() .authorizeRequests() .antMatchers("/login").permitAll() //允許所有人可以訪問(wèn)登陸頁(yè)面 .anyRequest().authenticated();//所有的請(qǐng)求需要在登陸之后才能訪問(wèn) }
看完了這篇文章,相信你對(duì)“spring中如何自定義登陸頁(yè)面和主頁(yè)”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。