当前位置:网站首页>Failureforwardurl and failureurl
Failureforwardurl and failureurl
2022-04-23 09:50:00 【Engage in money self-discipline】
The same thing
All of them deal with the situation of jumping to the page after authentication failure
Difference
- failureForwardUrl yes forward Jump ,failureUrl yes redirect Jump
- failureForwardUrl Exception information is stored in request in ,failureUrl The authentication failure exception information is stored in session in
failureForwardUrl
Because it's form authentication , So from formLogin() Method to enter and have a look , The bottom is through UsernamePasswordAuthenticationFilter The of this filter attemptAuthentication Method for certification , So in attemptAuthentication Break point in method , It can be debugged . Find that you will enter ForwardAuthenticationFailureHandler processor .

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 {
//【 matters needing attention 】 Release resources should be put in front , The certified ones are in the back
http.authorizeRequests()
.mvcMatchers("/index").permitAll() // On behalf of release index All requests for
.mvcMatchers("/loginHtml").permitAll() // release loginHtml request
.anyRequest().authenticated()// Authentication is required on behalf of other requests
.and()
.formLogin()// Indicates that other requests requiring authentication have passed the form authentication
//loginPage Once you customize this login page , Then you have to make it clear SpringSecurity Which one in the future url Process your login request
.loginPage("/loginHtml")// Used to specify user-defined login interface , Don't use SpringSecurity Default login interface Be careful : Once you customize the login page , Login must be specified url
//loginProcessingUrl This doLogin The request itself is not , Because we just need to tell SpringSecurity, In the future, as long as the front-end initiates a doLogin Such a request ,
// that SpringSecurity You should be username and password To capture
.loginProcessingUrl("/doLogin")// Specified login request processing url
.usernameParameter("uname") // Specifies the name of the login interface user name text box name value , If not specified , The default property name must be username
.passwordParameter("passwd")// Specify the password box of the login interface name value , If not specified , The default property name must be password
// .successForwardUrl("/index")// Authentication success forward Jump path ,forward Represents the jump inside the server , The address bar doesn't change Always jump to the specified request after successful authentication
// .defaultSuccessUrl("/index")// Authentication success Then jump , Redirect redirect After the jump , The address will change Successfully jump according to the last save request
.successHandler(new MyAuthenticationSuccessHandler()) // Handle when authentication is successful Front and back end separation solution
.failureForwardUrl("/loginHtml")// After the authentication fails forward Jump
// .failureUrl("/login.html")// After the authentication fails redirect Jump
.and()
.csrf().disable(); // prohibit csrf Cross site request protection
}
}
matters needing attention
failureForwardUrl Set to /login.html, Can't get exception information , Because when the browser initiates doLogin When the request reaches the server , After we enter the wrong authentication message , The server will initiate /login.html request , But this /login.html Although there is an interface , But it is not open in our configuration , So we'll find out he's a 302 The return code of
mvcMatchers Acting only on the back-end interface is controller, image html,js,img Resources are written without controller Jump , It's invalid

Actually failureUrl Set up /login.html It's the same 
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/" lang="en">
<head>
<meta charset="UTF-8">
<title> The user login </title>
</head>
<body>
<h2 th:text="${#request.getAttribute('SPRING_SECURITY_LAST_EXCEPTION')}">
</h2>
<h1> The user login </h1>
<form th:action="@{/doLogin}" method="post">
user name :<input type="text" name="uname"> <br>
password :<input type="text" name="passwd"><br>
<input type="submit" value=" Sign in ">
</form>
</body>
</html>

failureUrl
Because it's form authentication , So from formLogin() Method to enter and have a look , The bottom is through UsernamePasswordAuthenticationFilter The of this filter attemptAuthentication Method for certification , So in attemptAuthentication Break point in method , It can be debugged . Find that you will enter SimpleUrlAuthenticationFailureHandler processor .

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 {
//【 matters needing attention 】 Release resources should be put in front , The certified ones are in the back
http.authorizeRequests()
.mvcMatchers("/index").permitAll() // On behalf of release index All requests for
.mvcMatchers("/loginHtml").permitAll() // release loginHtml request
.anyRequest().authenticated()// Authentication is required on behalf of other requests
.and()
.formLogin()// Indicates that other requests requiring authentication have passed the form authentication
//loginPage Once you customize this login page , Then you have to make it clear SpringSecurity Which one in the future url Process your login request
.loginPage("/loginHtml")// Used to specify user-defined login interface , Don't use SpringSecurity Default login interface Be careful : Once you customize the login page , Login must be specified url
//loginProcessingUrl This doLogin The request itself is not , Because we just need to tell SpringSecurity, In the future, as long as the front-end initiates a doLogin Such a request ,
// that SpringSecurity You should be username and password To capture
.loginProcessingUrl("/doLogin")// Specified login request processing url
.usernameParameter("uname") // Specifies the name of the login interface user name text box name value , If not specified , The default property name must be username
.passwordParameter("passwd")// Specify the password box of the login interface name value , If not specified , The default property name must be password
// .successForwardUrl("/index")// Authentication success forward Jump path ,forward Represents the jump inside the server , The address bar doesn't change Always jump to the specified request after successful authentication
// .defaultSuccessUrl("/index")// Authentication success Then jump , Redirect redirect After the jump , The address will change Successfully jump according to the last save request
.successHandler(new MyAuthenticationSuccessHandler()) // Handle when authentication is successful Front and back end separation solution
// .failureForwardUrl("/login.html")// After the authentication fails forward Jump
.failureUrl("/login.html")// After the authentication fails redirect Jump
.and()
.csrf().disable(); // prohibit csrf Cross site request protection
}
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/" lang="en">
<head>
<meta charset="UTF-8">
<title> The user login </title>
</head>
<body>
<h2 th:text="${#httpSession.getAttribute('SPRING_SECURITY_LAST_EXCEPTION')}">
</h2>
<h1> The user login </h1>
<form th:action="@{/doLogin}" method="post">
user name :<input type="text" name="uname"> <br>
password :<input type="text" name="passwd"><br>
<input type="submit" value=" Sign in ">
</form>
</body>
</html>

版权声明
本文为[Engage in money self-discipline]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230946224782.html
边栏推荐
- #yyds干货盘点#ubuntu18.0.4安装mysql并解决ERROR 1698: Access denied for user ''root''@''localhost''
- 元宇宙时代的职业规划与执行
- Go language learning notes - language interface | go language from scratch
- 构建元宇宙时代敏捷制造的九种能力
- 计算机网络安全实验二|DNS协议漏洞利用实验
- Two declaration methods of functions of JS
- Buuctf [actf2020 freshman competition] include1
- C语言:表达式求值(整型提升、算术转换 ...)
- Chinese Remainder Theorem and extended Chinese remainder theorem that can be understood by Aunt Baojie
- Example of data object mask used by SAP translate
猜你喜欢

Personal homepage software fenrus

How to use SQL statement union to get another column of another table when the content of a column in a table is empty

Secrets in buffctf file 1

Leetcode0587. Install fence

Practice of Flink streaming batch integration in Xiaomi

NLLLoss+log_ SoftMax=CE_ Loss

论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——3背景

DVWA range practice

成功的DevOps Leader 应该清楚的3个挑战

Acquisition of DOM learning elements JS
随机推荐
Flutter's loading animation is more interesting
C语言:表达式求值(整型提升、算术转换 ...)
Introduction to sap pi / PO login and basic functions
GCD of p2257 YY (Mobius inversion)
Introduction to graph theory -- drawing
Pyqt5与通信
SAP pi / PO soap2proxy consumption external WS example
Career planning and implementation in the era of meta universe
元宇宙时代的职业规划与执行
PHP笔记(一):开发环境配置
亚马逊云科技入门资源中心,从0到1轻松上云
成功的DevOps Leader 应该清楚的3个挑战
Windows安装redis并将redis设置成服务开机自启
SAP 03-amdp CDs table function using 'with' clause
[hdu6833] a very easy math problem
Go language learning notes - slice, map | go language from scratch
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——3背景
JS DOM learn three ways to create elements
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——4视觉系统中的多故障
LeetCode 1611. The minimum number of operations to make an integer 0