如何实现SpringSecurity中自定义的登录页面设计?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1135个文字,预计阅读时间需要5分钟。
为什么需要自定义登录界面?答:因为当SpringBoot整合SpringSecurity时,只需一个依赖,无需其他配置,即可实现认证功能。但是它的认证登录界面是固定的,如上图所示,而我们希望我们的登录界面。
为什么需要自定义登录界面?
答:因为SpringBoot整合SpringSecurity时,只需要一个依赖,无需其他配置,就可以实现认证功能。但是它的认证登录界面是固定那样的,如下图所示,但是我们希望自己搞个好看的登录界面,所以需要自定义登录界面。
第一步:创建springboot项目
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="maven.apache.org/POM/4.0.0" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="maven.apache.org/POM/4.0.0 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 #设置 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="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 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 localhost:8080/hello,认证后
发现并没有到/index的情况,这是defaultSuccessUrl一特性,如果你想硬跳到/index,修改java defaultSuccessUrl("/index",true)即可
访问localhost:8080/loginHtml,认证后
defaultSuccessUrl和successForwardUrl区别
1、successForwardUrl是forward跳转,defaultSuccessUrl是重定向redirect跳转
2、successForwardUrl始终在认证成功之后跳转到指定请求,defaultSuccessUrl根据上一保存请求进行成功跳转
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计1135个文字,预计阅读时间需要5分钟。
为什么需要自定义登录界面?答:因为当SpringBoot整合SpringSecurity时,只需一个依赖,无需其他配置,即可实现认证功能。但是它的认证登录界面是固定的,如上图所示,而我们希望我们的登录界面。
为什么需要自定义登录界面?
答:因为SpringBoot整合SpringSecurity时,只需要一个依赖,无需其他配置,就可以实现认证功能。但是它的认证登录界面是固定那样的,如下图所示,但是我们希望自己搞个好看的登录界面,所以需要自定义登录界面。
第一步:创建springboot项目
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="maven.apache.org/POM/4.0.0" xmlns:xsi="www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="maven.apache.org/POM/4.0.0 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 #设置 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="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 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 localhost:8080/hello,认证后
发现并没有到/index的情况,这是defaultSuccessUrl一特性,如果你想硬跳到/index,修改java defaultSuccessUrl("/index",true)即可
访问localhost:8080/loginHtml,认证后
defaultSuccessUrl和successForwardUrl区别
1、successForwardUrl是forward跳转,defaultSuccessUrl是重定向redirect跳转
2、successForwardUrl始终在认证成功之后跳转到指定请求,defaultSuccessUrl根据上一保存请求进行成功跳转
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

