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

java SpringSecurity使用詳解

 更新時間:2021年08月31日 16:55:40   作者:你好y  
這篇文章主要介紹了java中Spring Security的實例詳解的相關(guān)資料,spring security是一個多方面的安全認證框架,提供了基于JavaEE規(guī)范的完整的安全認證解決方案,需要的朋友可以參考下

SpringSecurity

shrio,SpringSecurity:認證,授權(quán)(VIP1,vip2…)

  • 功能權(quán)限
  • 訪問權(quán)限
  • 菜單權(quán)限
  • 攔截器,過濾器:大量的原生代碼,冗余

1、pom.xml

 <!--Thymeleaf-->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
        </dependency>

簡介

Spring Security是針對Spring項目的安全框架,也是Spring Boot底層安全模塊默認的技術(shù)選型,他可以實現(xiàn)強大的Web安全控制,對于安全控制,我們僅需要引入Spring-boot-starter-security模塊,進行少量的配置,即可實現(xiàn)強大的安全管理!
記住幾個類:

  • WebSecurityConfigurerAdapter: 自定義Security策略
  • AuthenticationManagerBuilder:自定義認證策略
  • @EnableWebSecurity: 開啟WebSecurity模式 @Enablexxxx 開啟某個功能

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

“認證”(Authentication)
“授權(quán)”(Authorization)
這個概念是通用的,而不是只在Spring Security中存在。

1、pom.xml

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

2、Security的controller

package com.kuang.config;
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.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/*權(quán)限驗證的配置類,要先繼承WebSecurityConfigurerAdapter*/
@Configuration
public class SecurityConfig  extends WebSecurityConfigurerAdapter {
    //定義訪問規(guī)則:首頁每個人都可以訪問,但是功能也只有特定權(quán)限的人才能訪問   鏈式編程
    //授權(quán)
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");
        //沒有權(quán)限默認跳轉(zhuǎn)到登陸頁面  /login
        http.formLogin();
    }
    //認證
    /* 密碼編碼: BCryptPasswordEncoder()
    不編碼會報下面的錯誤
    * java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
    * */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("qin").password(new BCryptPasswordEncoder().encode("111")).roles("vip1","vip2","vip3")
                .and()
                .withUser("aaa").password(new BCryptPasswordEncoder().encode("111")).roles("vip1");
    }
}

3、路徑轉(zhuǎn)發(fā)的controller

package com.kuang.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Controller
public class PathController {
    @GetMapping({"/","index"})    //"/""index"都會去到index.html
    public String Toindex(){
        return "index";
    }
    @GetMapping("/toLogin")
    public String Tologin(){
        return "views/login";
    }
    @GetMapping("/level1/{id}")    //@PathVariable獲得url的占位符里面的值
    public String ToView(@PathVariable("id")int id){
        return "views/level1/"+id;
    }
    @GetMapping("/level2/{id}")
    public String ToView2(@PathVariable("id")int id){
        return "views/level2/"+id;
    }
    @GetMapping("/level3/{id}")
    public String ToView3(@PathVariable("id")int id){
        return "views/level3/"+id;
    }
}

當(dāng)然也可以在數(shù)據(jù)庫中拿信息

源碼分析

沒有權(quán)限的話會自動轉(zhuǎn)發(fā)到/login

//沒有權(quán)限默認跳轉(zhuǎn)到登陸頁面 /login
http.formLogin();

//開啟注銷
http.logout();

注銷及權(quán)限控制

 <!--thymeleof整合security-->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>
xmlns:sec=http://www.thymeleaf.org/extras/spring-security  

用spring-security實現(xiàn)用戶登錄后顯示用戶角色的信息

1、導(dǎo)入依賴thymeleof整合security

 <!--thymeleof整合security-->
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>

2、html命名空間

<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

3、根據(jù)用戶的登錄狀態(tài)進行判斷顯示該有的信息

4、根據(jù)源碼寫表單name屬性

5、實現(xiàn)有什么權(quán)限顯示什么樣的信息

6、注銷logout-404

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • JavaWeb Refresh響應(yīng)頭代碼實例詳解

    JavaWeb Refresh響應(yīng)頭代碼實例詳解

    這篇文章主要介紹了JavaWeb Refresh響應(yīng)頭代碼實例詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-02-02
  • 搞懂JAVAObject中的hashCode()

    搞懂JAVAObject中的hashCode()

    今天小編就為大家分享一篇關(guān)于關(guān)于Object中equals方法和hashCode方法判斷的分析,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2021-08-08
  • SpringBoot利用Validation包實現(xiàn)高效參數(shù)校驗

    SpringBoot利用Validation包實現(xiàn)高效參數(shù)校驗

    如果不進行校驗就直接使用這些數(shù)據(jù),可能會導(dǎo)致各種問題,那么SpringBoot如何利用Validation包實現(xiàn)高效參數(shù)校驗?zāi)?下面讓我們一起來探討這個重要的話題吧
    2025-04-04
  • IDEA 自定義方法注解模板的實現(xiàn)方法

    IDEA 自定義方法注解模板的實現(xiàn)方法

    這篇文章主要介紹了IDEA 自定義方法注解模板的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • mybatis中的mapper.xml使用循環(huán)語句

    mybatis中的mapper.xml使用循環(huán)語句

    這篇文章主要介紹了mybatis中的mapper.xml使用循環(huán)語句,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java常用工具類庫——Hutool的使用簡介

    Java常用工具類庫——Hutool的使用簡介

    這篇文章主要介紹了Java常用工具類庫——Hutool的使用簡介,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-04-04
  • Java使用雙異步實現(xiàn)將Excel的數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫

    Java使用雙異步實現(xiàn)將Excel的數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫

    在開發(fā)中,我們經(jīng)常會遇到這樣的需求,將Excel的數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫中,這篇文章主要來和大家講講Java如何使用雙異步實現(xiàn)將Excel的數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫,感興趣的可以了解下
    2024-01-01
  • springboot 在idea中實現(xiàn)熱部署的方法

    springboot 在idea中實現(xiàn)熱部署的方法

    這篇文章主要介紹了springboot 在idea中實現(xiàn)熱部署的方法,實現(xiàn)了熱部署,在每一次作了修改之后,都會自動的重啟,非常節(jié)約時間,感興趣的小伙伴們可以參考一下
    2018-10-10
  • springboot shardingjdbc與druid數(shù)據(jù)源沖突問題及解決

    springboot shardingjdbc與druid數(shù)據(jù)源沖突問題及解決

    這篇文章主要介紹了springboot shardingjdbc與druid數(shù)據(jù)源沖突問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • java FileWriter 追加文件及文件改名方式

    java FileWriter 追加文件及文件改名方式

    這篇文章主要介紹了java FileWriter 追加文件及文件改名的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07

最新評論

桐柏县| 安平县| 原平市| 兰州市| 曲水县| 鄢陵县| 大渡口区| 平定县| 贞丰县| 寻甸| 诸城市| 永吉县| 惠安县| 龙里县| 甘洛县| 永登县| 南投市| 明星| 南宫市| 庆安县| 洪泽县| 宁德市| 英山县| 新宁县| 宁乡县| 色达县| 汤阴县| 昌乐县| 仙居县| 宁都县| 常宁市| 长治县| 班戈县| 沂源县| 望江县| 民丰县| 西宁市| 全南县| 德令哈市| 犍为县| 方正县|