springboot + spring security + thymeleaf

下面是两个小小的demo,关于不同的springboot版本来实现权限的控制。

1.使用的版本springboot 2.1.3.RELEASE版本

在项目的初始化,我们需要导入的依赖是这么几个
1.thymeleaf
2.web
3.security

那么导入这么几个依赖后,面对是springboot 2.1.3.release版本,我们还需要导入最关键的一个的项目依赖

	<!-- https://mvnrepository.com/artifact/org.thymeleaf.extras/thymeleaf-extras-springsecurity5 -->
	<dependency>
		<groupId>org.thymeleaf.extras</groupId>
		<artifactId>thymeleaf-extras-springsecurity5</artifactId>
		<version>3.0.4.RELEASE</version>
	</dependency>

另外HTML上要使用一些权限的管理如

  • sec:authorize="!isAuthenticated()"
  • sec:authorize=“hasRole()”

最好在html标签上使用对应的注释如:xmlns:sec=“http://www.thymeleaf.org/thymeleaf-extras-springsecurity5

<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org"
	  xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Insert title here</title>
</head>
<body>
<h1 align="center">欢迎光临武林秘籍管理系统</h1>
<div sec:authorize="!isAuthenticated()">
	<h2 align="center">游客您好,如果想查看武林秘籍 <a th:href="@{/userlogin}">请登录</a></h2>
</div>
<div sec:authorize="isAuthenticated()">
	<h2>
		<span sec:authentication="name">,您好,你的角色有</span>
		<span sec:authentication="principal.authorities"></span>
	</h2>
	<form th:action="@{/logout}" method="post">
		<input type="submit" value="注销"/>
	</form>
</div>

<hr>

<div sec:authorize="hasRole('VIP1')">
	<h3>普通武功秘籍</h3>
	<ul>
		<li><a th:href="@{/level1/1}">罗汉拳</a></li>
		<li><a th:href="@{/level1/2}">武当长拳</a></li>
		<li><a th:href="@{/level1/3}">全真剑法</a></li>
	</ul>

</div>
		

详细的项目请看我的github

2.下面是springboot1.5.3RELEASE版本的

在项目的初始化,我们需要导入的依赖是这么几个
1.thymeleaf
2.web
3.security

因为我们导入的低版本的springboot,导致我们需要修改一下,springboot初始的thymeleaf版本号

	<properties>
		<java.version>1.8</java.version>
		<thymeleaf.version>3.0.8.RELEASE</thymeleaf.version>
		<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
		<thymeleaf-extras-springsecurity4.version>3.0.2.RELEASE</thymeleaf-extras-springsecurity4.version>
	</properties>

并且导入一个依赖包

	<dependency>
			<groupId>org.thymeleaf.extras</groupId>
			<artifactId>thymeleaf-extras-springsecurity4</artifactId>
			<!--<version>3.0.2.RELEASE</version>可以忽略,因为上面的properties上已经指定了版本-->
		</dependency>

html标签上 xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"这个来进行权限的一些相关设定。

详细项目查看我的github

遇到的问题,在springsecurity5.0以上,增加了密码加密手段,需要你进行如下的配置

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        //注意spring security 5.0版本后面新增了多种加密方式,改变了密码的格式,需要你使用passwordEncoder来进行加密
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("zhangsan").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP1","VIP2")
                .and()
                .withUser("lisi").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP2","VIP3")
                .and()
                .withUser("wangwu").password(new BCryptPasswordEncoder().encode("123456")).roles("VIP1","VIP3");
    }

关于需要导入的依赖包问题

思路是这样的

  1. 先进入springboot 的parent依赖包,来进行查看需要完成的对应操作,需要导入的依赖包
    在这里插入图片描述
  2. 看到我们需要导入大概依赖,那么接下来我们进行查询,额,这个查询的话,我也不是很会,但是我们能看到在
    spring-boot-starter-thymeleafz这个依赖包下具有,下面的包导入
    在这里插入图片描述
    大概的思路是这样的,具体的我们可以查看包导入的情况在这里插入图片描述
    感觉还差哪些依赖包,那么我们在来进行导入,所以以上就是
    thymeleaf-extras-springsecurity4或者thymeleaf-extras-springsecurity5的由来

另外本人在查看了springboot的parent依赖包里面的内容时候,发现了

在这里插入图片描述
我在里面找到了基本所有的包,比如上面你导入的thymeleaf-extras-springsecurity4,或者其他之类依赖包。

  1. 经过我查阅,为什么我们在导入需要依赖的时候,基本是不需要写上版本号的,因为在springboot的parent上已经有了相关的依赖包对应的版本号
  2. 使用的dependencyManagement表示下面的包不进行导入,只是进行了版本的控制。也就是可能你在导入依赖的时候,不用写版本号,使用springboot内部统一的版本,方便开发。

上面的理解来自这位大哥

完美

猜你喜欢

转载自blog.csdn.net/qq_41967899/article/details/89024942