SpringSecurity学习笔记——01
1.什么是Spring Security?
spring security是针对spring项目的安全框架,也是spring boot底层安全模块默认的技术选型,可以实现强大的Web安全控制,对于安全的控制,我们仅仅需要引入spring-boot-starter-security模块,进行少量的配置就可以实现强大的安全管理。
Spring Security的主要目标是“认证”和“授权”
认证:(Authentication)
“授权”:(Authentization)
2.Spring Security使用到的类
//我们对Spring Security进行配置的时候需要继承的类
WebSecurityConfigurationAdapter
//主要用于创建用户,并分配权限
AuthenticationManagerBuilder
//启动Spring Security
@EnableWebSecurity
3.怎么用Spring Security呢?(Spring Boot中)
3.1导入依赖
<dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
3.2 自定义配置类,对Security进行自定义配置。
需要编写一个配置类,继承WebSecurityConfigurerAdapter,并加上@EnableWebSecurity注解即可
如下所示:
package com.example.demo.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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/hello").hasRole("role1");
http.formLogin();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("user1").roles("role1","role2").password(new BCryptPasswordEncoder().encode("user1"))
.and()
.withUser("user2").roles("role1").password(new BCryptPasswordEncoder().encode("user1"));
}
}
在进行配置之前,要明确几个问题,
1.当对哪些资源进行访问的时候,需要权限?
2.分别需要什么权限?
3.怎么给添加用户,怎么给用户分配权限?
首先是用户的权限的分配;
有三种方式:
1.在配置文件中指定用户的名称、密码和权限。
2.在内存中指定
3.在数据库中
注:主要学了前两种。
第一种:直接在application.yaml(properties)中添加即可;
spring.security.user.name=admin
spring.security.user.password=123456
spring.security.user.roles=role1
第二种:在我们定义的配置类中重写config方法,即可,注意重写的是参数是AuthenticationManagerBuilder的方法:如下
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("user1").roles("role1","role2").password(new BCryptPasswordEncoder().encode("user1"))
.and()
.withUser("user2").roles("role1").password(new BCryptPasswordEncoder().encode("user1"));
}
注:新版本的security会要求对密码进行加密进行加密
。
其次,设置对不同访问的所需权限的设置
同样在config方法中进行指定。
@Override
protected void configure(HttpSecurity http) throws Exception {
//每个请求需要对应的权限,首页都可以访问
//请求的规则
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/hello").hasRole("role1");
//然后进入登录页面,进行登录。
http.formLogin();
}
antMatchers:表示请求的路径。
permintAll:表示任何权限的用户均可访问,一般是首页或者登录页。
hasRole:当包含某些权限的用户才可以访问
这里采用的是链式编程的方法。首先对请求路径进行匹配,若未登录,则进行登录
然后进入登录页面,进行登录。
【http.formLogin();】
如果不写http.formLogin();则访问页面的时候,直接就会报403,访问会被拒绝