Spring Security

簡介

Spring Security是Spring家族中的一個組成框架,具有強大且高度可定製的身份驗證和訪問控制功能,致力於為Java應用程式提供身份的驗證和授權

(先來一個小案例叭)

本人的環境如下

IDEA:2019.3.5

Maven: 3.6.3

JDK: 1.8

1.建立一個Maven專案

2.引入依賴

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent> <dependencies>
<!--spring boot web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!--Spring Security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>

3.建立啟動類

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

4.寫一個測試方法(Controller層)

@RestController
@RequestMapping("/test")
public class SecurityController { @RequestMapping("sayHello")
public String sayHello() {
return "Hello Spring Security";
}
}

然後我們就可以啟動我們的小demo啦,啟動的時候輸出控制檯會列印Spring Security的登入密碼(每次啟動都會重新初始化),是由UUID生成的,使用者名稱預設是user,輸入使用者名稱和密碼,登入就成功啦。

5.修改登入的使用者名稱和密碼

在resources目錄下建立一個配置檔案application.properties(application.yml),

# 自定義 spring security使用者名稱和密碼
spring.security.user.name=huang
spring.security.user.password=123456

不想要Spring Security的登入也是可以去掉的(關閉驗證),只要把Security的自動配置去掉就可以啦,在啟動類的@SpringBootApplication註解中新增就好。

@SpringBootApplication(exclude = SecurityAutoConfiguration.class)

6.基於記憶體的使用者資訊

有時候我們的使用者名稱和密碼太多,寫在配置檔案中不好,可以把使用者名稱和密碼儲存到記憶體中進行管理。

1)要寫一個配置類

一個繼承了 WebSecurityConfigurerAdapter抽象類的類,重寫它的 config 方法,在方法裡面新增使用者

//新增為配置類(相當於spring的xml檔案)
@Configuration
//開啟Spring Security功能
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//加密
PasswordEncoder pe = passwordEncoder(); auth.inMemoryAuthentication()
.withUser("huangxc")
.password(pe.encode("123456"))
.roles();
auth.inMemoryAuthentication()
.withUser("xian")
.password(pe.encode("654321"))
.roles();
auth.inMemoryAuthentication()
.withUser("admin")
.password(pe.encode("admin"))
.roles();
}
}

這樣當我們啟動專案時,就可以使用config方法裡面配置的使用者名稱和密碼了。如果你的Spring Security版本是5(現在只出到5)的話,是會報錯的(java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"),原因是密碼不能使用明文的方式,要進行加密。

配置類中加如下程式碼進行加密就好,PasswordEncoder是一個介面,有很多加密演算法的子類,而 new BCryptPasswordEncoder就是其中一個。

    //把方法新增到spring容器中
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
2)對使用者新增角色

在專案中,一個使用者往往可以具有多個角色的許可權,可以在新增使用者的時候進行設定,在roles方法中新增角色(可以新增多個),我這裡以新增兩個為例。

配置類上面新增一個註解

//開啟方法級別的認證
@EnableGlobalMethodSecurity(prePostEnabled = true)

控制器中寫兩個方法來測試一下

    @RequestMapping("commonUser")
//表示這個方法有兩個角色
@PreAuthorize(value = "hasAnyRole('admin','normal')")
public String commonUser() {
return "Hello 只用戶normal角色";
} @RequestMapping("adminUser")
//表示這個方法只擁有 admin 這個角色
@PreAuthorize(value = "hasAnyRole('admin'")
public String adminUser() {
return "Hello 使用者 admin 和 normal兩個角色";
}

@PreAuthorize:進行方法前的驗證,比如我admin這個使用者去進行登入,兩個方法都可以訪問,如果是 huangxc或者是xian這兩個使用者去訪問,只能訪問commonUser()這個方法(溫馨提示:每次測試的時候記得要輕觸快取)。