当前位置:网站首页>failureForwardUrl与failureUrl
failureForwardUrl与failureUrl
2022-04-23 09:46:00 【搞钱自律】
相同点
都是对认证失败后跳转页面情况的处理
不同点
- failureForwardUrl 是forward 跳转 ,failureUrl 是redirect 跳转
- failureForwardUrl异常信息存储在request中,failureUrl认证失败异常信息存储在session中
failureForwardUrl
因为是表单认证,所以从formLogin()方法进入看看,底层是通过UsernamePasswordAuthenticationFilter这个过滤器的attemptAuthentication方法进行认证的,所以在attemptAuthentication方法里打断点,可以进行调试。发现会进入到ForwardAuthenticationFailureHandler处理器。
package com.example.config;
import com.example.handler.MyAuthenticationSuccessHandler;
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 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 跳转后,地址会发生改变 根据上一保存请求进行成功跳转
.successHandler(new MyAuthenticationSuccessHandler()) //认证成功时处理 前后端分离解决方案
.failureForwardUrl("/loginHtml")//认证失败之后 forward 跳转
// .failureUrl("/login.html")//认证失败之后 redirect 跳转
.and()
.csrf().disable(); //禁止csrf 跨站请求保护
}
}
注意事项
failureForwardUrl
设置为/login.html,是无法获取到异常信息的,因为当浏览器发起doLogin请求到服务端时,我们输入错误的认证消息后,服务端会发起/login.html请求,但是这个/login.html虽然有界面,但是在我们的配置里并没有开放,所以我们会发现他是个302的返回码
mvcMatchers
只作用于后端接口就是controller,像html,js,img资源写上去无controller跳转,是无效的
其实failureUrl设置的/login.html也是一样的
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/" lang="en">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
<h2 th:text="${#request.getAttribute('SPRING_SECURITY_LAST_EXCEPTION')}">
</h2>
<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>
failureUrl
因为是表单认证,所以从formLogin()方法进入看看,底层是通过UsernamePasswordAuthenticationFilter这个过滤器的attemptAuthentication方法进行认证的,所以在attemptAuthentication方法里打断点,可以进行调试。发现会进入到SimpleUrlAuthenticationFailureHandler处理器。
package com.example.config;
import com.example.handler.MyAuthenticationSuccessHandler;
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 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 跳转后,地址会发生改变 根据上一保存请求进行成功跳转
.successHandler(new MyAuthenticationSuccessHandler()) //认证成功时处理 前后端分离解决方案
// .failureForwardUrl("/login.html")//认证失败之后 forward 跳转
.failureUrl("/login.html")//认证失败之后 redirect 跳转
.and()
.csrf().disable(); //禁止csrf 跨站请求保护
}
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/" lang="en">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
<h2 th:text="${#httpSession.getAttribute('SPRING_SECURITY_LAST_EXCEPTION')}">
</h2>
<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>
版权声明
本文为[搞钱自律]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43472934/article/details/124333734
边栏推荐
- 高薪程序员&面试题精讲系列91之Limit 20000加载很慢怎么解决?如何定位慢SQL?
- Using JS to realize a thousandth bit
- 论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——3背景
- [COCI] lattice (dichotomy + tree divide and conquer + string hash)
- C语言:表达式求值(整型提升、算术转换 ...)
- KVM installation and deployment
- [codeforces - 208e] blood cousins
- Kettle experiment
- Number theory blocking (integer division blocking)
- Leetcode0587. 安装栅栏(difficult)
猜你喜欢
Acquisition of DOM learning elements JS
元宇宙时代的职业规划与执行
Simple understanding of arguments in JS
高薪程序员&面试题精讲系列91之Limit 20000加载很慢怎么解决?如何定位慢SQL?
面试官:说几个PHP常用函数,幸好我面试之前看到了这篇文章
Easy to understand subset DP
Solving Lucas number and combination theorem
Yyds dry goods inventory ubuntu18 0.4 install MySQL and solve error 1698: access denied for user ''root' '@' 'localhost' '
Number theory blocking (integer division blocking)
SAP debug debug for in, reduce and other complex statements
随机推荐
[reading notes] Chapter 5 conditional statements, circular statements and block statements of Verilog digital system design tutorial (with answers to thinking questions)
Unfortunately, I broke the leader's confidential documents and spit blood to share the code skills of backup files
JS and how to judge custom attributes in H5
Leetcode question bank 78 Subset (recursive C implementation)
Using JS to realize a thousandth bit
JS scope, scope chain, global variables and local variables
ASUS laptop can't read USB and surf the Internet after reinstalling the system
Leetcode0587. Install fence
SQL used query statements
[2020wc Day2] F. Clarice picking mushrooms (subtree and query, light and heavy son thought)
Your guide to lowering your cholesterol with TLC (continuously updated)
[ACM-ICPC 2018 Shenyang Network preliminaries] J. Ka Chang (block + DFS sequence)
Secrets in buffctf file 1
What is monitoring intelligent playback and how to use intelligent playback to query video recording
How to use SQL statement union to get another column of another table when the content of a column in a table is empty
Nine abilities of agile manufacturing in the era of meta universe
《數字電子技術基礎》3.1 門電路概述、3.2 半導體二極管門電路
Learn FPGA (from Verilog to HLS)
F-niu Mei's apple tree (diameter combined)
Using sqlmap injection to obtain the account and password of the website administrator