当前位置:网站首页>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 dry goods inventory ubuntu18 0.4 install MySQL and solve error 1698: access denied for user ''root' '@' 'localhost' '
- [Niuke practice match 68] fans of Niuniu (matrix fast power cycle matrix optimization)
- Go language learning notes - exception handling | go language from scratch
- Nvidia最新三维重建技术Instant-ngp初探
- Pyqt5与通信
- Pre parsing of JS
- 个人主页软件Fenrus
- Introduction to sap pi / PO login and basic functions
- Creation of raid0 and RAID5 and Simulation of how RAID5 works
猜你喜欢

Secrets in buffctf file 1

kernel-pwn学习(3)--ret2user&&kernel ROP&&QWB2018-core

Two methods of building Yum source warehouse locally

PHP notes (I): development environment configuration

Chinese Remainder Theorem and extended Chinese remainder theorem that can be understood by Aunt Baojie

自定义登录失败处理

SAP 03-amdp CDs table function using 'with' clause

Go language learning notes - structure | go language from scratch

Canary publishing using ingress

Comparison of overloading, rewriting and hiding
随机推荐
[COCI] lattice (dichotomy + tree divide and conquer + string hash)
ABAP CDs view with association example
雨生百谷,万物生长
Nine abilities of agile manufacturing in the era of meta universe
Integral function and Dirichlet convolution
[CF 1425d] danger of mad snakes
Go language learning notes - structure | go language from scratch
Less than 100 secrets about prime numbers
亚马逊云科技入门资源中心,从0到1轻松上云
Simple understanding of arguments in JS
(Extended) bsgs and higher order congruence equation
Codeforces Round #784 (Div. 4)
SAP pi / PO soap2proxy consumption external WS example
PHP笔记(一):开发环境配置
SAP RFC_ CVI_ EI_ INBOUND_ Main BP master data creation example (Demo customer only)
P1390 sum of common divisor (Mobius inversion)
[COCI] Vje š TICA (subset DP)
论文阅读《Integrity Monitoring Techniques for Vision Navigation Systems》——5结果
[Niuke practice match 68] fans of Niuniu (matrix fast power cycle matrix optimization)
MySQL of database -- overview and installation