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>

在这里插入图片描述
对于导航栏做一些处理,当没有用户登陆时展示请登录,当有用户登陆时展示登录用户:

一些标签的作用:

  1. sec:authorize="hasRole('权限名称')" 权限判断
  2. sec:authorize="isAuthenticated()" 用户已经登录吗
  3. sec:authorize="!isAuthenticated()" 用户未登录
  4. sec:authentication="name" 获取用户名

在这里插入图片描述
实现效果:
原始页面:不管谁登录展示所有
在这里插入图片描述
实现业务后登录:无用户登陆时展示请登录,登录用户管理员后只展示用户管理员模块以及显示登录名
在这里插入图片描述
在这里插入图片描述
嗯!

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
H-ui.admin是用H-ui前端框架开发的轻量级网站后台模版 采用源生html语言,完全免费,简单灵活,兼容性好 让您快速搭建中小型网站后台 程序员的的福音 \根目录 │ _blank.html 空白页(每次我们都拿空白页去创建,这样比较干净!) │ _footer.html 页脚公共代码片段 │ _header.html 头部公共代码片段 │ _meta.html meta公共代码片段 │ robots.txt 搜索引擎爬虫配置文件 │ login.html 管理员登陆 │ index.html 首页(主框架) │ welcome.html 我的桌面(默认永远打开的页面) │ member-开头的 用户相关 │ artice-开头的 资讯相关 │ picture-开头的 图片相关 │ product-开头的 产品相关 │ page-开头的 页面相关 │ system-开头的 系统相关 │ admin-开头的 管理员相关 │ charts-开头的 统计相关 …… ├─css │ H-ui.reset.css H-ui.reset css │ H-ui.css h-ui CSS │ H-ui.min.css h-ui CSS 压缩版 │ H-ui.login.css H-ui.admin后台登录样式 │ H-ui.admin.css H-ui.admin样式 │ style.css 写你自己的样式 │ ├─images UI相关的图片素材 │ ├─js │ H-ui.js H-ui核心脚本 │ H-ui.admin.js 本站相关的js ├─lib │ jquery jQuery类库(v1.9.1) │ bootstrapSwitch 开关控件 │ Hui-iconfont_v1.0 阿里图标字体库(H-ui定制) │ font-awesome 字体库文件 │ icheck 单选框、复选框控件 │ laypage laypage 翻页插件 │ layer layer弹出层插件 │ laytpl JavaScript模板引擎 │ My97DatePicker 日期插件 │ Validform 表单验证插件 │ zepto zepto库 │ ueditor 百度编辑器 │ Highcharts 图表插件 │ dataTables 表格排序,检索插件 │ WebUploader 百度文件上传组件 │ lightbox2 图片预览组件 │ │ html5.js html5插件,让低版本IE支持html5元素 │ DD_belatedPNG_0.0.8a-min.js 解决IE6png透明 │ swfobject.js Flash插件 │ expressInstall.swf 检查flash插件 │ unslider.min.js Unslider图片滚动效果插件 │ stickUp.min.js 让页面元素"固定"位置 │ respond.min.js 让IE兼容media │ Echo.js 图片延迟加载插件 │ colpick.js 颜色插件 │ handlebars.js js模版引擎 │ waterfall.min.js 瀑布流插件 └─temp 测试数据、图片

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值