欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot 整合 Shiro+Thymeleaf過程解析

 更新時(shí)間:2019年10月19日 10:12:40   作者:lcsin  
這篇文章主要介紹了Spring Boot 整合 Shiro+Thymeleaf過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了Spring Boot 整合 Shiro+Thymeleaf過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

1.導(dǎo)包

<!-- springboot 與 shiro 的集成-->
<dependency>
  <groupId>org.apache.shiro</groupId>
  <artifactId>shiro-spring</artifactId>
  <version>1.4.1</version>
</dependency>

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

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-configuration-processor</artifactId>
  <optional>true</optional>
</dependency>

<dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <optional>true</optional>
</dependency>

2. 編寫配置類

@Configuration
@ConfigurationProperties(prefix = "shiro")
@Data
public class ShiroConfig {

  private String loginUrl;
  private String unauthorizedUrl;
  private String successUrl;
  private String logoutUrl;

  private String[] anons;
  private String[] authcs;

  /**
   * 配置securityManager
   * @param userRealm
   * @return
   */
  @Bean
  public SecurityManager securityManager(UserRealm userRealm){
    DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();

    securityManager.setRealm(userRealm);

    return securityManager;
  }

  /**
   * 配置shiroFilter
   * @param securityManager
   * @return
   */
  @Bean
  public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){
    ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

    shiroFilterFactoryBean.setSecurityManager(securityManager);
    shiroFilterFactoryBean.setLoginUrl(loginUrl);
    shiroFilterFactoryBean.setUnauthorizedUrl(unauthorizedUrl);
    shiroFilterFactoryBean.setSuccessUrl(successUrl);

    Map<String,String> filterMap = new HashMap<>();

    if(null != logoutUrl){
      filterMap.put(loginUrl,"logout");
    }
    if(anons!=null && anons.length>0){
      for(String anon:anons){
        filterMap.put(anon,"anon");
      }
    }
    if(authcs!=null && authcs.length>0){
      for(String authc:authcs){
        filterMap.put(authc,"authc");
      }
    }

    shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
    return shiroFilterFactoryBean;
  }

  /**
   * 配置自定義Realm
   * @return
   */
  @Bean
  public UserRealm userRealm(CredentialsMatcher credentialsMatcher){
    UserRealm userRealm = new UserRealm();

    userRealm.setCredentialsMatcher(credentialsMatcher);

    return userRealm;
  }

  /**
   * 配置憑證匹配器
   * @return
   */
  @Bean
  public HashedCredentialsMatcher hashedCredentialsMatcher(){
    HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();

    hashedCredentialsMatcher.setHashAlgorithmName("MD5");
    hashedCredentialsMatcher.setHashIterations(10);

    return hashedCredentialsMatcher;
  }

  /**
   * 配置ShiroDialect,用于Thymeleaf和shiro標(biāo)簽的使用
   * @return
   */
  @Bean
  public ShiroDialect shiroDialect(){

    return new ShiroDialect();
  }

}

3. application.yml 配置 攔截鏈

# shiro
shiro:
 login-url: /login.html
 anons:
  - /login.html
  - /index.html
  - doLogin
 authcs:
  - /**

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • idea創(chuàng)建Spring項(xiàng)目的方法步驟(圖文)

    idea創(chuàng)建Spring項(xiàng)目的方法步驟(圖文)

    這篇文章主要介紹了idea創(chuàng)建Spring項(xiàng)目的方法步驟(圖文),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01
  • SpringBoot實(shí)現(xiàn)多環(huán)境配置文件切換教程詳解

    SpringBoot實(shí)現(xiàn)多環(huán)境配置文件切換教程詳解

    很多時(shí)候,我們項(xiàng)目在開發(fā)環(huán)境和生成環(huán)境的環(huán)境配置是不一樣的,例如,數(shù)據(jù)庫配置,這個(gè)時(shí)候就需要切換環(huán)境配置文件。本文將詳細(xì)講解SpringBoot如何切換配置文件,需要的可以參考一下
    2022-03-03
  • 詳解Java如何實(shí)現(xiàn)百萬數(shù)據(jù)excel導(dǎo)出功能

    詳解Java如何實(shí)現(xiàn)百萬數(shù)據(jù)excel導(dǎo)出功能

    這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)百萬數(shù)據(jù)excel導(dǎo)出功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下
    2023-02-02
  • Java三種獲取redis的連接及redis_String類型演示(適合新手)

    Java三種獲取redis的連接及redis_String類型演示(適合新手)

    這篇文章主要介紹了Java三種獲取redis的連接及redis_String類型演示(適合新手),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • spring?boot集成redisson的最佳實(shí)踐示例

    spring?boot集成redisson的最佳實(shí)踐示例

    這篇文章主要為大家介紹了spring?boot集成redisson的最佳實(shí)踐示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03
  • IntelliJ IDEA 安裝目錄的核心文件的功能及用法

    IntelliJ IDEA 安裝目錄的核心文件的功能及用法

    這篇文章我們主要講解一下 IntelliJ IDEA 安裝目錄中的一些核心文件的功能及用法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-10-10
  • Java解決刪除字符使頻率相同問題

    Java解決刪除字符使頻率相同問題

    給你一個(gè)下標(biāo)從0開始的字符串 word ,字符串只包含小寫英文字母,你需要選擇一個(gè)下標(biāo)并刪除下標(biāo)處的字符,使得word中剩余每個(gè)字母出現(xiàn)頻率相同,本文給大家介紹了Java解決刪除字符使頻率相同問題,需要的朋友可以參考下
    2024-02-02
  • springboot更新配置Swagger3的一些小技巧

    springboot更新配置Swagger3的一些小技巧

    今天給大家分享springboot更新配置Swagger3的方法,大家需要注意Swagger3版本需要引入依賴,具體示例代碼參考下本文
    2021-07-07
  • 使用JSON.toJSONString()返回{}的原因

    使用JSON.toJSONString()返回{}的原因

    這篇文章主要介紹了使用JSON.toJSONString()返回{}的原因,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • java ZXing生成二維碼及條碼實(shí)例分享

    java ZXing生成二維碼及條碼實(shí)例分享

    本文分享了java ZXing生成二維碼及條碼的實(shí)例代碼,具有很好的參考價(jià)值,需要的朋友一起來看下吧
    2016-12-12

最新評(píng)論