# springboot_security
**Repository Path**: mengmeng110/springboot_security
## Basic Information
- **Project Name**: springboot_security
- **Description**: springboot整合spring_springsecurity
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 0
- **Created**: 2022-01-20
- **Last Updated**: 2022-01-20
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# SpringBoot整合SpringSecurity
- 功能权限
- 访问权限
- 菜单权限
- 各种拦截器、过滤器、导致各种原生代码过多
使用AOP 将配置进去需要将依赖导入
```xml
org.springframework.boot
spring-boot-starter-security
```
导入包后需要记住如下几个类:
- WebSecurityConfigurerAdapter:自定义Security策略
- AuthenticationManagerBuilder:自定义认证策略
- @EnableWebSecurity:开启WebSecurity模式
Spring Security的两个主要目标是“认证”和“授权”(访问控制)。
**“认证”(Authentication)**
身份验证是关于验证您的凭据,如用户名/用户ID和密码,以验证您的身份。
身份验证通常通过用户名和密码完成,有时与身份验证因素结合使用。
**“授权” (Authorization)**
授权发生在系统成功验证您的身份后,最终会授予您访问资源(如信息,文件,数据库,资金,位置,几乎任何内容)的完全权限。
这个概念是通用的,而不是只在Spring Security 中存在。
**1、编写基础配置类**
```Java
@Override
protected void configure(HttpSecurity http) throws Exception {
//定制授权的请求规则
//首页所有人可以访问
http.authorizeHttpRequests().antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
}
```
通过测试发现除了首页能进,其他均不能进入。
在config()方法中加入配置,开启自动配置的登录功能
```java
//开启自动配置的登录功能
// 进入自动来到 /login 请求来到登录页面
// /login?error 重定向到这里表示请求失败
http.formLogin()
.usernameParameter("username")
.passwordParameter("password")
.loginPage("/toLogin")
//登录表单提交请求
.loginProcessingUrl("login");
```
此时进入页面后即跳转到登录的页面
2、**重写自定义认证规则**
重写configure(AuthenticationManagerBuilder auth)方法
```java
//定义认证规则
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//此处的用户名和密码可以自定义写在内存中也可以,使用数据库进行查找
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("zhangsan").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
.and()
.withUser("rootvip").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
.and()
.withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2");
}
```
登录测试即可完成登录,实现每个角色只能访问自己认证下的规则。
**权限控制和注销**
1、开启注销登录功能
在配置方法中加入配置
```java
//开启自动配置的注销功能 /logout 注销请求
//关闭关闭csrf功能:跨站请求伪造,默认只能通过post方式提交logout请求
http.csrf().disable();
http.logout().logoutSuccessUrl("/");
```
同样在前端页面加入按钮
```java
//在前端页面,增加一个注销的按钮, index.html 导航栏中
注销
```
实现注销后来到首页
2、开启记住我功能在配置方法中加入
```java
@Override
protected void configure(HttpSecurity http) throws Exception {
//记住我
http.rememberMe().rememberMeParameter("remember");
}
```
3、自定义登录功能
在登录页的配置后面指定loginpage
```java
http.formLogin().loginPage("/toLogin");
```
同样在前端页指定我们自己定义的login请求
```java
登录
```
登录时,需要将这些信息进行配置将登录的前端具体完善
```Java
```
在后端进行验证处理
```java
http.formLogin()
.usernameParameter("username")
.passwordParameter("password")
.loginPage("/toLogin")
.loginProcessingUrl("/login"); // 登陆表单提交请求
```
具体配置代码如下:
```java
package com.zzy.springboot_security.config;
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.WebSecurityConfiguration;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
/**
* @author ZhuZhengYang
* @description TODO
* @since 2022/1/19
*/
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//定制授权的请求规则
//首页所有人可以访问
http.authorizeHttpRequests().antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
//开启自动配置的登录功能
// 进入自动来到 /login 请求来到登录页面
// /login?error 重定向到这里表示请求失败
http.formLogin()
.usernameParameter("username")
.passwordParameter("password")
.loginPage("/toLogin")
//登录表单提交请求
.loginProcessingUrl("/login");
//开启自动配置的注销功能 /logout 注销请求
// http.logout();
//注销成功后,跳转到首页
//关闭csrf功能:跨站请求伪造,默认只能通过post方式提交logout请求
http.csrf().disable();
http.logout().logoutSuccessUrl("/");
}
//定义认证规则
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//此处的用户名和密码可以自定义写在内存中也可以,使用数据库进行查找
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("zhangsan").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2", "vip3")
.and()
.withUser("rootvip").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1", "vip2", "vip3")
.and()
.withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1", "vip2");
}
}
```