SpringSecurity自定义登录界面
为什么需要自定义登录界面?
答:因为SpringBoot整合SpringSecurity时,只需要一个依赖,无需其他配置,就可以实现认证功能。但是它的认证登录界面是固定那样的,如下图所示,但是我们希望自己搞个好看的登录界面,所以需要自定义登录界面。
第一步:创建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#设置 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 {@Overridepublic void configure(HttpSecurity http) throws Exception {//【注意事项】放行资源要放在前面,认证的放在后面http.authorizeRequests().mvcMatchers("/index").permitAll() //代表放行index的所有请求.mvcMatchers("/loginHtml").permitAll() //放行loginHtml请求.anyRequest().authenticated()//代表其他请求需要认证.and().formLogin()//表示其他需要认证的请求通过表单认证//loginPage 一旦你自定义了这个登录页面,那你必须要明确告诉SpringSecurity日后哪个url处理你的登录请求.loginPage("/loginHtml")//用来指定自定义登录界面,不使用SpringSecurity默认登录界面 注意:一旦自定义登录页面,必须指定登录url//loginProcessingUrl 这个doLogin请求本身是没有的,因为我们只需要明确告诉SpringSecurity,日后只要前端发起的是一个doLogin这样的请求,//那SpringSecurity应该把你username和password给捕获到.loginProcessingUrl("/doLogin")//指定处理登录的请求url.and().csrf().disable(); //禁止csrf 跨站请求保护}
}
5.1 请求参数名修改
上面的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 {@Overridepublic void configure(HttpSecurity http) throws Exception {//【注意事项】放行资源要放在前面,认证的放在后面http.authorizeRequests().mvcMatchers("/index").permitAll() //代表放行index的所有请求.mvcMatchers("/loginHtml").permitAll() //放行loginHtml请求.anyRequest().authenticated()//代表其他请求需要认证.and().formLogin()//表示其他需要认证的请求通过表单认证//loginPage 一旦你自定义了这个登录页面,那你必须要明确告诉SpringSecurity日后哪个url处理你的登录请求.loginPage("/loginHtml")//用来指定自定义登录界面,不使用SpringSecurity默认登录界面 注意:一旦自定义登录页面,必须指定登录url//loginProcessingUrl 这个doLogin请求本身是没有的,因为我们只需要明确告诉SpringSecurity,日后只要前端发起的是一个doLogin这样的请求,//那SpringSecurity应该把你username和password给捕获到.loginProcessingUrl("/doLogin")//指定处理登录的请求url.usernameParameter("uname") //指定登录界面用户名文本框的name值,如果没有指定,默认属性名必须为username.passwordParameter("passwd")//指定登录界面密码密码框的name值,如果没有指定,默认属性名必须为password.and().csrf().disable(); //禁止csrf 跨站请求保护}
}
5.1 认证成功跳转路径
修改配置类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 {@Overridepublic void configure(HttpSecurity http) throws Exception {//【注意事项】放行资源要放在前面,认证的放在后面http.authorizeRequests().mvcMatchers("/index").permitAll() //代表放行index的所有请求.mvcMatchers("/loginHtml").permitAll() //放行loginHtml请求.anyRequest().authenticated()//代表其他请求需要认证.and().formLogin()//表示其他需要认证的请求通过表单认证//loginPage 一旦你自定义了这个登录页面,那你必须要明确告诉SpringSecurity日后哪个url处理你的登录请求.loginPage("/loginHtml")//用来指定自定义登录界面,不使用SpringSecurity默认登录界面 注意:一旦自定义登录页面,必须指定登录url//loginProcessingUrl 这个doLogin请求本身是没有的,因为我们只需要明确告诉SpringSecurity,日后只要前端发起的是一个doLogin这样的请求,//那SpringSecurity应该把你username和password给捕获到.loginProcessingUrl("/doLogin")//指定处理登录的请求url.usernameParameter("uname") //指定登录界面用户名文本框的name值,如果没有指定,默认属性名必须为username.passwordParameter("passwd")//指定登录界面密码密码框的name值,如果没有指定,默认属性名必须为password.successForwardUrl("/index")//认证成功 forward 跳转路径.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 {@Overridepublic void configure(HttpSecurity http) throws Exception {//【注意事项】放行资源要放在前面,认证的放在后面http.authorizeRequests().mvcMatchers("/index").permitAll() //代表放行index的所有请求.mvcMatchers("/loginHtml").permitAll() //放行loginHtml请求.anyRequest().authenticated()//代表其他请求需要认证.and().formLogin()//表示其他需要认证的请求通过表单认证//loginPage 一旦你自定义了这个登录页面,那你必须要明确告诉SpringSecurity日后哪个url处理你的登录请求.loginPage("/loginHtml")//用来指定自定义登录界面,不使用SpringSecurity默认登录界面 注意:一旦自定义登录页面,必须指定登录url//loginProcessingUrl 这个doLogin请求本身是没有的,因为我们只需要明确告诉SpringSecurity,日后只要前端发起的是一个doLogin这样的请求,//那SpringSecurity应该把你username和password给捕获到.loginProcessingUrl("/doLogin")//指定处理登录的请求url.usernameParameter("uname") //指定登录界面用户名文本框的name值,如果没有指定,默认属性名必须为username.passwordParameter("passwd")//指定登录界面密码密码框的name值,如果没有指定,默认属性名必须为password
// .successForwardUrl("/index")//认证成功 forward 跳转路径,forward代表服务器内部的跳转之后,地址栏不变 始终在认证成功之后跳转到指定请求.defaultSuccessUrl("/index")//认证成功 之后跳转,重定向 redirect 跳转后,地址会发生改变 根据上一保存请求进行成功跳转.and().csrf().disable(); //禁止csrf 跨站请求保护}
}
访问http://localhost:8080/hello,认证后
发现并没有到/index的情况,这是defaultSuccessUrl一特性,如果你想硬跳到/index,修改java defaultSuccessUrl("/index",true)
即可
访问http://localhost:8080/loginHtml,认证后
defaultSuccessUrl和successForwardUrl区别
- successForwardUrl是forward跳转,defaultSuccessUrl是重定向redirect跳转
- successForwardUrl始终在认证成功之后跳转到指定请求,defaultSuccessUrl根据上一保存请求进行成功跳转
SpringSecurity自定义登录界面相关推荐
- FineReport中如何自定义登录界面
在登录平台时,不希望使用FR默认的内置登录界面,想通过自定义登录界面实现登录操作,内置登录界面如下图: 登录界面,获取到用户名和密码的值,发送到报表系统,报表服务带着这两个参数访问认证地址进行认证. ...
- 帆软finereport决策系统自定义登录界面
通过前面对决策系统登录liu'chengliucheng 的梳理,发现通过改变AppearanceConfig的loginType与loginUrl即可实现跳转到自定义的登录界面. 第一步:先创建一个 ...
- finereport php,FineReport中自定义登录界面的方法
在登录平台时,不希望使用FR默认的内置登录界面,想通过自定义登录界面实现登录操作,内置登录界面如下图: 登录界面,获取到用户名和密码的值,发送到报表系统,报表服务带着这两个参数访问认证地址进行认证. ...
- 帆软自定义登录html,FineReport中如何自定义登录界面
在登录平台时,不希望使用FR默认的内置登录界面,想通过自定义登录界面实现登录操作,内置登录界面如下图: 登录界面,获取到用户名和密码的值,发送到报表系统,报表服务带着这两个参数访问认证地址进行认证. ...
- finereport 登录界面的代码文件_Confluence 6 自定义登录界面
这个页面将会给你一些如何对 Confluence 登录界面进行自定义的一些建议和知道.这个将不会自定义用户的登录过程,只是对用户看到的登录界面进行自定义. 备注: 自定义过后的 Confluence ...
- Ant Design Pro V5精讲(实践篇一):自定义登录界面、主界面
用户需求 登录界面改造成自己的 主界面的logo及产品名称改造成自己的 语言包去掉或者只留中英语言包 登录界面改造 进入pages/user/login目录找到index.tsx // 去掉语言包栏目 ...
- Linux安装Gnome桌面并自定义登录界面
最近想安装gnome,发现网上的教程很多,也不一样.我也自己测试了一下,其实操作很简单.下面就开始介绍如何安装并配置好桌面程序. 1,安装gnome 我们只需要执行一个命令即可: sudo apt i ...
- SpringSecurity学习笔记(三)自定义资源拦截规则以及登录界面跳转
参考视频,编程不良人 由前面的学习可以知道,SS的默认的拦截规则很简单,我们在项目中实际使用的时候往往需要更加复杂的拦截规则,这个时候就需要自定义一些拦截规则. 自定义拦截规则 在我们的项目中,资源往 ...
- SpringSecurity基础:自定义登录和登出
自定义登录界面 前面我们已经了解了如何实现数据库权限验证,那么现在我们接着来看看,如何将登陆页面修改为我们自定义的样式. 首先我们要了解一下SpringSecurity是如何进行登陆验证的,我们可以观 ...
最新文章
- 基于R语言的时间序列分析预测
- hc05与单片机连接图_单片机科普:单片机的IO口不够用了怎么办?如何扩展单片机的IO口...
- MySQL—数据库表的完整性约束(非外键约束)
- window如何杀掉java进程_windows下java -jar 后台运行以及杀死后台进程的操作
- 广西高职计算机网络技术,计算机网络技术专业考试大纲-广西职业技术学院.DOC...
- 8位16进制频率计设计实验--VHDL
- 计划任务执行 php代码,php实现Windows任务计划定时执行的代码以及扩展(图)
- 读书:我编程我快乐(一.4)
- mysql10---索引优化
- UVA10317- Equating Equations(回溯+剪枝)
- 竞赛经验——全国大学生创新创业训练计划(国创)
- go-fastDFS 分布式文件系统搭建(实现梳理)
- XP系统时间同步和开启WindowsTime服务
- 国外哪个服务器最稳定,合适的国外服务器有什么选择技巧
- 云原生数据湖以存储、计算、数据管理等能力通过信通院评测认证
- MySQL数据的备份与恢复
- 百度之星du熊学斐波那契I-C++实现
- C++字符串常用输入方法
- Linux下文件解压缩、软件安装
- java相关的外文文献及翻译_外文文献及翻译:JavaandtheInternet