1. 程式人生 > >thymeleaf使用shiro標籤 springboot2.0

thymeleaf使用shiro標籤 springboot2.0

在前臺頁面使用shiro的標籤,springboot2.0,先新增依賴

1、新增依賴

<dependency>
   <groupId>com.github.theborakompanioni</groupId>
   <artifactId>thymeleaf-extras-shiro</artifactId>
   <version>2.0.0</version>
</dependency>

2、在ShiroConfig中配置ShiroDialect

@Bean
public ShiroDialect shiroDialect() {
    return new ShiroDialect();
}

3、html中匯入shiro標籤

<!DOCTYPE html>
<html xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

引用

<shiro:principal property="username"/>

shiro:principal獲取得到的結果,是由自定義的ShiroRealm中的doGetAuthenticationInfo方法中

SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
        user
, //shiro.principal獲取得到結果 user.getPassword(), ByteSource.Util.bytes(user.getSalt()), getName() //realm name );

第一個引數決定的,此處我放入的是user實體類,shiro.principal獲取得到的結果就是User類,如果放入的是String,獲取結果則為String,後臺獲取方式(實體類)如

User user = (User)SecurityUtils.getSubject().getPrincipal()

去掉pom中的熱部署tools

<!--<dependency>-->
   <!--<groupId>org.springframework.boot</groupId>-->
   <!--<artifactId>spring-boot-devtools</artifactId>-->
   <!--<optional>true</optional>-->
<!--</dependency>-->

否則同一個類轉換也報錯