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

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

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

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

Spring Security基于Spring開(kāi)發(fā),項(xiàng)目中如果使用Spring作為基礎(chǔ),配合Spring Security做權(quán)限更加方便,而Shiro需要和Spring進(jìn)行整合開(kāi)發(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類(lèi)

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)類(lèi),那么我們的整合測(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)成字符串形式,并通過(guò)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)能訪問(wèn)admin.html和index.html

user只能訪問(wèn)index.html的操作

總結(jié)

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

相關(guān)文章

  • spring?boot+vue實(shí)現(xiàn)JSAPI微信支付的完整步驟

    spring?boot+vue實(shí)現(xiàn)JSAPI微信支付的完整步驟

    JSAPI支付是用戶在微信中打開(kāi)商戶的H5頁(yè)面,商戶在H5頁(yè)面通過(guò)調(diào)用微信支付提供的JSAPI接口調(diào)起微信支付模塊完成支付,下面這篇文章主要給大家介紹了關(guān)于spring?boot+vue實(shí)現(xiàn)JSAPI微信支付的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • Spring5路徑匹配器PathPattern解析

    Spring5路徑匹配器PathPattern解析

    這篇文章主要介紹了Spring5路徑匹配器PathPattern,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java中IO流簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java中IO流簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java io系統(tǒng)的設(shè)計(jì)初衷,就是為了實(shí)現(xiàn)“文件、控制臺(tái)、網(wǎng)絡(luò)設(shè)備”這些io設(shè)置的通信。接下來(lái)通過(guò)本文給大家介紹Java中IO流簡(jiǎn)介,感興趣的朋友一起看看吧
    2017-05-05
  • grade構(gòu)建閱讀spring源碼環(huán)境 Idea2020.3的過(guò)程

    grade構(gòu)建閱讀spring源碼環(huán)境 Idea2020.3的過(guò)程

    這篇文章主要介紹了grade構(gòu)建閱讀spring源碼環(huán)境 Idea2020.3,本文分步驟通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • SpringBoot之Json的序列化和反序列化問(wèn)題

    SpringBoot之Json的序列化和反序列化問(wèn)題

    這篇文章主要介紹了SpringBoot之Json的序列化和反序列化問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • springboot 中 druid+jpa+MYSQL數(shù)據(jù)庫(kù)配置過(guò)程

    springboot 中 druid+jpa+MYSQL數(shù)據(jù)庫(kù)配置過(guò)程

    這篇文章主要介紹了springboot 中 druid+jpa+MYSQL數(shù)據(jù)庫(kù)配置,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • Java Swing樹(shù)狀組件JTree用法實(shí)例詳解

    Java Swing樹(shù)狀組件JTree用法實(shí)例詳解

    這篇文章主要介紹了Java Swing樹(shù)狀組件JTree用法,結(jié)合具體實(shí)例形式分析了Swing組件JTree構(gòu)成樹(shù)狀列表的節(jié)點(diǎn)設(shè)置與事件響應(yīng),以及自定義圖形節(jié)點(diǎn)的相關(guān)操作技巧,需要的朋友可以參考下
    2017-11-11
  • 老生常談Java String字符串(必看篇)

    老生常談Java String字符串(必看篇)

    下面小編就為大家?guī)?lái)一篇老生常談Java String字符串(必看篇)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • 淺談javaSE GUI (Action事件)

    淺談javaSE GUI (Action事件)

    下面小編就為大家?guī)?lái)一篇淺談javaSE GUI (Action事件)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-06-06
  • Java實(shí)現(xiàn)獲取圖片和視頻文件的Exif信息

    Java實(shí)現(xiàn)獲取圖片和視頻文件的Exif信息

    這篇文章將重點(diǎn)為大家介紹一下如何使用Java編程語(yǔ)言結(jié)合metadata-extractor去自動(dòng)獲取全景圖片的Exif信息,獲取照片的拍攝坐標(biāo)信息,感興趣的可以了解一下
    2022-11-11

最新評(píng)論