我的博客:https://blog.youkuaiyun.com/qq_43910202
Thymeleaf简介
Thymeleaf的使用是由两部分组成的:标签 + 表达式,标签是Thymeleaf的语法结构,而表达式就是语法里的内容实现。
通过标签 + 表达式,让数据和模板结合,最终转换成html代码,返回给用户。
版本问题
用 SpringBoot + Thymeleaf 搭建了一个应用,发现 Thymeleaf sec:authorize-url 以及 sec:authorize="hasRole('ROLE_ADMIN')" 标签都不生效。
这是对应的版本号,版本高的可能不支持。(如果没出现效果,一定要注意这一点)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
这是对应的依赖
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
这是html代码
<div class="layui-card-body" style="text-align: center">
<table class="layui-table layui-form">
<thead>
<tr class="text-c">
<th width="25"><input type="checkbox" name="" value="" lay-filter="checkall" lay-skin="primary"/></th>
<th width="25">序号</th>
<th width="40">文章标题</th>
<th width="40">作者</th>
<th width="40">描述</th>
<th width="40">文章内容</th>
<th width="40">发布日期</th>
<th width="40">发布人</th>
<th sec:authorize="hasAuthority('artcle/artcle/manager')" width="40">置顶</th>
<th sec:authorize="hasAuthority('artcle/artcle/manager')" width="40">热点文章</th>
<th sec:authorize="hasAuthority('artcle/artcle/manager')" width="40">状态 </th>
<th width="100">操作</th>
</tr>
</thead>
<tbody>
<tr class="text-c" th:if="${pageInfo.list.size() == 0}">
<td colspan="15"><strong>暂无数据</strong></td>
</tr>
<tr class="text-c" th:each="artcle,count:${pageInfo.list}">
<td><input type="checkbox" value="1" th:value="${artcle.id}"
name="id" lay-skin="primary"/></td>
<td th:text="${count.count}"></td>
<td th:text="${artcle.title}"></td>
<td th:text="${artcle.author}"></td>
<td th:text="${artcle.description}"></td>
<td th:utext="${artcle.content}"></td>
<td th:text="${#temporals.format(artcle.publishDate, 'yyyy-MM-dd HH:mm:ss')}"></td>
<td th:text="${artcle.publishMan}"></td>
<td sec:authorize="hasAuthority('artcle/artcle/manager')" th:text="${artcle.top}"></td>
<td sec:authorize="hasAuthority('artcle/artcle/manager')" th:text="${artcle.hot}"></td>
<td sec:authorize="hasAuthority('artcle/artcle/manager')" th:text="${artcle.states}"></td>
<td class="td-manage">
<a title="编辑" href="javascript:;"
th:onclick="'javascript:layer_show(\'编辑\',\'/artcle/'+'artcle'+'/editBefore/'+${artcle.id}+'\',900,500)'"
class="ml-5" style="text-decoration:none"><i class="layui-icon"></i></a>
<a title="删除" href="javascript:;"
th:onclick="'javascript:deleteById(\'/artcle/'+'artcle'+'/delete/'+${artcle.id}+'\')'"
class="ml-5" style="text-decoration:none"><i class="layui-icon"></i></a>
</td>
</tr>
</tbody>
</table>
</div>
这是对应的controller代码
package com.mbyte.easy.artcle.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.artcle.entity.Artcle;
import com.mbyte.easy.artcle.service.IArtcleService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.ObjectUtils;
import java.util.List;
import static com.mbyte.easy.util.Utility.getCurrentUsername;
/**
* <p>
* 前端控制器
* </p>
* @author 严天贺
* @since 2019年2月16日
*/
@Controller
@RequestMapping("/artcle/artcle")
public class ArtcleController extends BaseController {
private String prefix = "artcle/artcle/";
@Autowired
private IArtcleService artcleService;
/**
* 查询列表
*
* @param model
* @param pageNo
* @param pageSize
* @param artcle
* @return
*/
@PreAuthorize("hasAuthority('/artcle/artcle')")
@RequestMapping
public String index(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String publishDateSpace, Artcle artcle) {
SecurityContextHolder.getContext().getAuthentication().getAuthorities().iterator().forEachRemaining(e->{
System.out.println(e.getAuthority());
});
Page<Artcle> page = new Page<Artcle>(pageNo, pageSize);
QueryWrapper<Artcle> queryWrapper = new QueryWrapper<Artcle>();
if(!ObjectUtils.isEmpty(artcle.getTitle())) {
queryWrapper = queryWrapper.like("title",artcle.getTitle());
}
if(!ObjectUtils.isEmpty(artcle.getAuthor())) {
queryWrapper = queryWrapper.like("author",artcle.getAuthor());
}
if(!ObjectUtils.isEmpty(artcle.getDescription())) {
queryWrapper = queryWrapper.like("description",artcle.getDescription());
}
if(!ObjectUtils.isEmpty(artcle.getContent())) {
queryWrapper = queryWrapper.like("content",artcle.getContent());
}
if(!ObjectUtils.isEmpty(artcle.getPublishDate())) {
queryWrapper = queryWrapper.like("publishDate",artcle.getPublishDate());
}
if(!ObjectUtils.isEmpty(artcle.getPublishMan())) {
queryWrapper = queryWrapper.like("publishMan",artcle.getPublishMan());
}
if(!ObjectUtils.isEmpty(artcle.getTop())) {
queryWrapper = queryWrapper.like("top",artcle.getTop());
}
if(!ObjectUtils.isEmpty(artcle.getHot())) {
queryWrapper = queryWrapper.like("hot",artcle.getHot());
}
if(!ObjectUtils.isEmpty(artcle.getStates())) {
queryWrapper = queryWrapper.like("states",artcle.getStates());
}
String currentUsername = getCurrentUsername();
if(currentUsername.equals("admin")){
if(!ObjectUtils.isEmpty(artcle.getAuthor())) {
queryWrapper = queryWrapper.like("author",artcle.getAuthor());
}
}else{
queryWrapper = queryWrapper.like("author",currentUsername);
}
IPage<Artcle> pageInfo = artcleService.page(page, queryWrapper);
model.addAttribute("publishDateSpace", publishDateSpace);
model.addAttribute("searchInfo", artcle);
model.addAttribute("pageInfo", new PageInfo(pageInfo));
return prefix+"list";
}