SpringBoot Security安装配置及Thymeleaf整合

功能:解决web站点的登录,权限验证,授权等功能

优点:在不影响站点业务代码,可以权限的授权与验证横切到业务中

1、要添加的依赖

<!--thymeleaf-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <!--security 和 thymeleaf 整合包-->
    <dependency>
      <groupId>org.thymeleaf.extras</groupId>
      <artifactId>thymeleaf-extras-springsecurity5</artifactId>
    </dependency>
    <!--web-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--security-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

2、Security 下授权与验证的简单配置(Security下有登录,注销,记住我等功能,可以快速集成到自己的login页上)
Tis:如果template页中使用了 Frame页,默认是不能访问的,需要添加 http.headers().frameOptions().sameOrigin();

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
  //授权
  @Override
  protected void configure(HttpSecurity http) throws Exception {

    //请求授权的规则
    http.authorizeRequests()
        //.antMatchers("/tologin").permitAll() //登录页所有人都可以访问
        //.antMatchers("/admin/**").hasRole("admin1")
        .antMatchers("/admin/list").hasRole("admin1")
        .antMatchers("/admin/role").hasRole("admin1")
        .antMatchers("/admin/cate").hasRole("admin2")
        .antMatchers("/admin/rule").hasRole("admin2");

    // 项目里面使用了springSecurity spring Security下,X-Frame-Options默认为DENY,非spring Security环境下,X-Frame-Options的默认大多也是DENY,这种情况下,浏览器拒绝当前页面加载任何Frame页面
    http.headers().frameOptions().sameOrigin();

    //登录页(Security默认有一个登录页)
    http.formLogin().permitAll()
        .loginPage("/tologin") //指定自定义的登录页地址
        .successForwardUrl("/admin/index") //登录成功跳转地址
        .usernameParameter("username").passwordParameter("password");//匹配自定义登录页的name元素名称

    // 开启注销功能,跳转到登录页
    http.csrf().disable(); //退出失败可能能的原因
    http.logout().logoutSuccessUrl("/tologin");

    //开启记住我功能,cookie 默认保存14天
    http.rememberMe()
        .rememberMeParameter("remember");//匹配自定义登录页的name元素名称

  }

  //认证
  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
        .passwordEncoder(new BCryptPasswordEncoder())//密码加密方式(有些版本的Security必须要指定)
        .withUser("root").password(new BCryptPasswordEncoder().encode("123")).roles("admin1","admin2","admin3")
        .and()
        .withUser("yeqiu").password(new BCryptPasswordEncoder().encode("123")).roles("admin1")
        .and()
        .withUser("admin").password(new BCryptPasswordEncoder().encode("123")).roles("admin2");

  }
}

3、Security 和 Thymeleaf 页面整合(添加依赖:thymeleaf-extras-springsecurity)

<!--加入约束-->
<html class="x-admin-sm" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">

<!--
  sec:authorize="isAuthenticated()" 用户是否登录
  sec:authorize="hasAnyRole('admin1')" 是否具有某个角色
  sec:authentication="name" 当前登录用户
  sec:authentication="principal.authorities" 当前用户全部角色
-->

<div sec:authorize="isAuthenticated()">
  <h2><span sec:authentication="name"></span>,您好 您的身份是
    <span sec:authentication="principal.authorities"></span>
  </h2>
</div>

<li sec:authorize="hasRole('admin2')">
   <a onclick="xadmin.add_tab('权限管理','admin/rule')">
      <cite>权限管理</cite>
   </a>
</li>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • SpringBoot使用Spring Security实现登录注销功能

    1.首先看下我的项目结构 我们逐个讲解 /** * 用户登录配置类 * @author Administrator * */ public class AdminUserDateils implements UserDetails { private static final long serialVersionUID = -1546619839676530441L; private transient YCAdmin yCAdmin; public AdminUserDateils() { }

  • 详解Springboot2.3集成Spring security 框架(原生集成)

    0.pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0

  • SpringBoot整合Spring Security的详细教程

    好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/RobodLee/DayDayUP,欢迎Star,更多文章请前往:目录导航 前言 Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架.提供了完善的认证机制和方法级的授权功能.是一款非常优秀的权限管理框架.它的核心是一组过滤器链,不同的功能经由不同的过滤器.这篇文章就是想通过一个小案例将Spring Security整合到SpringBoot中去.要实现的功能就是在认证服务器上

  • SpringBoot安全认证Security的实现方法

    一.基本环境搭建 父pom依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> </parent> 1. 添加pom依赖: <dependency> <groupId&

  • Springboot+SpringSecurity+JWT实现用户登录和权限认证示例

    如今,互联网项目对于安全的要求越来越严格,这就是对后端开发提出了更多的要求,目前比较成熟的几种大家比较熟悉的模式,像RBAC 基于角色权限的验证,shiro框架专门用于处理权限方面的,另一个比较流行的后端框架是Spring-Security,该框架提供了一整套比较成熟,也很完整的机制用于处理各类场景下的可以基于权限,资源路径,以及授权方面的解决方案,部分模块支持定制化,而且在和oauth2.0进行了很好的无缝连接,在移动互联网的授权认证方面有很强的优势,具体的使用大家可以结合自己的业务场景进行选

  • SpringBoot集成Spring Security用JWT令牌实现登录和鉴权的方法

    最近在做项目的过程中 需要用JWT做登录和鉴权 查了很多资料 都不甚详细 有的是需要在application.yml里进行jwt的配置 但我在导包后并没有相应的配置项 因而并不适用 在踩过很多坑之后 稍微整理了一下 做个笔记 一.概念 1.什么是JWT Json Web Token (JWT)是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准(RFC 7519) 该token被设计为紧凑且安全的 特别适用于分布式站点的单点登录(SSO)场景 随着JWT的出现 使得校验方式更加简单便

  • SpringBoot 配合 SpringSecurity 实现自动登录功能的代码

    自动登录是我们在软件开发时一个非常常见的功能,例如我们登录 QQ 邮箱: 很多网站我们在登录的时候都会看到类似的选项,毕竟总让用户输入用户名密码是一件很麻烦的事. 自动登录功能就是,用户在登录成功后,在某一段时间内,如果用户关闭了浏览器并重新打开,或者服务器重启了,都不需要用户重新登录了,用户依然可以直接访问接口数据 作为一个常见的功能,我们的 Spring Security 肯定也提供了相应的支持,本文我们就来看下 Spring Security 中如何实现这个功能. 一.加入 remembe

  • SpringBoot基于SpringSecurity表单登录和权限验证的示例

    一.简介 上篇介绍了一个自己做的管理系统,最近空闲的时间自己在继续做,把之前登录时候自定义的拦截器过滤器换成了基于SpringSecurity来做,其中遇到了很多坑,总结下,大家有遇到类似问题的话就当是为大家闭坑吧. 二.项目实现功能和成果展示 首先来看下登录界面:这是我输入的一个正确的信息,点击登录后SpringSecurity会根据你输入的用户名和密码去验证是否正确,如果正确的话就去你定义的页面,我这里定义的是查询教师信息页面.来看下代码吧. 三.准备工作(前台页面.实体类) 实体类Teac

  • SpringBoot Security前后端分离登录验证的实现

    最近一段时间在研究OAuth2的使用,想整个单点登录,从网上找了很多demo都没有实施成功,也许是因为压根就不懂OAuth2的原理导致.有那么几天,越来越没有头绪,又不能放弃,转过头来一想,OAuth2是在Security的基础上扩展的,对于Security自己都是一无所知,干脆就先研究一下Security吧,先把Security搭建起来,找找感觉. 说干就干,在现成的SpringBoot 2.1.4.RELEASE环境下,进行Security的使用. 简单的Security的使用就不说了,目前

  • SpringBoot+SpringSecurity 不拦截静态资源的实现

    一.问题描述 在 SpringBoot 中加入 SpringSecurity 中之后,静态资源总是被过滤,导致界面很难看: 目录结构: 二.问题解决 正常不拦截资源,我查阅资料,基本都是重新 config 方法即可: package org.yolo.securitylogin.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conf

随机推荐