在web开发中,安全第一位!过滤器,拦截器……
功能性需求:否
做网站:安全应该在什么时候考虑?设计之初!
- 漏洞,隐私泄露
- 架构一单确定
市面上常用的安全框架:shiro、SpringSecurity:很像,除了类不一样,名字不一样;功能都一样。主要分 认证、授权两步
官网:https://spring.io/projects/spring-security
MVC—SPRING–SPRINGBOOT–框架思想
<!--pom中引入thymeleaf依赖-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
Aop:横切~配置类~
简介
Spring Security 是针对Spring项目的安全框架,也是Spring Boot底层安全模块默认的技术选型,他可以实现强大的Web安全控制,对于安全控制,我们仅需要引入spring-boot-starter-security模块,进行少量的配置,即可实现强大的安全管理!
记住几个类:
- WebSecurityConfigurerAdapter:自定义Security策略
- AuthenticationManagerBuilder:自定义认证策略
- @EnableWebSecurity:开启WebSecurity模式 ,@EnableXXX开启某个功能
Spring Security的两个主要目标是“认证”和“授权”(访问控制)。
“认证”(Authentication)
“授权”(Authorization)
这个概念是通用的,而不是只在Spring Security中存在。
参考官网:https://spring.io/projects/spring-security,查看我们自己项目中的版本,找到对应的帮助文档:https://docs.spring.io/spring-security/site/docs/5.2.0.RELEASE/reference/htmlsingle/
pom.xml引入
<!--security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
新建了配置文件config…
22
测试访问权限情况如下:
配置无权的则跳转登陆页面
package com.kuang.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 {
//alt+insert
//链式编程
@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人可以访问,功能页只有对应有权限的人才能访问
//请求授权的规则
http.authorizeRequests()
.antMatchers("/").permitAll() //首页"/" 所有人都能访问.permitAll()
.antMatchers("/level1/**").hasRole("vip1") //有vip1权限的人才能访问 level1
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3") ;
//没有权限默认会跳到登陆页面,需要开启登陆的页面
http.formLogin();
}
}
alt+insert->override
出现密码编码错误,就是密码在内存中需要加密
//2、认证 在springboot 2.1.x可以直接使用
//密码编码: PasswordEncoder
//在Spring Security 5.0+新增了很多的加密方法
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//这些数据正常从数据库中读
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("kuangshen").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
.and()
.withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3")
.and()
.withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
}
JDBC-密码认证
@Autowired
private DataSource dataSource;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
// ensure the passwords are encoded properly
UserBuilder users = User.withDefaultPasswordEncoder();
auth
.jdbcAuthentication()
.dataSource(dataSource)
.withDefaultSchema()
.withUser(users.username("user").password("password").roles("USER"))
.withUser(users.username("admin").password("password").roles("USER","ADMIN"));
}
Thymeleaf 和 SpringSecurity整合 在pom.xml中引入
<!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity4 -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
在html中引入
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<!--xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"
为了不报错报红 写代码会自动提示,不加也能继续使用
<div sec:></div> alt+enter 会自动加入这段代码 然后添加 pom.xml中thymeleaf-extras-springsecurity4
-->
go on
<!--未登录--><!--isAuthenticated() 表示用户是否已经认证过了-->
<div sec:authorize="!isAuthenticated()">
<a class="item" th:href="@{/toLogin}">
<i class="address card icon"></i> 登录
</a>
</div>
<!--如果已登陆:用户名,注销-->
<div sec:authorize="isAuthenticated()">
<a class="item" >
用户名:<span sec:authentication="name"></span>
角色:<span sec:authentication="principal.getAuthorities()"></span>
</a>
</div>
<div sec:authorize="isAuthenticated()">
<a class="item" th:href="@{/logout}">
<i class="sign-out icon"></i> 注销
</a>
</div>
发现登陆并没有识别正确的注销角色用户名
两种方式解决
1、按照狂神的解决方式为:pom.xml 中springboot改成2.0.9.RELEASE
2、这种方式是不改动springboot版本,改动security-thymeleaf整合包的版本
<!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity5 -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
同时修改首页角色引用方式:
<!--如果已登陆:用户名,注销-->
<div sec:authorize="isAuthenticated()">
<a class="item" >
用户名:<span sec:authentication="name"></span>
角色:<span sec:authentication="authorities"></span>
</a>
</div>
<!--或者角色写成这样-->
<!--角色:<span sec:authentication="principal.authorities"></span>-->
运行登陆后的效果如下
//防止网站攻击 关闭跨站请求功能 springboot默认开启中
http.csrf().disable();//关闭scrf功能,登出失败可能存在的原因
页面通过权限显示或隐藏
<!--菜单根据用户的角色动态的实现 存在vip1权限就显示-->
<div class="column" sec:authorize="hasRole('vip1')">
</div>
//开启记住我功能 cookie
//开@Override
protected void configure(HttpSecurity http) throws Exception {
//首页所有人可以访问,功能页只有对应有权限的人才能访问
//请求授权的规则
http.authorizeRequests()
.antMatchers("/").permitAll() //首页"/" 所有人都能访问.permitAll()
.antMatchers("/level1/**").hasRole("vip1") //有vip1权限的人才能访问 level1
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3") ;
//没有权限默认会跳到登陆页面,需要开启登陆的页面
// /login
//.loginPage("/toLogin")自定义登陆页面
http.formLogin();
//注销.开启了注销功能,跳转到首页
http.logout().logoutSuccessUrl("/");
//防止网站攻击 关闭跨站请求功能 springboot默认开启中
http.csrf().disable();//关闭scrf功能,登出失败可能存在的原因
//开启记住我功能 cookie
默认保存两周
http.rememberMe();
}
显示界面
登陆后显示cookie,有效期默认保存两周
重点://虽然跳转/toLogin登陆 但是认证是走的/login
//没有权限默认会跳到登陆页面,需要开启登陆的页面
// /login
//.loginPage("/toLogin")自定义登陆页面 //虽然跳转/toLogin登陆 但是认证是走的/login
http.formLogin().loginPage("/toLogin").usernameParameter("user").passwordParameter("pwd").loginProcessingUrl("/login");
login.html指定了登陆名和密码
开启自定义记住我
//开启记住我功能 cookie 默认保存两周
//自定义接受前端的参数
http.rememberMe().rememberMeParameter("remember");