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

詳解Springboot2.3集成Spring security 框架(原生集成)

 更新時(shí)間:2020年08月12日 10:20:55   作者:Jack方  
這篇文章主要介紹了詳解Springboot2.3集成Spring security 框架(原生集成),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

0、pom

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.0.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.jack</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>demo</name>
	<description>Demo project for Spring Security</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

1、SpringSecurityConfig(security配置)

// 手動(dòng)定義用戶認(rèn)證 和 // 關(guān)聯(lián)用戶Service認(rèn)證 二者取一

這里測(cè)試用的是 手動(dòng)定義用戶認(rèn)證!??!

package com.jack.demo;

import org.springframework.beans.factory.annotation.Autowired;
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.builders.WebSecurity;
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;

/**
 * @program: demo
 * @description: Security 配置
 * @author: Jack.Fang
 * @date:2020-06-01 1541
 **/

@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

  @Autowired
  private MyUserService myUserService;

  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    // 手動(dòng)定義用戶認(rèn)證
    auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("admin").password(new BCryptPasswordEncoder().encode("123456")).roles("ADMIN");
    auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("jack").password(new BCryptPasswordEncoder().encode("fang")).roles("USER");

    // 關(guān)聯(lián)用戶Service認(rèn)證
    //auth.userDetailsService(myUserService).passwordEncoder(new MyPasswordEncoder());

    // 默認(rèn)jdbc認(rèn)證
    // auth.jdbcAuthentication().usersByUsernameQuery("").authoritiesByUsernameQuery("").passwordEncoder(new MyPasswordEncoder());
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/").permitAll()
        .anyRequest().authenticated()
        .and()
        .logout().permitAll()
        .and()
        .formLogin();
    http.csrf().disable();
  }

  @Override
  public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/js/**","/css/**","/image/**");
  }
}

2、MyPasswordEncoder(自定義密碼比較)

package com.jack.demo;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @program: demo
 * @description: 密碼加密
 * @author: Jack.Fang
 * @date:2020-06-01 1619
 **/
public class MyPasswordEncoder implements PasswordEncoder {

  @Override
  public String encode(CharSequence charSequence) {
    return new BCryptPasswordEncoder().encode(charSequence.toString());
  }

  @Override
  public boolean matches(CharSequence charSequence, String s) {
    return new BCryptPasswordEncoder().matches(charSequence,s);
  }
}

3、MyUserService(自行實(shí)現(xiàn)的用戶登錄接口)

具體內(nèi)容 省略。這里測(cè)試用的是SpringSecurityConfig手動(dòng)添加用戶名與密碼。

package com.jack.demo;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * @program: demo
 * @description: 用戶
 * @author: Jack.Fang
 * @date:2020-06-01 1617
 **/
@Component
public class MyUserService implements UserDetailsService {

  @Override
  public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
    return null;
  }
}

4、啟動(dòng)類(測(cè)試)

DemoApplication.java

package com.jack.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PreFilter;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@EnableGlobalMethodSecurity(prePostEnabled = true)
@RestController
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

	@RequestMapping("/")
	public String index(){
		return "hello Spring Security!";
	}

	@RequestMapping("/hello")
	public String hello(){
		return "hello !";
	}

	@PreAuthorize("hasRole('ROLE_ADMIN')")
	@RequestMapping("/roleAdmin")
	public String role() {
		return "admin auth";
	}


	@PreAuthorize("#id<10 and principal.username.equals(#username) and #user.username.equals('abc')")
	@PostAuthorize("returnObject%2==0")
	@RequestMapping("/test")
	public Integer test(Integer id, String username, User user) {
		// ...
		return id;
	}

	@PreFilter("filterObject%2==0")
	@PostFilter("filterObject%4==0")
	@RequestMapping("/test2")
	public List<Integer> test2(List<Integer> idList) {
		// ...
		return idList;
	}
}

測(cè)試hello接口(http://localhost:8080/hello)

未登錄跳轉(zhuǎn)登錄頁(yè)


登錄SpringSecurityConfig配置的admin賬號(hào)與密碼123456
成功調(diào)用hello

測(cè)試roleAdmin(登錄admin 123456成功,登錄jack fang訪問(wèn)則失敗)


登出 logout

到此這篇關(guān)于詳解Springboot2.3集成Spring security 框架(原生集成)的文章就介紹到這了,更多相關(guān)Springboot2.3集成Spring security 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Java如何實(shí)現(xiàn)與JS相同的Des加解密算法

    詳解Java如何實(shí)現(xiàn)與JS相同的Des加解密算法

    這篇文章主要介紹了如何在Java中實(shí)現(xiàn)與JavaScript相同的DES(Data Encryption Standard)加解密算法,確保在兩個(gè)平臺(tái)之間可以無(wú)縫地傳遞加密信息,希望對(duì)大家有一定的幫助
    2025-04-04
  • jstack配合top命令分析CPU飆高、程序死鎖問(wèn)題

    jstack配合top命令分析CPU飆高、程序死鎖問(wèn)題

    記得前段時(shí)間,同事說(shuō)他們測(cè)試環(huán)境的服務(wù)器cpu使用率一直處于100%,本地又沒有什么接口調(diào)用,為什么會(huì)這樣?cpu使用率居高不下,自然是有某些線程一直占用著cpu資源,那又如何查看占用cpu較高的線程
    2021-09-09
  • 淺析java 10中的var關(guān)鍵字用法

    淺析java 10中的var關(guān)鍵字用法

    2018年3月20日,Oracle發(fā)布java10。java10為java帶來(lái)了很多新特性。這篇文章主要介紹了Java 10 var關(guān)鍵字詳解和示例教程,需要的朋友可以參考下
    2018-10-10
  • java compareTo和compare方法比較詳解

    java compareTo和compare方法比較詳解

    這篇文章主要介紹了java compareTo和compare方法比較詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • springboot static關(guān)鍵字真能提高Bean的優(yōu)先級(jí)(厲害了)

    springboot static關(guān)鍵字真能提高Bean的優(yōu)先級(jí)(厲害了)

    這篇文章主要介紹了springboot static關(guān)鍵字真能提高Bean的優(yōu)先級(jí)(厲害了),需要的朋友可以參考下
    2020-07-07
  • java實(shí)現(xiàn)隨機(jī)數(shù)生成器

    java實(shí)現(xiàn)隨機(jī)數(shù)生成器

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)隨機(jī)數(shù)生成器,隨機(jī)數(shù)生成小程序,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • 淺析Spring的JdbcTemplate方法

    淺析Spring的JdbcTemplate方法

    本篇淺析Spring的JdbcTemplate方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-01-01
  • Eclipse中導(dǎo)入Maven Web項(xiàng)目并配置其在Tomcat中運(yùn)行圖文詳解

    Eclipse中導(dǎo)入Maven Web項(xiàng)目并配置其在Tomcat中運(yùn)行圖文詳解

    這篇文章主要介紹了Eclipse中導(dǎo)入Maven Web項(xiàng)目并配置其在Tomcat中運(yùn)行圖文詳解,需要的朋友可以參考下
    2017-12-12
  • Spring Boot如何讀取自定義外部屬性詳解

    Spring Boot如何讀取自定義外部屬性詳解

    這篇文章主要給大家介紹了關(guān)于Spring Boot如何讀取自定義外部屬性的相關(guān)資料,這個(gè)功能實(shí)現(xiàn)介紹的很詳細(xì),需要的朋友可以參考下
    2021-05-05
  • Mybatis中返回Map的實(shí)現(xiàn)

    Mybatis中返回Map的實(shí)現(xiàn)

    這篇文章主要介紹了Mybatis中返回Map的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03

最新評(píng)論

吴忠市| 莱州市| 陵水| 余姚市| 文水县| 双江| 鄄城县| 文昌市| 灵山县| 康马县| 宜宾市| 葫芦岛市| 平塘县| 仁寿县| 衢州市| 秦皇岛市| 临泽县| 望城县| 龙江县| 临夏市| 大埔县| 连城县| 宜春市| 三都| 潍坊市| 霍城县| 工布江达县| 高安市| 屯门区| 昆山市| 大连市| 耒阳市| 德令哈市| 米易县| 庐江县| 运城市| 山东省| 太仓市| 攀枝花市| 和平区| 九龙城区|