Spring Security(安全)
主流安全框架:shiro
、Spring Security
:相似,除了类不一样,名字不一样
功能:
- access-control:访问控制
- customizable authentication:可自定义身份验证
1.创建项目
如果你从springboot官方网站创建快速启动项目,你可能会遇到访问未响应的情况,建议使用阿里云的地址
https://start.aliyun.com/
2.导入资源
网页模板资源
模板缓存先关闭
spring.thymeleaf.cache=false
复制代码
创建controller,控制视图跳转
package com.gip.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class RouterController {
@GetMapping({"/", "/index"})
public String index() {
return "index";
}
@RequestMapping("/toLogin")
public String toLogin() {
return "views/login";
}
@RequestMapping("/level1/{id}")
public String level1(@PathVariable("id") int id) {
return "views/level1/"+id;
}
@RequestMapping("/level2/{id}")
public String level2(@PathVariable("id") int id) {
return "views/level2/" + id;
}
@RequestMapping("/level3/{id}")
public String level3(@PathVariable("id") int id) {
return "views/level3/" + id;
}
}
复制代码
使用Resultful
风格传递参数
导入Spring Security
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
复制代码
Spring Security 是 Spring 家族中的一个安全管理框架,实际上,在 Spring Boot 出现之前,Spring Security 就已经发展了多年了,但是使用的并不多,安全管理这个领域,一直是 Shiro 的天下。
相对于 Shiro,在 SSM/SSH 中整合 Spring Security 都是比较麻烦的操作,所以,Spring Security 虽然功能比 Shiro 强大,但是使用反而没有 Shiro 多(Shiro 虽然功能没有 Spring Security 多,但是对于大部分项目而言,Shiro 也够用了)。
自从有了 Spring Boot 之后,Spring Boot 对于 Spring Security 提供了 自动化配置方案,可以零配置使用 Spring Security。
因此,一般来说,常见的安全管理技术栈的组合是这样的:
- SSM + Shiro
- Spring Boot/Spring Cloud + Spring Security
注意,这只是一个推荐的组合而已,如果单纯从技术上来说,无论怎么组合,都是可以运行的。
我们来看下具体使用。
安全笼统来说就是三块,定义规则,用户认证,为用户授权
3.设置规则
新建SecurityConfig
类
package com.gip.config;
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;
//AOP:拦截器
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//使用链式编程
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
}
}
复制代码
@EnableWebSecurity
:声明是安全配置,让spring托管
这个类要继承 WebSecurityConfigurerAdapter
4.授权,认证
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//在内存中设置用户
//理论上应该从数据库中读取用户
auth.inMemoryAuthentication()
.passwordEncoder(new BCryptPasswordEncoder())
.withUser("yang")
.password(new BCryptPasswordEncoder().encode("123456"))
.roles("vip1")
.and()
.withUser("gip")
.password(new BCryptPasswordEncoder().encode("123456"))
.roles("vip2","vip3");
}
复制代码
Spring security 5.0中新增了多种加密方式,也改变了默认的密码格式.
inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
,这相当于登陆时用BCrypt加密方式对用户密码进行处理
以前的”.password(“123456″)” 变成了 .password(new BCryptPasswordEncoder().encode("123"))
,这相当于对内存中的密码进行Bcrypt编码加密.如果比对时一致,说明密码正确,才允许登陆.
重新运行项目进行测试
登录账号后,拥有权限访问