8.5 springSecurity注销和权限控制
- 继续上一节,在Security授权处设置开启注销,注意使用logoutSuccessUrl(“/”)方法
package com.zk.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 CNCLUKZK
* @create 2022/8/3-23:07
*/
//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");
//没有权限默认到登陆页,开启登陆页
http.formLogin();
http.httpBasic();
//防止网站攻击工具:get易受攻击且是csrf攻击,改为post
//新版本不用关闭
//http.csrf().disable(); //springboot默认开启csrf是正确的,会防止跨站请求攻击,
// 低版本的springboot需要关闭csrf,登陆失败存在的原因 或者设置为post方式注销
//开启注销功能,注销跳到首页
http.logout().logoutSuccessUrl("/");
}
}
退出失败问题:
- get请求导致,设置为post方式注销
- springboot默认开启csrf导致
- springboot版本问题,新版本没问题
- 然后导入security-thymeLeaf整合包,可以在thymeLeaf写security操作
<!--security-thymeLeaf整合包,thymeLeaf写security操作-->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
- 注意在index.html页面中引入的命名空间springsecurity5的命名空间是/extras/spring-security,这样才有sec命名提示
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
- 在index.html里面用thymeLeaf操作security内容来实现根据security权限动态的显示菜单
授权是给用户角色来授权
sec:authorize=“”授权,通过这个用户是否有这个角色,来判断是否通过授权
sec:authentication=“name” 认证信息
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>首页</title>
<!--semantic-ui-->
<link href="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.css" rel="stylesheet">
<link th:href="@{/css/qinstyle.css}" rel="stylesheet">
</head>
<body>
<!--主容器-->
<div class="ui container">
<div class="ui segment" id="index-header-nav" th:fragment="nav-menu">
<div class="ui secondary menu">
<a class="item" th:href="@{/index}">首页</a>
<!--登录注销-->
<div class="right menu">
<!--未登录,检测用户是否被授权,没有通过认证的用户没有有权限-->
<div sec:authorize="!isAuthenticated()">
<a class="item" th:href="@{/login}">
<i class="address card icon"></i> 登录
</a>
</div>
<!--已登录,检测用户是否被授权,通过认证的用户有权限-->
<div sec:authorize="isAuthenticated()">
<a class="item">
<i class="user icon"></i><span sec:authentication="name"></span>
<i class="address card icon"></i> <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>
</div>
</div>
<div class="ui segment" style="text-align: center">
<h3>Spring Security Study by zk</h3>
</div>
<div>
<br>
<div class="ui three column stackable grid">
<!--根据用户的角色动态的实现菜单显示-->
<!--authorize授权验证有此权限-->
<div class="column" sec:authorize="hasRole('vip1')">
<div class="ui raised segment">
<div class="ui">
<div class="content">
<h5 class="content">Level 1</h5>
<hr>
<div><a th:href="@{/level1/1}"><i class="bullhorn icon"></i> Level-1-1</a></div>
<div><a th:href="@{/level1/2}"><i class="bullhorn icon"></i> Level-1-2</a></div>
<div><a th:href="@{/level1/3}"><i class="bullhorn icon"></i> Level-1-3</a></div>
</div>
</div>
</div>
</div>
<!--authorize授权验证有此权限-->
<div class="column" sec:authorize="hasRole('vip2')">
<div class="ui raised segment">
<div class="ui">
<div class="content">
<h5 class="content">Level 2</h5>
<hr>
<div><a th:href="@{/level2/1}"><i class="bullhorn icon"></i> Level-2-1</a></div>
<div><a th:href="@{/level2/2}"><i class="bullhorn icon"></i> Level-2-2</a></div>
<div><a th:href="@{/level2/3}"><i class="bullhorn icon"></i> Level-2-3</a></div>
</div>
</div>
</div>
</div>
<!--authorize授权验证有此权限-->
<div class="column" sec:authorize="hasRole('vip3')">
<div class="ui raised segment">
<div class="ui">
<div class="content">
<h5 class="content">Level 3</h5>
<hr>
<div><a th:href="@{/level3/1}"><i class="bullhorn icon"></i> Level-3-1</a></div>
<div><a th:href="@{/level3/2}"><i class="bullhorn icon"></i> Level-3-2</a></div>
<div><a th:href="@{/level3/3}"><i class="bullhorn icon"></i> Level-3-3</a></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script th:src="@{/js/jquery-3.1.1.min.js}"></script>
<script th:src="@{/js/semantic.min.js}"></script>
</body>
</html>
- 测试效果
下一篇:SpringBoot-29-springSecurity记住我及首页定制
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/123843.html