亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于spring security實(shí)現(xiàn)登錄注銷功能過(guò)程解析

 更新時(shí)間:2020年01月15日 09:25:48   作者:炫舞風(fēng)中  
這篇文章主要介紹了基于spring security實(shí)現(xiàn)登錄注銷功能過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了基于spring security實(shí)現(xiàn)登錄注銷功能過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

1、引入maven依賴

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

2、Security 配置類 說(shuō)明登錄方式、登錄頁(yè)面、哪個(gè)url需要認(rèn)證、注入登錄失敗/成功過(guò)濾器

@Configuration
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {

  /**
   * 注入 Security 屬性類配置
   */
  @Autowired
  private SecurityProperties securityProperties;

  /**
   * 注入 自定義的 登錄成功處理類
   */
  @Autowired
  private MyAuthenticationSuccessHandler mySuccessHandler;
  /**
   * 注入 自定義的 登錄失敗處理類
   */
  @Autowired
  private MyAuthenticationFailHandler myFailHandler;

  /**
   * 重寫PasswordEncoder 接口中的方法,實(shí)例化加密策略
   * @return 返回 BCrypt 加密策略
   */
  @Bean
  public PasswordEncoder passwordEncoder(){
    return new BCryptPasswordEncoder();
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {

    //登錄成功的頁(yè)面地址
    String redirectUrl = securityProperties.getLoginPage();
    //basic 登錄方式
//   http.httpBasic()

    //表單登錄 方式
    http.formLogin()
        .loginPage("/authentication/require")
        //登錄需要經(jīng)過(guò)的url請(qǐng)求
        .loginProcessingUrl("/authentication/form")
        .successHandler(mySuccessHandler)
        .failureHandler(myFailHandler)
        .and()
        //請(qǐng)求授權(quán)
        .authorizeRequests()
        //不需要權(quán)限認(rèn)證的url
        .antMatchers("/authentication/*",redirectUrl).permitAll()
        //任何請(qǐng)求
        .anyRequest()
        //需要身份認(rèn)證
        .authenticated()
        .and()
        //關(guān)閉跨站請(qǐng)求防護(hù)
        .csrf().disable();
    //默認(rèn)注銷地址:/logout
    http.logout().
        //注銷之后 跳轉(zhuǎn)的頁(yè)面
        logoutSuccessUrl("/authentication/require");
  }

3、自定義登錄成功和失敗的處理器

(1)、登錄成功

@Component
@Slf4j
public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
  @Override
  public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {

     logger.info("登錄成功");
     //將 authention 信息打包成json格式返回
      httpServletResponse.setContentType("application/json;charset=UTF-8");
      httpServletResponse.getWriter().write("登錄成功");
 } }

(2)、登錄失敗

@Component
@Slf4j
public class MyAuthenticationFailHandler extends SimpleUrlAuthenticationFailureHandler {
  @Override
  public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
    logger.info("登錄失敗");

      //設(shè)置狀態(tài)碼
      httpServletResponse.setStatus(500);
      //將 登錄失敗 信息打包成json格式返回
      httpServletResponse.setContentType("application/json;charset=UTF-8");
      httpServletResponse.getWriter().write("登錄失敗:"+e.getMessage());
 } }

4、UserDetail 類 加載用戶數(shù)據(jù) , 返回UserDetail 實(shí)例 (里面包含用戶信息)

@Component
@Slf4j
public class MyUserDetailsService implements UserDetailsService {

  @Autowired
  private PasswordEncoder passwordEncoder;

  /**
   * 根據(jù)進(jìn)行登錄
   * @param username
   * @return
   * @throws UsernameNotFoundException
   */
  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    log.info("登錄用戶名:"+username);
    String password = passwordEncoder.encode("123456");
    //User三個(gè)參數(shù)  (用戶名+密碼+權(quán)限)
    //根據(jù)查找到的用戶信息判斷用戶是否被凍結(jié)
    log.info("數(shù)據(jù)庫(kù)密碼:"+password);
    return new User(username,password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
  }
}

5、登錄路徑請(qǐng)求類,.loginPage("/authentication/require")

@RestController
@Slf4j
@ResponseStatus(code = HttpStatus.UNAUTHORIZED)
public class BrowerSecurityController {

  /**
   * 把當(dāng)前的請(qǐng)求緩存到 session 里去
   */
  private RequestCache requestCache = new HttpSessionRequestCache();

  /**
   * 重定向 策略
   */
  private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();

  /**
   * 注入 Security 屬性類配置
   */
  @Autowired
  private SecurityProperties securityProperties;

  /**
   * 當(dāng)需要身份認(rèn)證時(shí) 跳轉(zhuǎn)到這里
   */
  @RequestMapping("/authentication/require")
  public SimpleResponse requireAuthentication(HttpServletRequest request, HttpServletResponse response) throws IOException {
    //拿到請(qǐng)求對(duì)象
    SavedRequest savedRequest = requestCache.getRequest(request, response);
    if (savedRequest != null){
      //獲取 跳轉(zhuǎn)url
      String targetUrl = savedRequest.getRedirectUrl();
      log.info("引發(fā)跳轉(zhuǎn)的請(qǐng)求是:"+targetUrl);

      //判斷 targetUrl 是不是 .html 結(jié)尾, 如果是:跳轉(zhuǎn)到登錄頁(yè)(返回view)
      if (StringUtils.endsWithIgnoreCase(targetUrl,".html")){
        String redirectUrl = securityProperties.getLoginPage();
        redirectStrategy.sendRedirect(request,response,redirectUrl);
      }
    }
    //如果不是,返回一個(gè)json 字符串
    return new SimpleResponse("訪問(wèn)的服務(wù)需要身份認(rèn)證,請(qǐng)引導(dǎo)用戶到登錄頁(yè)");
  }

6、postman請(qǐng)求測(cè)試

(1)未登錄請(qǐng)求

(2)、登錄

(3)、再次訪問(wèn)

(4)、注銷

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java Annotation Overview詳解

    Java Annotation Overview詳解

    這篇文章主要介紹了Java Annotation Overview,需要的朋友可以參考下
    2014-02-02
  • Spring?Boot項(xiàng)目如何優(yōu)雅實(shí)現(xiàn)Excel導(dǎo)入與導(dǎo)出功能

    Spring?Boot項(xiàng)目如何優(yōu)雅實(shí)現(xiàn)Excel導(dǎo)入與導(dǎo)出功能

    在我們平時(shí)工作中經(jīng)常會(huì)遇到要操作Excel的功能,比如導(dǎo)出個(gè)用戶信息或者訂單信息的Excel報(bào)表,下面這篇文章主要給大家介紹了關(guān)于Spring?Boot項(xiàng)目中如何優(yōu)雅實(shí)現(xiàn)Excel導(dǎo)入與導(dǎo)出功能的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • 淺聊一下Spring?Security的使用方法

    淺聊一下Spring?Security的使用方法

    Spring?Security?是一個(gè)基于?Spring?框架的安全框架,提供了一套安全性認(rèn)證和授權(quán)的解決方案,用于保護(hù)?Web?應(yīng)用程序和服務(wù),接下來(lái)小編就和大家聊聊Spring?Security,感興趣的小伙伴跟著小編一起來(lái)看看吧
    2023-08-08
  • Java 畫時(shí)鐘遇到的問(wèn)題及解決方案

    Java 畫時(shí)鐘遇到的問(wèn)題及解決方案

    我是一個(gè)剛?cè)腴T的小菜鳥(niǎo),希望我寫的東西可以幫助和我一樣剛?cè)腴T的兄弟們少走一些彎路,也希望大佬們可以多指點(diǎn)指點(diǎn)我。感謝!解決在畫時(shí)鐘遇到的問(wèn)題讓我花費(fèi)不少時(shí)間...說(shuō)兩個(gè)困擾我比較久的
    2021-11-11
  • Java線程的聯(lián)合用法實(shí)例分析

    Java線程的聯(lián)合用法實(shí)例分析

    這篇文章主要介紹了Java線程的聯(lián)合用法,結(jié)合實(shí)例形式分析了java線程聯(lián)合的原理、實(shí)現(xiàn)方法及相關(guān)操作技巧,需要的朋友可以參考下
    2019-10-10
  • Java Map 通過(guò) key 或者 value 過(guò)濾的實(shí)例代碼

    Java Map 通過(guò) key 或者 value 過(guò)濾的實(shí)例代碼

    這篇文章主要介紹了Java Map 通過(guò) key 或者 value 過(guò)濾的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-06-06
  • java 后端生成pdf模板合并單元格表格的案例

    java 后端生成pdf模板合并單元格表格的案例

    這篇文章主要介紹了java 后端生成pdf模板合并單元格表格的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • Java?GUI實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)

    Java?GUI實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Java?GUI實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Spring?Cloud?Gateway?遠(yuǎn)程代碼執(zhí)行漏洞(CVE-2022-22947)的過(guò)程解析

    Spring?Cloud?Gateway?遠(yuǎn)程代碼執(zhí)行漏洞(CVE-2022-22947)的過(guò)程解析

    Spring?Cloud?Gateway?是基于?Spring?Framework?和?Spring?Boot?構(gòu)建的?API?網(wǎng)關(guān),它旨在為微服務(wù)架構(gòu)提供一種簡(jiǎn)單、有效、統(tǒng)一的?API?路由管理方式,這篇文章主要介紹了Spring?Cloud?Gateway?遠(yuǎn)程代碼執(zhí)行漏洞(CVE-2022-22947),需要的朋友可以參考下
    2022-08-08
  • 淺析Java getResource詳細(xì)介紹

    淺析Java getResource詳細(xì)介紹

    這篇文章主要介紹了Java getResource 講解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09

最新評(píng)論