💗wei_shuo的个人主页
💫wei_shuo的学习社区
🌐Hello World !
Security
Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架;提供一组可以在Spring应用上下文中配置的Bean,充分利用Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作;
认证和授权
• 依赖导入
<!--security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
• 认证和授权config/SecurityConfig.java
thymeleafpackage com.wei.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.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("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
//没有权限默认跳到登录页面
http.formLogin();
}
//认证
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("weishuo").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
.and()
.withUser("root").password(new BCryptPasswordthymeleafEncoder().encode("123456")).roles("vip1","vip2","vip3")
.and()
.withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
}
}
注销和权限控制
• Config/SecurityConfig.java
//开启注销功能,注销成功跳转到首页
http.csrf().disable(); //关闭csrf功能
http.logout().logoutSuccessUrl("/");
security-thymeleaf整合
• 导入依赖
<!--security-thymeleaf整合包-->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
• index.html:命名空间导入
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
• index.html:配置登录、未登录显示,隐藏
<!--登录、注销-->
<div class="right menu">
<!--如果未登录-->
<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.authorities"></span>
</a>
</div>
<div sec:authorize="isAuthenticated()">
<a class="item" th:href="@{/logout}">
<i class="sign-out icon"></i>注销
</a>
</div>
</div>
• index.html:配置角色页面动态显示
<div class="column" sec:authorize="hasRole('vip1')">……</div>
<div class="column" sec:authorize="hasRole('vip2')">……</div>
<div class="column" sec:authorize="hasRole('vip3')">……</div>
Remember me
• Config/SecurityConfig.java
//记住我功能,自定义接受参数
http.rememberMe().rememberMeParameter("remember");
首页定制
没有权限默认跳到登录页面,登录页定制
•
.loginPage("/toLogin"):将用户重定向到指定的登录页面进行登录;
•
.usernameParameter("username"):从用户提交的表单中获取用户名时使用的参数名;
•
.passwordParameter("password"):从用户提交的表单中获取密码时使用的参数名;
•
.loginProcessingUrl("/login"):指定表单登录提交的 URL,用于处理用户提交的登录表单数据
//没有权限默认跳到登录页面,登录页定制
http.formLogin().loginPage("/toLogin").usernameParameter("username").passwordParameter("password")
.loginProcessingUrl("/login");
🌼 结语:创作不易,如果觉得博主的文章赏心悦目,还请——点赞
👍收藏
⭐️评论
📝