SpringSecurity入门demo(二)表单认证

上一篇博客集成 Spring Security,使用其默认生效的 HTTP 基本认证保护 URL 资源,下面使用表单认证来保护 URL 资源。

一、默认表单认证:

代码改动:自定义WebSecurityConfig配置类

package com.security.demo.config;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
}

因为WebSecurityConfigurerAdapter的configure(HttpSecurity http)方法自带默认的表单身份认证,这里继承后不做方法修改,启动项目,这时访问localhost:8089/securityDemo/user/test仍然会跳转到默认的登陆页

二、自定义表单登陆:

1、自定义表单登陆页:

  代码改动:

(1)覆盖configure(HttpSecurity http)方法

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{protected void configure(HttpSecurity http) throws Exception{http.authorizeRequests().anyRequest().authenticated().and()	.formLogin().loginPage("/myLogin.html")// 使登录页不设限访问.permitAll().and().csrf().disable();}
}

(2)编写自定义的登陆页myLogin.html,放在resources/static/ 

<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>登录</title>
</head>
<body>
<div class = "login" style="width:300px;height:300px"><h2>Acced Form</h2><div class ="login-top"></div><h1>LOGIN FORM</h1><form action="myLogin.html" method="post"><input type="text" name="username" placeholder="username"/><input type="password" name="password" placeholder="password"/><div class="forgot" style="margin-top:20px;"><a href="#">forgot Password</a><input type="submit" value="login"></div></form><div class="login-bottom"><h3>New User &nbsp;<a href ="">Register</a>&nbsp;&nbsp;</h3></div>
</div>
</body>
</html>

访问localhost:8089/securityDemo/user/test会自动跳转到localhost:8089/securityDemo/static/myLogin.html

2、自定义登陆接口地址: 如自定义登陆接口为/login,代码改动:

(1)覆盖方法:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{protected void configure(HttpSecurity http) throws Exception{http.authorizeRequests().anyRequest().authenticated().and().formLogin()//	.loginPage("/myLogin.html").loginProcessingUrl("/login").permitAll().and().csrf().disable();}
}

(2)新增/login接口

package com.security.demo.controller;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class Login {@RequestMapping("/login")public String login(String username,String password){System.out.println("用户名:"+username+",密码:"+password);return "登陆成功";}
}

重启后访问localhost:8089/securityDemo/user/test,自动跳转到spring默认的登陆页

输入user、控制台打印的密码,点击登陆按钮,可以看到调用了/login接口

调用成功后自动跳转到目标接口

注意:测试发现这个/login接口去掉也可以。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/620160.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Next.js 集成 Auth0 登入和自定义登入页面

Next.js 集成 Auth0 和自定义登入页面 注册账号和基本配置进入 auth0 官网注册账号并登入进入控制台后访问 Applications/Applications进入程序配置页面添加配置 在 Next.js 使用在项目中集成 通过 Auth0Lock 配置方式自定义登入页面效果展示实现过程 注册账号和基本配置 进入…

Pandas实战100例 | 案例 15: 移动平均 - 使用 `rolling` 方法

案例 15: 移动平均 - 使用 rolling 方法 知识点讲解 移动平均是时间序列数据分析中的一种基本技术&#xff0c;用于平滑时间序列中的短期波动并突出长期趋势。Pandas 的 rolling 方法提供了计算移动平均的简便方式。 计算移动平均: 使用 rolling 方法&#xff0c;你可以指定…

Java 数组常见的排序和查找算法

2、数组 2.1、常见的算法&#xff1a; 排序算法&#xff1a; 冒泡排序算法 选择排序算法 查找算法&#xff1a; 二分法查找2.2、算法实际上在 java 中已经封装好了。 排序可以调用方法。例如&#xff1a;java 中提供了一个数组工具类&#xff1a; java.util.Arrays Arrays 是一…

[渗透测试学习] Appointment - HackTheBox

文章目录 Task 1Task 2Task 3Task 4Task 5Task 6Task 7Task 8Task 9Task 10Task 1 SQL 缩写代表什么? Structured Query LanguageTask 2 最常见的 SQL 漏洞类型之一是什么? SQL injectionTask 3 此漏洞的 2021 年 OWASP Top 10 分类是什么?

scroll-view在小程序页面里实现滚动,uniapp项目

要实现红框中的区域进行滚动,scroll-view必须写高 <template><!-- 合同-待确认 --><view class"viewport"><!-- 上 --><view class"top-box"><!-- tab --><view class"tabs"><textv-for"(ite…

Alibaba-> EasyExcel 整理3

1 导入依赖 <!-- easyExcel --><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version >3.2.1</version><exclusions><exclusion><artifactId>poi-ooxml-schemas</art…

css垂直水平居中的几种实现方式

垂直水平居中的几种实现方式 一、固定宽高&#xff1a; 1、定位 margin-top margin-left .box-container{position: relative;width: 300px;height: 300px;}.box-container .box {width: 200px; height: 100px;position: absolute; left: 50%; top: 50%;margin-top: -50px;…

what is BERT?

BERT Introduction Paper 参考博客 9781838821593_ColorImages.pdf (packt-cdn.com) Bidirectional Encoder Representation from Transformer 来自Transformer的双向编码器表征 基于上下文&#xff08;context-based&#xff09;的嵌入模型。 那么基于上下文&#xff08;…

【MySQL性能优化】- MySQL结构与SQL执行过程

MySQL结构与SQL执行过程 &#x1f604;生命不息&#xff0c;写作不止 &#x1f525; 继续踏上学习之路&#xff0c;学之分享笔记 &#x1f44a; 总有一天我也能像各位大佬一样 &#x1f3c6; 博客首页 怒放吧德德 To记录领地 &#x1f31d;分享学习心得&#xff0c;欢迎指正…

自然语言处理笔记

文章目录 情感词典中文分词 情感词典 英文的情感词典有&#xff1a;LIWC, SentiWordNet等 中文的情感词典有&#xff1a;NTUSD, 正文褒贬词典TSING, 知网HowNet等 中文分词 中文分词的工具有&#xff1a;jieba(核心算法是张华平的Nshort算法)&#xff0c; SnowNLP&#xff0…

开源了一款Vue3 Ts Vite4 uni-app 驱动的跨端快速启动模板

一、开源项目简介 由 Vue3 & Ts & Vite4 & uni-app 驱动的跨端快速启动模板。推荐使用 VSCode 编辑器开发&#xff0c;集成了 Prettier ESLint StyleLint husky lint-staged commitlint UnoCSS Vue3 TypeScript Vite4 setup&#xff0c;开箱即用。 二、…

【计算机组成原理】程序的转换及机器级表示 常用计算机术语英文缩写汇总

编码 二进制编码的十进制数&#xff08;BCD&#xff09;&#xff1a;Binary Coded Decimal美国信息交换标准代码&#xff08;ASCII&#xff09;&#xff1a;American Standard Code for Information Interchange 数据的排列顺序 最低有效位&#xff08;LSB&#xff09;&…

全链路追踪关键技术-TraceId、SpanId生成规则

链路追踪的traceid原理梳理 如何追踪微服务调用&#xff1f; ● traceId&#xff0c;用于标识某一次具体的请求ID。当用户的请求进入系统后&#xff0c;会在RPC调用网络的第一层生成一个全局唯一的traceId&#xff0c;并且会随着每一层的RPC调用&#xff0c;不断往后传递&…

day17 平衡二叉树 二叉树的所有路径 左叶子之和

题目1&#xff1a;110 平衡二叉树 题目链接&#xff1a;110 平衡二叉树 题意 判断二叉树是否为平衡二叉树&#xff08;每个节点的左右两个子树的高度差绝对值不超过1&#xff09; 递归遍历 递归三部曲 1&#xff09;确定递归函数的参数和返回值 2&#xff09;确定终止条…

uniapp小程序当页面内容超出时显示滚动条,不超出时不显示---样式自定义

使用scroll-view中的show-scrollbar属性 注意:需要搭配enhanced使用 否则无效 <scroll-view class"contentshow" scroll-y :show-scrollbartrue :enhancedtrue><view class"content" :show-scrollbartrue><text>{{vehicleCartinfo}}<…

【动态规划】LeetCode-42. 接雨水

42. 接雨水。 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图&#xff0c;计算按此排列的柱子&#xff0c;下雨之后能接多少雨水。 示例 1&#xff1a; 输入&#xff1a;height [0,1,0,2,1,0,1,3,2,1,2,1] 输出&#xff1a;6 解释&#xff1a;上面是由数组 [0,1,0,2,1…

Quartus 软件界面介绍与部分使用技巧

内容太多&#xff0c;只能慢慢补充完善了~ 对一个软件的熟练掌握&#xff0c;不仅在于完成项目工程&#xff0c;还在于对一个软件的各个功能的位置与使用要熟稔于心&#xff08;个人看法&#xff09;。 软件界面 默认打开的软件界面如下&#xff1a; 关掉所有能关闭的窗口&am…

怎么安装IK分词器

.安装IK分词器 1.在线安装ik插件&#xff08;较慢&#xff09; # 进入容器内部 docker exec -it elasticsearch /bin/bash ​ # 在线下载并安装 ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.1/elastics…

学习笔记-数据容器:dict(字典)

一.字典的定义 1.语法 使用{ }存储原始&#xff0c;每一个元素是一个键值对每一个键值对包含Key和Value&#xff08;用冒号分隔&#xff09;键值对之间使用逗号分隔Key和Value可以是任意类型的数据&#xff08;key不可为可变类型(列表集合字典)&#xff09;Key不可重复&#…

LeetCode304. Range Sum Query 2D - Immutable

文章目录 一、题目二、题解 一、题目 Given a 2D matrix matrix, handle multiple queries of the following type: Calculate the sum of the elements of matrix inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2)…