亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Springboot2.3集成Spring security框架的案例

發布時間:2020-08-13 11:40:55 來源:億速云 閱讀:348 作者:小新 欄目:開發技術

這篇文章主要介紹Springboot2.3集成Spring security框架的案例,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

0、pom

<&#63;xml version="1.0" encoding="UTF-8"&#63;>
<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配置)

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

    // 關聯用戶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(自行實現的用戶登錄接口)

具體內容 省略。這里測試用的是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)

未登錄跳轉登錄頁

Springboot2.3集成Spring security框架的案例

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

Springboot2.3集成Spring security框架的案例

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

Springboot2.3集成Spring security框架的案例

Springboot2.3集成Spring security框架的案例

登出 logout

Springboot2.3集成Spring security框架的案例

以上是Springboot2.3集成Spring security框架的案例的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

苍山县| 临潭县| 大连市| 航空| 渭南市| 武鸣县| 兴文县| 昌江| 日喀则市| 旬阳县| 永昌县| 连城县| 黑龙江省| 左权县| 乌兰县| 营山县| 淮阳县| 平遥县| 博爱县| 页游| 绍兴县| 蒲江县| 鹤壁市| 阳谷县| 沽源县| 响水县| 泸水县| 宁明县| 沂水县| 开原市| 葫芦岛市| 赤峰市| 高雄县| 安康市| 黔西县| 舞钢市| 会泽县| 临夏市| 靖江市| 宁波市| 华宁县|