欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Security中successHandler和failureHandler使用方式

 更新時(shí)間:2024年08月01日 15:13:47   作者:放肆熱愛(ài)  
這篇文章主要介紹了Spring Security中successHandler和failureHandler使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

前言

successHandler和failureHandler是Spring Security中兩個(gè)較為強(qiáng)大的用來(lái)處理登錄成功和失敗的回調(diào)函數(shù),通過(guò)它們兩個(gè)我們就可以自定義一些前后端數(shù)據(jù)的交互。

successHandler

該方法有三個(gè)參數(shù)

  • req:相當(dāng)與HttpServletRequest
  • res:相當(dāng)與HttpServletRespose
  • authentication:這里保存了我們登錄后的用戶信息

進(jìn)行如下配置

.successHandler((req, resp, authentication) -> {
                    Object principal = authentication.getPrincipal();
                    resp.setContentType("application/json;charset=utf-8");
                    PrintWriter out = resp.getWriter();
                    out.write(new ObjectMapper().writeValueAsString(principal));
                    out.flush();
                    out.close();
                })

配置類(lèi)代碼

package com.scexample.sc.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import java.io.PrintWriter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Bean
    PasswordEncoder passwordEncoder(){
        return NoOpPasswordEncoder.getInstance();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("xiaoming")
                .password("123456").roles("admin");

    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/js/**","/css/**","/images/**");  //這個(gè)是用來(lái)忽略一些url地址,對(duì)其不進(jìn)行校驗(yàn),通常用在一些靜態(tài)文件中。
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/aaa.html")
                 .loginProcessingUrl("/logintest")
                .usernameParameter("name")
                .passwordParameter("passwd")
                .successHandler((req, res, authentication) -> {
                    Object principal = authentication.getPrincipal();
                    res.setContentType("application/json;charset=utf-8");
                    PrintWriter out = res.getWriter();
                    out.write(new ObjectMapper().writeValueAsString(principal));
                    out.flush();
                    out.close();
                })
                .permitAll()
                .and()
                .csrf().disable()
        );
    }
}

再次登錄后

failureHandler

該方法有三個(gè)參數(shù)

  • req:相當(dāng)與HttpServletRequest
  • res:相當(dāng)與HttpServletRespose
  • e:這里保存了我們登錄失敗的原因

異常種類(lèi):

  • LockedException 賬戶鎖定
  • CredentialsExpiredException 密碼過(guò)期
  • AccountExpiredException 賬戶過(guò)期
  • DisabledException 賬戶被禁止
  • BadCredentialsException 用戶名或者密碼錯(cuò)誤
.failureHandler((req, res, e) -> {
                    res.setContentType("application/json;charset=utf-8");
                    PrintWriter out = res.getWriter();
                    out.write(e.getMessage());
                    out.flush();
                    out.close();
                })

配置類(lèi)代碼:

package com.scexample.sc.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import java.io.PrintWriter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Bean
    PasswordEncoder passwordEncoder(){
        return NoOpPasswordEncoder.getInstance();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("xiaoming")
                .password("123456").roles("admin");

    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/js/**","/css/**","/images/**");  //這個(gè)是用來(lái)忽略一些url地址,對(duì)其不進(jìn)行校驗(yàn),通常用在一些靜態(tài)文件中。
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/aaa.html")
                 .loginProcessingUrl("/logintest")
                .usernameParameter("name")
                .passwordParameter("passwd")
                .successHandler((req, res, authentication) -> {
                    Object principal = authentication.getPrincipal();
                    res.setContentType("application/json;charset=utf-8");
                    PrintWriter out = res.getWriter();
                    out.write(new ObjectMapper().writeValueAsString(principal));
                    out.flush();
                    out.close();
                })
                .failureHandler((req, res, e) -> {
                    res.setContentType("application/json;charset=utf-8");
                    PrintWriter out = res.getWriter();
                    out.write(e.getMessage());
                    out.flush();
                    out.close();
                })           
                .permitAll()
                .and()
                .csrf().disable()          
    }
}

未認(rèn)證處理方法

spring security默認(rèn)情況下,如果認(rèn)證不成功,直接重定向到登錄頁(yè)面。

但是項(xiàng)目中,我們有的時(shí)候不需要這樣,我們需要在前端進(jìn)行判斷 ,然后再?zèng)Q定進(jìn)行其他的處理,那我們就可以用authenticationEntryPoint這個(gè)接口進(jìn)行自定義了,取消它的默認(rèn)重定向行為。

該方法有三個(gè)參數(shù)

  • req:相當(dāng)與HttpServletRequest
  • res:相當(dāng)與HttpServletRespose
  • authException:指的就是我們未認(rèn)證的exception
 				.csrf().disable()
                .exceptionHandling()
                .authenticationEntryPoint((req, res, authException) -> {
                    res.setContentType("application/json;charset=utf-8");
                    PrintWriter out = res.getWriter();
                    out.write("檢測(cè)到未登錄狀態(tài),請(qǐng)先登錄");
                    out.flush();
                    out.close();
                }

配置類(lèi)代碼

package com.scexample.sc.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import java.io.PrintWriter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Bean
    PasswordEncoder passwordEncoder(){
        return NoOpPasswordEncoder.getInstance();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("xiaoming")
                .password("123456").roles("admin");

    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/js/**","/css/**","/images/**");  //這個(gè)是用來(lái)忽略一些url地址,對(duì)其不進(jìn)行校驗(yàn),通常用在一些靜態(tài)文件中。
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/aaa.html")
                 .loginProcessingUrl("/logintest")
                .usernameParameter("name")
                .passwordParameter("passwd")
                .successHandler((req, res, authentication) -> {
                    Object principal = authentication.getPrincipal();
                    res.setContentType("application/json;charset=utf-8");
                    PrintWriter out = res.getWriter();
                    out.write(new ObjectMapper().writeValueAsString(principal));
                    out.flush();
                    out.close();
                })
                .failureHandler((req, res, e) -> {
                    res.setContentType("application/json;charset=utf-8");
                    PrintWriter out = res.getWriter();
                    out.write(e.getMessage());
                    out.flush();
                    out.close();
                })           
                .permitAll()
                .and()
                .csrf().disable()
                .exceptionHandling()
                .authenticationEntryPoint((req, res, authException) -> {
                    res.setContentType("application/json;charset=utf-8");
                    PrintWriter out = res.getWriter();
                    out.write("檢測(cè)到未登錄狀態(tài),請(qǐng)先登錄");
                    out.flush();
                    out.close();
                }
               );          
    }
}

注銷(xiāo)登錄

				   .logoutSuccessHandler((req, res, authentication) -> {
                    res.setContentType("application/json;charset=utf-8");
                    PrintWriter out = res.getWriter();
                    out.write("注銷(xiāo)成功");
                    out.flush();
                    out.close();
                })

配置類(lèi)代碼:

package com.scexample.sc.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import java.io.PrintWriter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Bean
    PasswordEncoder passwordEncoder(){
        return NoOpPasswordEncoder.getInstance();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("xiaoming")
                .password("123456").roles("admin");

    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/js/**","/css/**","/images/**");  //這個(gè)是用來(lái)忽略一些url地址,對(duì)其不進(jìn)行校驗(yàn),通常用在一些靜態(tài)文件中。
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/aaa.html")
                 .loginProcessingUrl("/logintest")
                .usernameParameter("name")
                .passwordParameter("passwd")
                .successHandler((req, res, authentication) -> {
                    Object principal = authentication.getPrincipal();
                    res.setContentType("application/json;charset=utf-8");
                    PrintWriter out = res.getWriter();
                    out.write(new ObjectMapper().writeValueAsString(principal));
                    out.flush();
                    out.close();
                })
                .failureHandler((req, res, e) -> {
                    res.setContentType("application/json;charset=utf-8");
                    PrintWriter out = res.getWriter();
                    out.write(e.getMessage());
                    out.flush();
                    out.close();
                })           
                .permitAll()
                .and()
                .logout()
                .logoutUrl("/logout")
                .logoutSuccessHandler((req, res, authentication) -> {
                    res.setContentType("application/json;charset=utf-8");
                    PrintWriter out = res.getWriter();
                    out.write("注銷(xiāo)成功");
                    out.flush();
                    out.close();
                })
                .permitAll()
                .and()
                .csrf().disable()
                .exceptionHandling()
                .authenticationEntryPoint((req, res, authException) -> {
                    res.setContentType("application/json;charset=utf-8");
                    PrintWriter out = res.getWriter();
                    out.write("檢測(cè)到未登錄狀態(tài),請(qǐng)先登錄");
                    out.flush();
                    out.close();
                }
               );          
    }
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解SpringBoot啟動(dòng)代碼和自動(dòng)裝配源碼分析

    詳解SpringBoot啟動(dòng)代碼和自動(dòng)裝配源碼分析

    這篇文章主要介紹了SpringBoot啟動(dòng)代碼和自動(dòng)裝配源碼分析,使用SpringBoot很簡(jiǎn)單,在主類(lèi)中添加一個(gè)@SpringBootApplication,以及調(diào)用SpringApplication.run()并傳入主類(lèi),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • Springboot調(diào)整接口響應(yīng)返回時(shí)長(zhǎng)詳解(解決響應(yīng)超時(shí)問(wèn)題)

    Springboot調(diào)整接口響應(yīng)返回時(shí)長(zhǎng)詳解(解決響應(yīng)超時(shí)問(wèn)題)

    當(dāng)后端對(duì)于數(shù)據(jù)量較大的處理或是某些耗時(shí)的操作時(shí),需要先對(duì)請(qǐng)求接口的請(qǐng)求進(jìn)行響應(yīng),下面這篇文章主要給大家介紹了關(guān)于Springboot調(diào)整接口響應(yīng)返回時(shí)長(zhǎng)(解決響應(yīng)超時(shí)問(wèn)題)的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • 基于Java的電梯系統(tǒng)實(shí)現(xiàn)過(guò)程

    基于Java的電梯系統(tǒng)實(shí)現(xiàn)過(guò)程

    這篇文章主要介紹了基于Java的電梯系統(tǒng)實(shí)現(xiàn)過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • SpringBoot集成SwaggerUi以及啟動(dòng)時(shí)遇到的錯(cuò)誤

    SpringBoot集成SwaggerUi以及啟動(dòng)時(shí)遇到的錯(cuò)誤

    這篇文章主要介紹了SpringBoot集成SwaggerUi以及啟動(dòng)時(shí)遇到的錯(cuò)誤,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • SpringBoot使用Flyway進(jìn)行數(shù)據(jù)庫(kù)遷移的實(shí)現(xiàn)示例

    SpringBoot使用Flyway進(jìn)行數(shù)據(jù)庫(kù)遷移的實(shí)現(xiàn)示例

    Flyway是一個(gè)數(shù)據(jù)庫(kù)遷移工具,它提供遷移歷史和回滾的功能,本文主要介紹了如何使用Flyway來(lái)管理Spring Boot應(yīng)用程序中的SQL數(shù)據(jù)庫(kù)架構(gòu),感興趣的可以了解一下
    2023-08-08
  • Struts中action線程安全問(wèn)題解析

    Struts中action線程安全問(wèn)題解析

    這篇文章主要介紹了Struts中action線程安全問(wèn)題解析,涉及實(shí)例代碼,還是挺不錯(cuò)的,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-10-10
  • springboot清除字符串前后空格與防xss攻擊方法

    springboot清除字符串前后空格與防xss攻擊方法

    這篇文章主要介紹了springboot清除字符串前后空格與防xss攻擊方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 基于XML配置Spring的自動(dòng)裝配過(guò)程解析

    基于XML配置Spring的自動(dòng)裝配過(guò)程解析

    這篇文章主要介紹了基于XML配置Spring的自動(dòng)裝配過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • 最新評(píng)論