最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Security實(shí)現(xiàn)自動(dòng)登陸功能示例

 更新時(shí)間:2021年11月04日 14:03:21   作者:Java Gosling  
自動(dòng)登錄在很多網(wǎng)站和APP上都能用的到,解決了用戶(hù)每次輸入賬號(hào)密碼的麻煩。本文就使用Spring Security實(shí)現(xiàn)自動(dòng)登陸功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

當(dāng)我們?cè)诘卿浵馫Q郵箱這種大多數(shù)的網(wǎng)站,往往在登錄按鍵上會(huì)有下次自動(dòng)登錄這個(gè)選項(xiàng),勾選后登錄成功,在一段時(shí)間內(nèi),即便退出瀏覽器或者服務(wù)器重啟,再次訪(fǎng)問(wèn)不需要用戶(hù)輸入賬號(hào)密碼進(jìn)行登錄,這也解決了用戶(hù)每次輸入賬號(hào)密碼的麻煩。

在這里插入圖片描述

接下來(lái)實(shí)現(xiàn)自動(dòng)登陸。

applicatio.properties配置用戶(hù)名密碼

spring.security.user.name=java
spring.security.user.password=java

controller層實(shí)現(xiàn)

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }
}

配置類(lèi)實(shí)現(xiàn)

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
    http.formLogin()
            .and()
            .authorizeRequests()
            .anyRequest()
            .authenticated()
            .and()
            .rememberMe()
            .and()
            .csrf().disable();
}

訪(fǎng)問(wèn)http://localhost:8080/hello,此時(shí)系統(tǒng)會(huì)重定向到登錄頁(yè)面。

在這里插入圖片描述

二話(huà)不說(shuō),輸入賬號(hào)密碼,開(kāi)搞!

此時(shí)看到了登錄數(shù)據(jù)remember-me的值為on,當(dāng)自定義登陸框的時(shí)候應(yīng)該知道如何定義key了吧。

在這里插入圖片描述

在hello接口,可以很清楚的看到cookie里保存了一個(gè)remember-me的令牌,這個(gè)就是自動(dòng)登錄的關(guān)鍵所在。

在這里插入圖片描述

至于令牌是怎么生成的,先看一段源碼。核心處理類(lèi)TokenBasedRememberMeServices->onLoginSuccess

public void onLoginSuccess(HttpServletRequest request, HttpServletResponse response, Authentication successfulAuthentication) {
    //拿到用戶(hù)名和密碼
    String username = this.retrieveUserName(successfulAuthentication);
    String password = this.retrievePassword(successfulAuthentication);
    //用戶(hù)名為空 打印日志
    if (!StringUtils.hasLength(username)) {
        this.logger.debug("Unable to retrieve username");
    } else {
        //密碼為空 通過(guò)用戶(hù)名再去查詢(xún)
        if (!StringUtils.hasLength(password)) {
            UserDetails user = this.getUserDetailsService().loadUserByUsername(username);
            password = user.getPassword();
            //查到的密碼還為空 打印日志 結(jié)束
            if (!StringUtils.hasLength(password)) {
                this.logger.debug("Unable to obtain password for user: " + username);
                return;
            }
        }
        //令牌有效期的生成 1209600是兩周 也就是說(shuō)令牌有效期14天
        int tokenLifetime = this.calculateLoginLifetime(request, successfulAuthentication);
        long expiryTime = System.currentTimeMillis();
        expiryTime += 1000L * (long)(tokenLifetime < 0 ? 1209600 : tokenLifetime);
        //生成簽名 signature
        String signatureValue = this.makeTokenSignature(expiryTime, username, password);
        //設(shè)置cookie
        this.setCookie(new String[]{username, Long.toString(expiryTime), signatureValue}, tokenLifetime, request, response);
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Added remember-me cookie for user '" + username + "', expiry: '" + new Date(expiryTime) + "'");
        }
    }
} 

//使用MD5加密 通過(guò)用戶(hù)名、令牌有效期、密碼和key生成rememberMe的令牌 這里的key也就是加密的鹽值
protected String makeTokenSignature(long tokenExpiryTime, String username, String password) {
    String data = username + ":" + tokenExpiryTime + ":" + password + ":" + this.getKey();

    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        return new String(Hex.encode(digest.digest(data.getBytes())));
    } catch (NoSuchAlgorithmException var7) {
        throw new IllegalStateException("No MD5 algorithm available!");
    }
}

看完了核心的源碼,也就知道了令牌的生成規(guī)則:username + “:” + tokenExpiryTime + “:” + password + “:” + key(key 是一個(gè)散列鹽值,可以用來(lái)防治令牌被修改,通過(guò)MD5散列函數(shù)生成。),然后通過(guò)Base64編碼。

取出剛才的remember-me=amF2YToxNjM3MTI2MDk1OTMxOmQ5OGI3OTY5OTE4ZmQwMzE3ZWUyY2U4Y2MzMjQxZGQ0進(jìn)行下驗(yàn)證。

在這里插入圖片描述

解碼后是java:1637126095931:d98b7969918fd0317ee2ce8cc3241dd4,很明顯javausername,1637126095931是兩周后的tokenExpiryTime,d98b7969918fd0317ee2ce8cc3241dd4passwordkey值的MD5加密生成的。

需要注意的是key值是通過(guò)UUID隨機(jī)生成的,當(dāng)重啟服務(wù)器時(shí),UUID的變化會(huì)導(dǎo)致自動(dòng)登錄失敗,所以為了避免之前生成的令牌失效,可以在配置中定義key值。

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.formLogin()
            .and()
            .authorizeRequests()
            .anyRequest()
            .authenticated()
            .and()
            .rememberMe()
            .key("HelloWorld")
            .and()
            .csrf().disable();
}

Spring Security—登陸流程分析曾經(jīng)說(shuō)到 Spring Security中的認(rèn)證授權(quán)都是通過(guò)過(guò)濾器來(lái)實(shí)現(xiàn)的。RememberMeAuthenticationFilter 是自動(dòng)登錄的核心過(guò)濾器。

public class RememberMeAuthenticationFilter extends GenericFilterBean implements ApplicationEventPublisherAware {
    private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
      throws IOException, ServletException {
        //獲取當(dāng)前用戶(hù)實(shí)例 繼續(xù)過(guò)濾校驗(yàn)
   if (SecurityContextHolder.getContext().getAuthentication() != null) {
      this.logger.debug(LogMessage
            .of(() -> "SecurityContextHolder not populated with remember-me token, as it already contained: '"
                  + SecurityContextHolder.getContext().getAuthentication() + "'"));
      chain.doFilter(request, response);
      return;
   }
   //登錄獲取Auth
   Authentication rememberMeAuth = this.rememberMeServices.autoLogin(request, response);
   if (rememberMeAuth != null) {
      // Attempt authenticaton via AuthenticationManager
      try {
      //進(jìn)行remember-me校驗(yàn)
         rememberMeAuth = this.authenticationManager.authenticate(rememberMeAuth);
         // Store to SecurityContextHolder
         //保存用戶(hù)實(shí)例
         SecurityContextHolder.getContext().setAuthentication(rememberMeAuth);
         //成功頁(yè)面跳轉(zhuǎn)
         onSuccessfulAuthentication(request, response, rememberMeAuth);
         this.logger.debug(LogMessage.of(() -> "SecurityContextHolder populated with remember-me token: '"
               + SecurityContextHolder.getContext().getAuthentication() + "'"));
         if (this.eventPublisher != null) {
            this.eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(
                  SecurityContextHolder.getContext().getAuthentication(), this.getClass()));
         }
         if (this.successHandler != null) {
            this.successHandler.onAuthenticationSuccess(request, response, rememberMeAuth);
            return;
         }
      }
      catch (AuthenticationException ex) {
         this.logger.debug(LogMessage
               .format("SecurityContextHolder not populated with remember-me token, as AuthenticationManager "
                     + "rejected Authentication returned by RememberMeServices: '%s'; "
                     + "invalidating remember-me token", rememberMeAuth),
               ex);
         this.rememberMeServices.loginFail(request, response);
         //失敗頁(yè)面跳轉(zhuǎn)
         onUnsuccessfulAuthentication(request, response, ex);
      }
   }
   chain.doFilter(request, response);
}
}
@Override
public final Authentication autoLogin(HttpServletRequest request, HttpServletResponse response) {
   //獲取cookie
   String rememberMeCookie = extractRememberMeCookie(request);
   if (rememberMeCookie == null) {
      return null;
   }
   this.logger.debug("Remember-me cookie detected");
   if (rememberMeCookie.length() == 0) {
      this.logger.debug("Cookie was empty");
      cancelCookie(request, response);
      return null;
   }
   try {
       //解碼cookie 拿到令牌
      String[] cookieTokens = decodeCookie(rememberMeCookie);
      //通過(guò)令牌獲取UserdDetails
      UserDetails user = processAutoLoginCookie(cookieTokens, request, response);
      this.userDetailsChecker.check(user);
      this.logger.debug("Remember-me cookie accepted");
      return createSuccessfulAuthentication(request, user);
   }
   catch (CookieTheftException ex) {
      cancelCookie(request, response);
      throw ex;
   }
   catch (UsernameNotFoundException ex) {
      this.logger.debug("Remember-me login was valid but corresponding user not found.", ex);
   }
   catch (InvalidCookieException ex) {
      this.logger.debug("Invalid remember-me cookie: " + ex.getMessage());
   }
   catch (AccountStatusException ex) {
      this.logger.debug("Invalid UserDetails: " + ex.getMessage());
   }
   catch (RememberMeAuthenticationException ex) {
      this.logger.debug(ex.getMessage());
   }
   cancelCookie(request, response);
   return null;
}

大致整體流程就是如果拿不到實(shí)例,則進(jìn)行remember-me驗(yàn)證,通過(guò)autoLogin方法里獲取cookie,解析令牌,拿到Auth,最后進(jìn)行校驗(yàn)。之后剩下的和登陸流程分析的差不多。

到此這篇關(guān)于Spring Security實(shí)現(xiàn)自動(dòng)登陸功能示例的文章就介紹到這了,更多相關(guān)Spring Security 自動(dòng)登陸內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java優(yōu)先隊(duì)列(PriorityQueue)重寫(xiě)compare操作

    Java優(yōu)先隊(duì)列(PriorityQueue)重寫(xiě)compare操作

    這篇文章主要介紹了Java優(yōu)先隊(duì)列(PriorityQueue)重寫(xiě)compare操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • 詳解JAVA的控制語(yǔ)句

    詳解JAVA的控制語(yǔ)句

    這篇文章主要介紹了Java中的控制語(yǔ)句,循環(huán)等語(yǔ)句是Java編程中流程控制的基礎(chǔ),需要的朋友可以參考下,希望能夠給你帶來(lái)幫助
    2021-11-11
  • 詳解JDBC的概念及獲取數(shù)據(jù)庫(kù)連接的5種方式

    詳解JDBC的概念及獲取數(shù)據(jù)庫(kù)連接的5種方式

    Java?DataBase?Connectivity是將Java與SQL結(jié)合且獨(dú)立于特定的數(shù)據(jù)庫(kù)系統(tǒng)的應(yīng)用程序編程接口,一種可用于執(zhí)行SQL語(yǔ)句的JavaAPI。本文主要介紹了JDBC的概念及獲取數(shù)據(jù)庫(kù)連接的5種方式,需要的可以參考一下
    2022-09-09
  • 最全MyBatis核心配置文件總結(jié)(收藏)

    最全MyBatis核心配置文件總結(jié)(收藏)

    這篇文章主要介紹了最全MyBatis核心配置文件總結(jié)(收藏),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • java system類(lèi)使用方法示例 獲取系統(tǒng)信息

    java system類(lèi)使用方法示例 獲取系統(tǒng)信息

    這篇文章主要介紹了java system類(lèi)使用方法,該類(lèi)中的方法都是靜態(tài)的。不能被實(shí)例化,沒(méi)有對(duì)外提供構(gòu)造函數(shù),該類(lèi)可以獲取系統(tǒng)信息
    2014-01-01
  • java實(shí)現(xiàn)cassandra高級(jí)操作之分頁(yè)實(shí)例(有項(xiàng)目具體需求)

    java實(shí)現(xiàn)cassandra高級(jí)操作之分頁(yè)實(shí)例(有項(xiàng)目具體需求)

    這篇文章主要介紹了java實(shí)現(xiàn)cassandra高級(jí)操作之分頁(yè)實(shí)例(有項(xiàng)目具體需求),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-04-04
  • 關(guān)于Maven依賴(lài)沖突解決之exclusions

    關(guān)于Maven依賴(lài)沖突解決之exclusions

    這篇文章主要介紹了關(guān)于Maven依賴(lài)沖突解決之exclusions用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Spring數(shù)據(jù)訪(fǎng)問(wèn)模板化方法

    Spring數(shù)據(jù)訪(fǎng)問(wèn)模板化方法

    今天小編就為大家分享一篇關(guān)于Spring數(shù)據(jù)訪(fǎng)問(wèn)模板化,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • Java之ThreadLocal使用常見(jiàn)和方式案例講解

    Java之ThreadLocal使用常見(jiàn)和方式案例講解

    這篇文章主要介紹了Java之ThreadLocal使用常見(jiàn)和方式案例講解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Java進(jìn)階教程之IO基礎(chǔ)

    Java進(jìn)階教程之IO基礎(chǔ)

    這篇文章主要介紹了Java進(jìn)階教程之IO基礎(chǔ),這里只是對(duì)Java IO的基本介紹,包含讀取和寫(xiě)入等操作,需要的朋友可以參考下
    2014-09-09

最新評(píng)論

苗栗市| 都江堰市| 社旗县| 嘉禾县| 玛纳斯县| 京山县| 探索| 乐亭县| 甘孜县| 进贤县| 林甸县| 石屏县| 临沧市| 乌海市| 迭部县| 延寿县| 城口县| 临漳县| 巴彦县| 交城县| 丹棱县| 阿克苏市| 双流县| 雷州市| 时尚| 文水县| 民和| 吕梁市| 柯坪县| 阿瓦提县| 潞西市| 碌曲县| 新蔡县| 江山市| 土默特右旗| 吉木乃县| 昂仁县| 西丰县| 响水县| 广安市| 阿克苏市|