SpringBoot 整合 H-ui.admin 管理后台项目实现SpringSecurity框架
通过整合 H-ui.admin简单体现SpringSecurity框架的作用
以及实现用户访问控制
一、什么是SpringSecurity:
- Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架 。它是用于保护基于Spring的应用程序的实际标准。
- Spring Security是一个框架,致力于为Java应用程序提供身份验证和授权。与所有Spring项目一样,Spring Security的真正强大之处在于可以轻松扩展以满足自定义要求
二、整合 H-ui.admin:
1)需要用到 H-ui.admin前端框架里的一些项目文件下载地址
2)引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
3)applicaiton.yml 中配置 thymeleaf 缓存:
spring: thymeleaf: cache:false
4)添加 login.html 登录页面、index.html 后台管理首页、welcome.html 管理系统欢迎页面
5)加入项目的静态资源包lib、static到resource资源目录的static下
6)首页添加资讯管理、产品管理、会员管理、管理员管理模块
7)为页面添加控制器实现跳转
package com.springsecuritydemo.springsecuritydemo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class BaseController { @RequestMapping({"/","/index"}) public String index(){ return "index"; } @RequestMapping({"/tologin"}) public String toLogin(){ return "login"; } @RequestMapping({"/welcome"}) public String toWelcome(){ return "welcome"; } @RequestMapping({"/article-list"}) public String toArticleList(){ return "article/article-list"; } @RequestMapping({"/product-brand"}) public String toProductBrand(){ return "product/product-brand"; } @RequestMapping({"/product-category"}) public String toProductCategory(){ return "product/product-category"; } @RequestMapping({"/product-list"}) public String toProductList(){ return "product/product-list"; } @RequestMapping({"/member-list"}) public String toMemberList(){ return "member/member-list"; } @RequestMapping({"/member-del"}) public String toMemberDel(){ return "member/member-del"; } @RequestMapping({"/admin-role"}) public String toAdminRole(){ return "admin/admin-role"; } @RequestMapping({"/admin-permission"}) public String toAdminPermission(){ return "admin/admin-permission"; } @RequestMapping({"/admin-list"}) public String toAdminList(){ return "admin/admin-list"; } }
三、在项目中添加 SpringSecurity 安全模块
1)添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2)添加 Java Configuration 配置类
package com.springsecuritydemo.springsecuritydemo; 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 { //授权 //admin页面需要admin权限 //article页面需要有article权限 @Override protected void configure(HttpSecurity http) throws Exception { //关闭springsecurity阻止Frame http.headers().frameOptions().disable(); //请求访问需要的权限 //例如:admin-role admin-permission admin-list http.authorizeRequests().antMatchers("/admin-role","/admin-permission","/admin-list").hasRole("admin") .antMatchers("/article-*").hasRole("article") .antMatchers("/product-*").hasRole("product") .antMatchers("/member-*").hasRole("member"); //登陆页面表单提交时表示用户名和密码的请求参数是:username和password //loginPage() 表示登陆页面,默认是/login //展示自定义的登陆页面,/tologin请求与/login请求关联,/login提交表单默认的用户名和密码的请求参数是:username和password,还能验证登陆失败 //阻止网络攻击的检测 http.csrf().disable(); http.formLogin().loginPage("/tologin").loginProcessingUrl("/login"); //开启注销 http.logout(); //开启记住用户 http.rememberMe(); } //认证:用户和权限绑定 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { //101用户对应的权限article //springsecyrity5.0+版本对于密码会进行不同规则的密码加密 //passwordEncoder() //BCryptPasswordEncoder()加密格式对密码进行加密 auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("101").password(new BCryptPasswordEncoder().encode("1")).roles("article").and() .withUser("102").password(new BCryptPasswordEncoder().encode("1")).roles("product").and() .withUser("103").password(new BCryptPasswordEncoder().encode("1")).roles("member").and() .withUser("104").password(new BCryptPasswordEncoder().encode("1")).roles("admin"); } }
由于tologin请求与login请求关联,所以需要将自定义的login页面中的用户名和密码的name属性改为模块默认的username以及password,表单中的action属性改为action="login"访问springsecurity中的login
附:如何实现不同用户登陆后展示的界面不同:
授权时用hasAnyRole方法,对于不是超级管理员的用户添加超级管理员权限
Html页面加入sec:authorize="hasAnyRole()"标签,该标签作用是当登录的用户是hasAnyRole标签中的任一个成员时,显示该当前内容
使用该标签需要在html页面中引入命名空间
<html xmlns:sec="http://www.w3.org/1999/xhtml">
以及依赖
<dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity5</artifactId> </dependency>
对于导航栏做一些处理,当没有用户登陆时展示请登录,当有用户登陆时展示登录用户:
一些标签的作用:
sec:authorize="hasRole('权限名称')"
权限判断sec:authorize="isAuthenticated()"
用户已经登录吗sec:authorize="!isAuthenticated()"
用户未登录sec:authentication="name"
获取用户名
实现效果:
原始页面:不管谁登录展示所有
实现业务后登录:无用户登陆时展示请登录,登录用户管理员后只展示用户管理员模块以及显示登录名
嗯!
- SpringBoot2.0 整合 SpringSecurity 框架,实现用户权限安全管理
- SpringBoot2.0 整合 SpringSecurity 框架,实现用户权限安全管理
- SpringBoot2.0 整合 SpringSecurity 框架实现用户权限安全管理方法
- springboot快速开发框架(脚手架)-iris后台管理项目
- SpringBoot2.0整合Shiro框架实现用户权限管理的示例
- springboot项目整合shiro实现权限管理
- (开源项目)基于springboot的后台管理系统基本框架
- SpringBoot基础教程及框架整合(二)----项目结构简介
- SpringBoot基础教程及框架整合(一)----IDEA创建SpringBoot项目
- SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例
- SpringBoot整合Shiro权限管理框架
- springboot 整合shiro + cas 实现单点登录权限管理(一)(sso)
- 【个人学习】使用idea搭建SpringBoot,整合Mybatis、Thymeleaf,连接数据库,实现具有前端界面项目:主要功能登录,注册,个人信息查看、更改,不定时更新中...
- springBoot admin 实现对springboot项目的监控-实战
- SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例(转)
- vue脚手架项目结合adminLTE框架做单页面应用后台管理项目
- springboot整合webservice 实现项目之间的数据交互
- 前后分离敏捷开发框架源码基于Vue+ElementUI+Springboot后台权限
- Springboot整合token的权限管理实现(一)—— 生成并发送 token
- docker安装redis连接redis可视化工具Redis Desktop Manager+springboot项目使用Cache缓存+springboot项目整合 Redis 实现缓存