主頁 > 後端開發 > SpringSecurity

SpringSecurity

2021-12-09 06:11:58 後端開發

11. SpringSecurity

11.1 SpringSecurity簡介

Spring 是一個非常流行和成功的 Java 應用開發框架,Spring Security 基于 Spring 框架,提供了一套 Web 應用安全性的完整解決方案,一般來說,Web 應用的安全性包括用戶認證(Authentication)和用戶授權(Authorization)兩個部分,用戶認證指的是驗證某個用戶是否為系統中的合法主體,也就是說用戶能否訪問該系統,用戶認證一般要求用戶提供用戶名和密碼,系統通過校驗用戶名和密碼來完成認證程序,用戶授權指的是驗證某個用戶是否有權限執行某個操作,在一個系統中,不同用戶所具有的權限是不同的,比如對一個檔案來說,有的用戶只能進行讀取,而有的用戶可以進行修改,一般來說,系統會為不同的用戶分配不同的角色,而每個角色則對應一系列的權限,

11.2 實驗環境搭建

1、新建springboot專案

引入web模塊、thymeleaf模塊

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--thymeleaf -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>2.5.6</version>
</dependency>

2、匯入靜態資源

index.html
|views
  |level1
     1.html
     2.html
     3.html
  |level2
     1.html
     2.html
     3.html
  |level3
     1.html
     2.html
     3.html
  Login.html

3、controller跳轉

package com.dzj.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class RouterController {

    @RequestMapping({"/","/index"})
    public String toIndex(){
        return "index";
    }
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "views/login";
    }
    @RequestMapping("/level1/{id}")
    public String toLevel1(@PathVariable("id")int id){
        return "views/level1/"+id;
    }
    @RequestMapping("/level2/{id}")
    public String toLevel2(@PathVariable("id")int id){
        return "views/level2/"+id;
    }
    @RequestMapping("/level3/{id}")
    public String toLevel3(@PathVariable("id")int id){
        return "views/level3/"+id;
    }
}

11.3 認識SpringSecurity

Spring Security 是針對Spring專案的安全框架,也是Spring Boot底層安全模塊默認的技術選型,他可以實作強大的Web安全控制,對于安全控制,我們僅需要引入 spring-boot-starter-security 模塊,進行少量的配置,即可實作強大的安全管理!

記住幾個類:

  • WebSecurityConfigurerAdapter:自定義Security策略
  • AuthenticationManagerBuilder:自定義認證策略
  • @EnableWebSecurity:開啟WebSecurity模式

Spring Security的兩個主要目標是 “認證” 和 “授權”(訪問控制),

“認證”(Authentication)

身份驗證是關于驗證您的憑據,如用戶名/用戶ID和密碼,以驗證您的身份,

身份驗證通常通過用戶名和密碼完成,有時與身份驗證因素結合使用,

“授權” (Authorization)

授權發生在系統成功驗證您的身份后,最侄訓授予您訪問資源(如資訊,檔案,資料庫,資金,位置,幾乎任何內容)的完全權限,

這個概念是通用的,而不是只在Spring Security 中存在,

1、認證和授權

目前,我們的測驗環境,是誰都可以訪問的,我們使用 Spring Security 增加上認證和授權的功能

引入 Spring Security 模塊

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

撰寫SpringSecurity配置類

參考官網:https://spring.io/projects/spring-security

查看我們自己專案中的版本,找到對應的幫助檔案:

https://docs.spring.io/spring-security/site/docs/5.5.3.RELEASE/reference/html5

進行全文搜索:WebSecurityConfigurerAdapter

撰寫基礎配置類

package com.dzj.config;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity // 開啟WebSecurity模式
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    //定義授權規則
   @Override
   protected void configure(HttpSecurity http) throws Exception {
       
  }
    //定義認證規則
   @Override
   protected void configure(AuthenticationManagerBuilder auth) throws Exception {
       
  }
}

定制請求的授權規則

@Override
protected void configure(HttpSecurity http) throws Exception {
   // 定制請求的授權規則
   // 首頁所有人可以訪問
   http.authorizeRequests().antMatchers("/").permitAll()
  .antMatchers("/level1/**").hasRole("vip1")
  .antMatchers("/level2/**").hasRole("vip2")
  .antMatchers("/level3/**").hasRole("vip3");
}

測驗一下,發現除了首頁都進不去了!因為我們目前沒有登錄的角色,因為請求需要登錄的角色擁有對應的權限才可以!

在configure()方法中加入以下配置,開啟自動配置的登錄功能!

// 開啟自動配置的登錄功能
// /login 請求來到登錄頁
// /login?error 重定向到這里表示登錄失敗
http.formLogin();

測驗發現,沒有權限的時候,會跳轉到登錄的頁面!

查看剛才登錄頁的注釋資訊,我們可以定義認證規則,重寫configure(AuthenticationManagerBuilder auth)方法

//定義認證規則
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
   
   //在記憶體中定義,也可以在jdbc中去拿....
   auth.inMemoryAuthentication()
          .withUser("dengzj").password("aadzj").roles("vip2","vip3")
          .and()
          .withUser("root").password("aadzj").roles("vip1","vip2","vip3")
          .and()
          .withUser("guest").password("aadzj").roles("vip1","vip2");
}

測驗,我們可以使用這些賬號登錄進行測驗!發現會報錯!There is no PasswordEncoder mapped for the id “null”

原因,我們要將前端傳過來的密碼進行某種方式加密,否則就無法登錄,修改代碼

//認證
//密碼編碼:passwordEncoder,需要對密碼進行加密處理
//在SpringSecurity 5.0+ 中新增了很多的加密方法~
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    //Spring security 5.0中新增了多種加密方式,也改變了密碼的格式,
   //要想我們的專案還能夠正常登陸,需要修改一下configure中的代碼,我們要將前端傳過來的密碼進行某種方式加密
   //spring security 官方推薦的是使用bcrypt加密方式,
    auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
    .withUser("dengzi").password(new BCryptPasswordEncoder().encode("aadzj")).roles("vip2","vip3")
    .and()
    .withUser("root").password(new BCryptPasswordEncoder().encode("aadzj")).roles("vip1","vip2","vip3")
    .and()
    .withUser("guest").password(new BCryptPasswordEncoder().encode("aadzj")).roles("vip1");
}

測驗,發現,登錄成功,并且每個角色只能訪問自己認證下的規則!搞定

2、權限控制和注銷

開啟自動配置的注銷的功能

//定制請求的授權規則
@Override
protected void configure(HttpSecurity http) throws Exception {
   //....
   //開啟自動配置的注銷的功能
      // /logout 注銷請求
   http.logout();
}

在前端增加一個注銷的按鈕,index.html 導航欄中

<a  th:href="https://www.cnblogs.com/aadzj/archive/2021/12/08/@{/logout}">
    <i ></i> 注銷
</a>

測驗一下,登錄成功后點擊注銷,發現注銷完畢會跳轉到登錄頁面!

但是,如果想注銷成功后,依舊可以跳轉到首頁,該怎么處理呢?

// .logoutSuccessUrl("/"); 注銷成功來到首頁
http.logout().logoutSuccessUrl("/");

測驗,注銷完畢后,發現跳轉到首頁OK

根據真實網站需求定制

用戶沒有登錄的時候,導航欄上只顯示登錄按鈕,用戶登錄之后,導航欄可以顯示登錄的用戶資訊及注銷按鈕!還有就是,比如 dengzj 這個用戶,它只有 vip2,vip3功能,那么登錄則只顯示這兩個功能,而vip1的功能選單不顯示,這個就是真實的網站情況,該如何做呢?

我們需要結合thymeleaf中的一些功能匯入security-thymeleaf整合包

<!-- security-thymeleaf整合包 -->
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity5</artifactId>
    <version>3.0.4.RELEASE</version>
</dependency>

sec:authorize="isAuthenticated()" :判斷是否認證登錄,顯示不同的資訊

修改前端頁面(index.html)

匯入命名空間

xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security"

修改導航欄,增加認證判斷

<!--登錄注銷-->
<div >
    <!--如果未登錄,顯示登陸按鈕-->
    <div sec:authorize="!isAuthenticated()">
        <a  th:href="https://www.cnblogs.com/aadzj/archive/2021/12/08/@{/toLogin}">
            <i ></i> 登錄
        </a>
    </div>
    <!--如果登錄,顯示用戶名、注銷-->
    <div sec:authorize="isAuthenticated()">
        <a >
            用戶名:<span sec:authentication="name"></span>
            角色:<span sec:authentication="principal.authorities"></span>
        </a>
    </div>
    <div sec:authorize="isAuthenticated()">
        <a  th:href="https://www.cnblogs.com/aadzj/archive/2021/12/08/@{/logout}">
            <i ></i> 注銷
        </a>
    </div>
</div>

重啟測驗,登錄成功后確實顯示了我們想要的頁面

關閉csrf功能

如果注銷404了,就是因為它默認防止csrf跨站請求偽造,產生安全問題,我們可以將請求改為post表單提交,或者在spring security中關閉csrf功能,在配置中增加:http.csrf().disable();

http.csrf().disable();//關閉csrf功能:跨站請求偽造,默認只能通過post方式提交logout請求
http.logout().logoutSuccessUrl("/");

角色功能塊認證 sec:authorize="hasRole('xxx')"

<div >
    <!--選單根據用戶不同權限動態實作 sec:authorize="hasRole('vip1')" -->
    <div  sec:authorize="hasRole('vip1')">
        <div >
            <div >
                <div >
                    <h5 >Level 1</h5>
                    <hr>
                    <div><a th:href="https://www.cnblogs.com/aadzj/archive/2021/12/08/@{/level1/1}"><i ></i> Level-1-1</a></div>
                    <div><a th:href="https://www.cnblogs.com/aadzj/archive/2021/12/08/@{/level1/2}"><i ></i> Level-1-2</a></div>
                    <div><a th:href="https://www.cnblogs.com/aadzj/archive/2021/12/08/@{/level1/3}"><i ></i> Level-1-3</a></div>
                </div>
            </div>
        </div>
    </div>

    <div  sec:authorize="hasRole('vip2')">
        <div >
            <div >
                <div >
                    <h5 >Level 2</h5>
                    <hr>
                    <div><a th:href="https://www.cnblogs.com/aadzj/archive/2021/12/08/@{/level2/1}"><i ></i> Level-2-1</a></div>
                    <div><a th:href="https://www.cnblogs.com/aadzj/archive/2021/12/08/@{/level2/2}"><i ></i> Level-2-2</a></div>
                    <div><a th:href="https://www.cnblogs.com/aadzj/archive/2021/12/08/@{/level2/3}"><i ></i> Level-2-3</a></div>
                </div>
            </div>
        </div>
    </div>

    <div  sec:authorize="hasRole('vip3')">
        <div >
            <div >
                <div >
                    <h5 >Level 3</h5>
                    <hr>
                    <div><a th:href="https://www.cnblogs.com/aadzj/archive/2021/12/08/@{/level3/1}"><i ></i> Level-3-1</a></div>
                    <div><a th:href="https://www.cnblogs.com/aadzj/archive/2021/12/08/@{/level3/2}"><i ></i> Level-3-2</a></div>
                    <div><a th:href="https://www.cnblogs.com/aadzj/archive/2021/12/08/@{/level3/3}"><i ></i> Level-3-3</a></div>
                </div>
            </div>
        </div>
    </div>

</div>

登錄測驗,成功!權限控制和注銷搞定!

3、記住我功能

現在的情況,我們只要登錄之后,關閉瀏覽器,再登錄,就會讓我們重新登錄,但是很多網站的情況,就是有一個記住密碼的功能,這個該如何實作呢?

開啟記住我功能

//定制請求的授權規則
@Override
protected void configure(HttpSecurity http) throws Exception {
    
	// ......
   //記住我
   http.rememberMe();
    
}

啟動測驗,查看瀏覽器的cookie

再次啟動專案測驗,發現登錄頁多了一個記住我功能,登錄之后關閉瀏覽器,然后重新打開瀏覽器訪問,發現用戶依舊存在!

如何實作的呢?其實非常簡單,我們可以查看瀏覽器的cookie

注銷時cookie的洗掉 spring security 幫我們自動洗掉 cookie

結論

登錄成功后,將cookie發送給瀏覽器保存,以后登錄帶上這個cookie,只要通過檢查就可以免登錄了,如果點擊注銷,則會洗掉這個cookie,

4、定制登錄頁

現在這個登錄頁面都是spring security 默認的,怎么樣可以使用我們自己寫的Login界面呢?

在剛才的登錄頁配置后面指定 loginpage

http.formLogin().loginPage("/toLogin");
//http.formLogin().loginPage("/toLogin").usernameParameter("user").passwordParameter("pwd").loginProcessingUrl("/login");

前端也需要指向我們自己定義的 login 請求

<div sec:authorize="!isAuthenticated()">
    <a  th:href="https://www.cnblogs.com/aadzj/archive/2021/12/08/@{/toLogin}">
        <i ></i> 登錄
    </a>
</div>

login.html頁面配置

請求登錄,需要將這些資訊發送到哪里,我們也需要配置,login.html 配置提交請求及方式,方式必須為post,在 loginPage()原始碼中的注釋上有寫明:

<form th:action="@{/login}" method="post">
    <div >
        <label>Username</label>
        <div >
            <input type="text" placeholder="Username" name="user">
            <i ></i>
        </div>
    </div>
    <div >
        <label>Password</label>
        <div >
            <input type="password" name="pwd">
            <i ></i>
        </div>
    </div>
    <div >
        <input type="checkbox" name="remember">記住我
    </div>
    <input type="submit" />
</form>

接收登錄的用戶名和密碼的引數

這個請求提交上來,我們還需要驗證處理,怎么做呢?我們可以查看formLogin()方法的原始碼!我們配置接收登錄的用戶名和密碼的引數!

http.formLogin()
  .usernameParameter("username")
  .passwordParameter("password")
  .loginPage("/toLogin")
  .loginProcessingUrl("/login"); // 登陸表單提交請求

在登錄頁增加記住我的多選框

<div >
    <input type="checkbox" name="remember">記住我
</div>

后端驗證處理 rememberMe()

//開啟記住我功能,默認保存時間兩周
http.rememberMe().rememberMeParameter("remember");

測驗,OK!搞定!

5、完整配置代碼(SecurityConfig.java)

package com.dzj.config;

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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    //定義授權規則
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //首頁所有人可以訪問,功能也只有有對應權限的人才能訪問
        //請求授權的規則
        http.authorizeRequests().antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");
        //沒有權限會跳轉到登錄頁,需要開啟登錄的頁面
        http.formLogin().loginPage("/toLogin").usernameParameter("username").passwordParameter("password").loginProcessingUrl("/login");

        //防止網站攻擊
        http.csrf().disable();  //關閉csrf功能,登錄失敗存在的原因

        //注銷
        http.logout().logoutSuccessUrl("/"); //注銷成功后跳轉到哪個位置

        //開啟記住我功能,默認保存時間兩周
        http.rememberMe().rememberMeParameter("remember");
    }

    //定義認證規則
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //在記憶體中定義,也可以在jdbc中去拿....
       //Spring security 5.0中新增了多種加密方式,也改變了密碼的格式,
       //要想我們的專案還能夠正常登陸,需要修改一下configure中的代碼,我們要將前端傳過來的密碼進行某種方式加密
       //spring security 官方推薦的是使用bcrypt加密方式,
        
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
        .withUser("dengzi").password(new BCryptPasswordEncoder().encode("aadzj")).roles("vip2","vip3")
        .and()
        .withUser("root").password(new BCryptPasswordEncoder().encode("aadzj")).roles("vip1","vip2","vip3")
        .and()
        .withUser("guest").password(new BCryptPasswordEncoder().encode("aadzj")).roles("vip1");
    }
}

本文來自博客園,作者:小公羊,轉載請注明原文鏈接:https://www.cnblogs.com/aadzj/p/15636802.html

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/376850.html

標籤:其他

上一篇:python魔術方法__call__的研究

下一篇:java 代碼生成器設計方案

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more