SpringBoot Security安装配置及Thymeleaf整合

 更新时间:2020年12月04日 08:36:11   作者:人间有妖气  
这篇文章主要介绍了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>

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

相关文章

  • Spring Boot+maven打war包的方法

    Spring Boot+maven打war包的方法

    这篇文章主要介绍了Spring Boot+maven打war包的方法,本文通过实例代码相结合的形式给大家介绍的非常详细,需要的朋友参考下吧
    2018-05-05
  • JVM运行时数据区原理解析

    JVM运行时数据区原理解析

    这篇文章主要介绍了JVM运行时数据区原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • 浅谈Spring5 响应式编程

    浅谈Spring5 响应式编程

    本篇文章主要介绍了浅谈Spring5 响应式编程,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01
  • junit4教程junit4.5官方下载

    junit4教程junit4.5官方下载

    前提:本文假设读者已经具有使用JUnit 4以前版本的经验。
    2008-09-09
  • SpringBoot分离打Jar包的两种配置方式

    SpringBoot分离打Jar包的两种配置方式

    这篇文章主要介绍了SpringBoot分离打Jar包的两种配置方式,方式一是基于maven-jar-plugin,方式二是基于spring-boot-maven-plugin,文中结合实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-11-11
  • 如何解决springboot数据库查询时出现的时区差异问题

    如何解决springboot数据库查询时出现的时区差异问题

    这篇文章主要介绍了如何解决springboot数据库查询时出现的时区差异问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • 关于Maven混合配置私有仓库和公共仓库的问题

    关于Maven混合配置私有仓库和公共仓库的问题

    这篇文章主要介绍了Maven混合配置私有仓库和公共仓库,通过实例代码详细介绍了私有和公共仓库混合配置的方法,需要的朋友可以参考下
    2022-06-06
  • 8个简单部分开启Java语言学习之路 附java学习书单

    8个简单部分开启Java语言学习之路 附java学习书单

    8个简单部分开启Java语言学习之路,附java学习书单,这篇文章主要向大家介绍了学习java语言的方向,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • 详解SpringBoot整合RabbitMQ如何实现消息确认

    详解SpringBoot整合RabbitMQ如何实现消息确认

    这篇文章主要介绍了SpringBoot整合RabbitMQ是如何实现消息确认的,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • Java数据类型的规则

    Java数据类型的规则

    这篇文章主要介绍了Java数据类型的规则的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-12-12

最新评论