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

Java SpringBoot安全框架整合Spring Security詳解

 更新時(shí)間:2021年09月14日 09:59:04   作者:DrLai  
這篇文章主要介紹了Spring Boot整合Spring Security的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

1.工業(yè)級(jí)安全框架介紹

Spring Security基于Spring開發(fā),項(xiàng)目中如果使用Spring作為基礎(chǔ),配合Spring Security做權(quán)限更加方便,而Shiro需要和Spring進(jìn)行整合開發(fā)。因此作為spring全家桶中的Spring Security在java領(lǐng)域很常用。

2.建議搭建Spring Security環(huán)境

2.1在pom.xml中添加相關(guān)依賴

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>springsecurityReview</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <artifactId>spring-boot-dependencies</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.5.4</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>
</project>

2.2創(chuàng)建Handler類

package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class Handler {
    @GetMapping("/index")
    public String index(){
        return "index";
    }
}

2.3創(chuàng)建簡(jiǎn)單的html和配置相關(guān)thymeleaf的路徑

2.4最后再加個(gè)啟動(dòng)類,那么我們的整合測(cè)試就完成勒

package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

2.5成果展示 用戶名默認(rèn)user,密碼則隨機(jī)生成的這串?dāng)?shù)字

3.進(jìn)階版使用

3.1用戶名和密碼自定義

3.2在config包下創(chuàng)建Encoder

進(jìn)行密碼的校驗(yàn)和轉(zhuǎn)碼操作,將密碼轉(zhuǎn)成字符串形式,并通過match方法驚醒校驗(yàn)。

3.3賦予賬號(hào)角色權(quán)限

package com.example.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;
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    //角色和資源的關(guān)系
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/admin").hasRole("ADMIN")
        .antMatchers("/index").access("hasRole('ADMIN') or hasRole('USER') ")
                .anyRequest().authenticated()
        .and()
        .formLogin()
        .loginPage("/login")
        .permitAll()
        .and()
        .logout()
        .permitAll()
        .and()
        .csrf()
        .disable();
    }
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
       auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder())
       .withUser("user").password(new MyPasswordEncoder()
       .encode("000")).roles("USER")
       .and()
       .withUser("admin").password(new MyPasswordEncoder()
       .encode("123")).roles("ADMIN","USER");
    }
}

最后達(dá)到admin賬號(hào)能訪問admin.html和index.html

user只能訪問index.html的操作

總結(jié)

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

相關(guān)文章

  • Netty學(xué)習(xí)教程之Netty與Marshalling結(jié)合發(fā)送對(duì)象

    Netty學(xué)習(xí)教程之Netty與Marshalling結(jié)合發(fā)送對(duì)象

    Netty是由JBOSS提供的一個(gè)Java開源框架,之前已經(jīng)給大家簡(jiǎn)單介紹了一些基礎(chǔ)與使用,下面這篇文章主要給大家介紹了關(guān)于Netty與Marshalling結(jié)合發(fā)送對(duì)象的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-05-05
  • JDK1.7 Paths,Files類實(shí)現(xiàn)文件夾的復(fù)制與刪除的實(shí)例

    JDK1.7 Paths,Files類實(shí)現(xiàn)文件夾的復(fù)制與刪除的實(shí)例

    下面小編就為大家分享一篇JDK1.7 Paths,Files類實(shí)現(xiàn)文件夾的復(fù)制與刪除的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。以前跟隨小編過來看看吧
    2017-11-11
  • Java8中用foreach循環(huán)獲取對(duì)象的index下標(biāo)詳解

    Java8中用foreach循環(huán)獲取對(duì)象的index下標(biāo)詳解

    這篇文章主要給大家介紹了關(guān)于Java8中用foreach循環(huán)獲取對(duì)象的index下標(biāo)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Spring5學(xué)習(xí)之基礎(chǔ)知識(shí)總結(jié)

    Spring5學(xué)習(xí)之基礎(chǔ)知識(shí)總結(jié)

    這篇文章主要介紹了Spring5學(xué)習(xí)之基礎(chǔ)知識(shí)總結(jié),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-05-05
  • Spring中Bean的創(chuàng)建流程詳細(xì)解讀

    Spring中Bean的創(chuàng)建流程詳細(xì)解讀

    這篇文章主要介紹了Spring中Bean的創(chuàng)建流程詳細(xì)解讀,Spring 中創(chuàng)建 Bean ,是通過調(diào)用 GetBean 方法來觸發(fā)的,所以,我們會(huì)從這個(gè)方法開始,需要的朋友可以參考下
    2023-10-10
  • SpringCloud配置動(dòng)態(tài)更新原理解析

    SpringCloud配置動(dòng)態(tài)更新原理解析

    在微服務(wù)架構(gòu)的浩瀚星海中,服務(wù)配置的動(dòng)態(tài)更新如同魔法一般,能夠讓應(yīng)用在不重啟的情況下,實(shí)時(shí)響應(yīng)配置的變更,Spring Cloud作為微服務(wù)架構(gòu)中的佼佼者,其動(dòng)態(tài)配置更新的能力尤為引人注目,本文給大家介紹了SpringCloud配置動(dòng)態(tài)更新原理,需要的朋友可以參考下
    2025-01-01
  • 淺析Java中的繼承與組合

    淺析Java中的繼承與組合

    本文將介紹組合和繼承的概念及區(qū)別,并從多方面分析在寫代碼時(shí)如何進(jìn)行選擇。文中通過示例代碼介紹的很詳細(xì),有需要的朋友可以參考借鑒,下面來一起看看吧。
    2016-12-12
  • 詳解spring cloud config整合gitlab搭建分布式的配置中心

    詳解spring cloud config整合gitlab搭建分布式的配置中心

    這篇文章主要介紹了詳解spring cloud config整合gitlab搭建分布式的配置中心,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • JDBC環(huán)境設(shè)置(中文詳解)

    JDBC環(huán)境設(shè)置(中文詳解)

    要開始使用JDBC設(shè)置,按照以下所示的步驟開發(fā)JDBC環(huán)境。以下內(nèi)容假設(shè)Windows平臺(tái)上
    2014-03-03
  • 深入了解Java核心類庫(kù)--BigDecimal和System類

    深入了解Java核心類庫(kù)--BigDecimal和System類

    這篇文章主要為大家詳細(xì)介紹了javaBigDecimal和System類定義與使用的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能給你帶來幫助
    2021-07-07

最新評(píng)論

云林县| 台江县| 青神县| 益阳市| 东方市| 江安县| 巴林左旗| 阿坝县| 东台市| 龙南县| 商城县| 鄱阳县| 北宁市| 台东县| 同江市| 张掖市| 布拖县| 南昌市| 沙田区| 江津市| 永吉县| 嘉峪关市| 大足县| 西乡县| 泰兴市| 玛纳斯县| 广水市| 司法| 九龙坡区| 介休市| 谷城县| 靖边县| 云龙县| 论坛| 阿拉尔市| 莱芜市| 宜州市| 甘谷县| 泌阳县| 宜宾县| 龙岩市|