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

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;
    }
}

當然也可以在數(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)文章

  • Java實現(xiàn)Excel百萬級數(shù)據(jù)導(dǎo)入功能的示例代碼

    Java實現(xiàn)Excel百萬級數(shù)據(jù)導(dǎo)入功能的示例代碼

    這篇文章主要為大家詳細介紹了Java如何實現(xiàn)Excel百萬級數(shù)據(jù)導(dǎo)入功能,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考下
    2024-04-04
  • java生成申請單序列號的實現(xiàn)方法

    java生成申請單序列號的實現(xiàn)方法

    申請單序列號一般要求根據(jù)一定的規(guī)則生成后幾位連續(xù)的字符串,下面是我項目中使用的生成序列號的代碼,其中用到了鎖機制,有需要的朋友可以參考一下
    2014-01-01
  • Java計算兩個字符相似度的幾種常用方法

    Java計算兩個字符相似度的幾種常用方法

    這篇文章主要給大家介紹了關(guān)于Java計算兩個字符相似度的幾種常用方法,這是一個很實用的功能,該方法需要傳入兩個字符串,經(jīng)過計算會返回兩個字符串的相似度,需要的朋友可以參考下
    2023-10-10
  • dm.jdbc.driver.DMException網(wǎng)絡(luò)通信異常的解決過程

    dm.jdbc.driver.DMException網(wǎng)絡(luò)通信異常的解決過程

    最近一個項目里面出現(xiàn)了一個比較詭異的問題,給大家分享下,這篇文章主要給大家介紹了關(guān)于dm.jdbc.driver.DMException網(wǎng)絡(luò)通信異常的解決過程,需要的朋友可以參考下
    2023-02-02
  • SpringBoot實現(xiàn)分頁功能

    SpringBoot實現(xiàn)分頁功能

    這篇文章主要為大家詳細介紹了SpringBoot實現(xiàn)分頁功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • JavaSwing GridLayout 網(wǎng)格布局的實現(xiàn)代碼

    JavaSwing GridLayout 網(wǎng)格布局的實現(xiàn)代碼

    這篇文章主要介紹了JavaSwing GridLayout 網(wǎng)格布局的實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2019-12-12
  • Netty中的心跳檢測機制詳解

    Netty中的心跳檢測機制詳解

    這篇文章主要介紹了Netty中的心跳檢測機制詳解,Netty 是 基于 TCP 協(xié)議開發(fā)的,在四層協(xié)議 TCP 協(xié)議的實現(xiàn)中也提供了 keepalive 報文用來探測對端是否可用,TCP 層將在定時時間到后發(fā)送相應(yīng)的 KeepAlive 探針以確定連接可用性,需要的朋友可以參考下
    2023-12-12
  • Java+swing實現(xiàn)抖音上的表白程序詳解

    Java+swing實現(xiàn)抖音上的表白程序詳解

    這篇文章主要為大家詳細介紹了如何利用Java?swing實現(xiàn)抖音上的表白程序,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習一下
    2022-06-06
  • springboot實現(xiàn)瀏覽器截屏并添加文字

    springboot實現(xiàn)瀏覽器截屏并添加文字

    大家好,本篇文章主要講的是springboot實現(xiàn)瀏覽器截屏并添加文字,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02
  • Java中 ? extends T 和 ? super T的理解

    Java中 ? extends T 和 ? super&nb

    本文主要介紹了Java中 ? extends T 和 ? super T的理解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2022-05-05

最新評論