亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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配置)

// 手動定義用戶認證 和 // 關聯(lián)用戶Service認證 二者取一

這里測試用的是 手動定義用戶認證!??!

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 {
    // 手動定義用戶認證
    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");

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

    // 默認jdbc認證
    // 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(自行實現(xiàn)的用戶登錄接口)

具體內容 省略。這里測試用的是SpringSecurityConfig手動添加用戶名與密碼。

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、啟動類(測試)

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

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

未登錄跳轉登錄頁


登錄SpringSecurityConfig配置的admin賬號與密碼123456
成功調用hello

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


登出 logout

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

相關文章

  • Spring Bean生命周期之屬性賦值階段詳解

    Spring Bean生命周期之屬性賦值階段詳解

    這篇文章主要為大家詳細介紹了Spring Bean生命周期之屬性賦值階段,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • Java三大特性-封裝知識小結

    Java三大特性-封裝知識小結

    所有的面向對象編程語言的思路都是差不多的,而這三大特性,則是思路中的支柱點,接下來我就重點講解了一下java三大特性-封裝,感興趣的朋友跟隨腳本之家小編一起看看吧
    2018-03-03
  • shiro實現(xiàn)單點登錄(一個用戶同一時刻只能在一個地方登錄)

    shiro實現(xiàn)單點登錄(一個用戶同一時刻只能在一個地方登錄)

    這篇文章主要介紹了shiro實現(xiàn)單點登錄(一個用戶同一時刻只能在一個地方登錄)的相關資料,非常不錯,具有參考借鑒價值,感興趣的朋友一起學習吧
    2016-08-08
  • Java實現(xiàn)Web應用中的定時任務(實例講解)

    Java實現(xiàn)Web應用中的定時任務(實例講解)

    下面小編就為大家分享一篇Java實現(xiàn)Web 應用中的定時任務的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-11-11
  • SpringBoot3中數(shù)據(jù)庫集成實踐詳解

    SpringBoot3中數(shù)據(jù)庫集成實踐詳解

    項目工程中,集成數(shù)據(jù)庫實現(xiàn)對數(shù)據(jù)的增曬改查管理,是最基礎的能力,所以下面小編就來和大家講講SpringBoot3如何實現(xiàn)數(shù)據(jù)庫集成,需要的可以參考下
    2023-08-08
  • 細數(shù)java中Long與Integer比較容易犯的錯誤總結

    細數(shù)java中Long與Integer比較容易犯的錯誤總結

    下面小編就為大家?guī)硪黄殧?shù)java中Long與Integer比較容易犯的錯誤總結。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • 一篇文章從無到有詳解Spring中的AOP

    一篇文章從無到有詳解Spring中的AOP

    。Spring AOP 是基于 AOP 編程模式的一個框架,它的使用有效減少了系統(tǒng)間的重復代碼,達到了模塊間的松耦合目的,這篇文章主要給大家介紹了關于Spring中AOP的相關資料,需要的朋友可以參考下
    2021-08-08
  • 基于maven使用IDEA創(chuàng)建多模塊項目

    基于maven使用IDEA創(chuàng)建多模塊項目

    這篇文章主要介紹了基于maven使用IDEA創(chuàng)建多模塊項目,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • logback的DuplicateMessageFilter日志過濾操作源碼解讀

    logback的DuplicateMessageFilter日志過濾操作源碼解讀

    這篇文章主要為大家介紹了logback的DuplicateMessageFilter日志過濾操作源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • Java策略模式實現(xiàn)簡單購物車功能

    Java策略模式實現(xiàn)簡單購物車功能

    這篇文章主要介紹了Java策略模式實現(xiàn)簡單地購物車,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08

最新評論