资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

SpringBoot整合Shiro+Thymeleaf过程解析

这篇文章主要介绍了Spring Boot 整合 Shiro+Thymeleaf过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

成都创新互联是一家集网站建设,嘉黎企业网站建设,嘉黎品牌网站建设,网站定制,嘉黎网站建设报价,网络营销,网络优化,嘉黎网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

1.导包



  org.apache.shiro
  shiro-spring
  1.4.1




  com.github.theborakompanioni
  thymeleaf-extras-shiro
  2.0.0



  org.springframework.boot
  spring-boot-starter-thymeleaf



  org.springframework.boot
  spring-boot-configuration-processor
  true



  org.projectlombok
  lombok
  true

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 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标签的使用
   * @return
   */
  @Bean
  public ShiroDialect shiroDialect(){

    return new ShiroDialect();
  }

}

3. application.yml 配置 拦截链

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


本文题目:SpringBoot整合Shiro+Thymeleaf过程解析
网站地址:http://cdkjz.cn/article/pdsjio.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220