SpringBoot Security安装配置及Thymeleaf整合

作者:人间有妖气 时间:2023-11-27 16:18:41 

功能:解决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>

来源:https://www.cnblogs.com/songl/p/14009866.html

标签:Spring,Boot,Security,安装,配置
0
投稿

猜你喜欢

  • Java NIO和IO的区别

    2023-07-15 22:53:46
  • Android整理好的图片压缩工具类

    2023-11-06 03:24:19
  • java虚拟机原理:类加载过程详解

    2023-08-09 11:35:37
  • 5步学会使用VideoView播放视频

    2023-09-12 05:51:07
  • Java Vector和ArrayList的异同分析及实例讲解

    2023-12-03 17:27:10
  • Unity3D实现摄像机镜头移动并限制角度

    2023-09-30 17:06:51
  • Ubuntu16.04 LTS 下安装 Android Studio 2.2.2 的详细步骤

    2022-08-11 09:59:22
  • Elasticsearch学习之Terms set 查询

    2021-12-01 01:52:19
  • c# 配置文件App.config操作类库的方法

    2023-01-19 10:34:37
  • 详解C#枚举中使用Flags特性

    2023-05-19 04:44:31
  • 解决springcloud中Feign导入依赖为unknow的情况

    2022-02-03 14:05:45
  • Spring Boot多个定时任务阻塞问题的解决方法

    2023-09-20 11:43:36
  • Android入门之RelativeLayout、FrameLayout用法分析

    2021-10-16 03:21:32
  • Spring Boot小型项目如何使用异步任务管理器实现不同业务间的解耦

    2022-11-21 07:49:54
  • SpringBoot实现异步事件驱动的方法

    2023-11-01 07:48:54
  • Spring EL表示式的运用@Value说明

    2023-03-05 02:33:19
  • java上乘武功入门--反射

    2021-06-08 21:32:27
  • Java安全 ysoserial CommonsCollections3示例分析

    2022-01-19 05:08:19
  • Spring实战之Bean定义中的SpEL表达式语言支持操作示例

    2021-07-07 10:46:19
  • C#归并排序的实现方法(递归,非递归,自然归并)

    2023-10-03 01:00:36
  • asp之家 软件编程 m.aspxhome.com