Spring Boot 整合 Shiro+Thymeleaf

1.导包

<!-- 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");
            }
        }

//        filterMap.put("/login.html","anon");
//        filterMap.put("/index.html","anon");
//        filterMap.put("/doLogin","anon");
//        filterMap.put("/**","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;
    }

}

3. application.yml 配置 拦截链

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

原文地址:https://www.cnblogs.com/lcsin/p/11699918.html

时间: 2024-08-29 23:57:19

Spring Boot 整合 Shiro+Thymeleaf的相关文章

Spring Boot 整合 Shiro ,两种方式全总结!

在 Spring Boot 中做权限管理,一般来说,主流的方案是 Spring Security ,但是,仅仅从技术角度来说,也可以使用 Shiro. 今天松哥就来和大家聊聊 Spring Boot 整合 Shiro 的话题! 一般来说,Spring Security 和 Shiro 的比较如下: Spring Security 是一个重量级的安全管理框架:Shiro 则是一个轻量级的安全管理框架 Spring Security 概念复杂,配置繁琐:Shiro 概念简单.配置简单 Spring

学记:spring boot整合shiro出现UnavailableSecurityManagerException

spring boot自带spring security,spring security自然不用说是一个强大的安全框架,但是用惯了shiro,一时半会用不来spring security,所以要在spring boot中自己整合shiro.说到整合shiro,网上也是有不少教程的,但是网上的教程也不是一定是对的,可能有版本等各种问题,所以说还是要自己来动手做一遍. 在我动手整合的时候出现UnavailableSecurityManagerException的错误: 2016-12-24 10:5

上手spring boot项目(二)之spring boot整合shiro安全框架

题记:在学习了springboot和thymeleaf之后,想完成一个项目练练手,于是使用springboot+mybatis和thymeleaf完成一个博客系统,在完成的过程中出现的一些问题,将这些问题记录下来,作为自己的学习心得.在这先感谢群主TyCoding的Tumo项目,虽然本人实在太菜了,好些地方看不懂,但还是使我受益匪浅. shiro作为一个小巧灵活的安全框架,在认证和授权方面简约但又不简单,十分容易上手使用.下面是整合shiro的具体流程. 1.添加依赖 1 <!--shiro和s

Spring Boot 整合 Shiro

一.简介 Shiro是Apache下一个开源的安全框架,提供了认证.授权.加密.会话管理,与 Spring Security 相比,Shiro 是一个轻量级框架,使用了比较简单易懂易于使用的授权方式. 1.Shiro特性 Authentication:身份认证/登录,验证用户是不是拥有相应的身份: Authorization:授权,即权限验证,验证某个已认证的用户是否拥有某个权限: Session Manager:会话管理,用户登录后就是一次会话,在没有退出之前,它的所有信息都在会话中: Cry

spring boot整合shiro

安全框架Shiro和Spring Security比较,本文主要围绕Shiro进行学习 一 Shiro 是一个强大而灵活的开源安全框架,能够清晰的处理认证 授权 管理会话以及,密码加密 01 .认证与授权相关概念 安全实体:  系统需要保护的具体对象数据 权限: 系统相关的功能操作,例如基本的CRUD Authentication:身份认证授权/登录,验证用户是否拥有相应的身份 Authorization:  授权,即权限的认证,认证某个已认证的用户是否拥有某个权限 Session  Manag

Spring boot整合shiro权限管理

apache shiro: https://shiro.apache.org/ Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码学和会话管理.使用Shiro的易于理解的API,您可以快速.轻松地获得任何应用程序,从最小的移动应用程序到最大的网络和企业应用程序. shiro的三个核心组件: Subject, SecurityManager 和 Realms. Subject:即"当前操作用户".但是,在Shiro中,Subject这一概念并不仅仅指人,

Spring Boot 整合模板引擎 Freemaker、thymeleaf

1.常用的 Spring Boot 2.x 模板引擎和官方推荐案例 1)JSP(后端渲染,消耗性能) Java Server Pages 动态网页技术,由应用服务器中的 JSP 引擎来编译和执行,再将生成的整个页面返回给客户端.优点是:可以写java代码.支持表达式语言(el.jstl).内建函数. 但 JSP 本质上是 Servlet,它非常占用 JVM 内存.Java Web 官方推荐,但 Spring Boot 不推荐https://docs.spring.io/spring-boot/d

spring boot 整合spring Data JPA+Spring Security+Thymeleaf框架(上)

最近上班太忙所以耽搁了给大家分享实战springboot 框架的使用. 下面是spring boot 整合多个框架的使用. 首先是准备工作要做好. 第一  导入框架所需的包,我们用的事maven 进行对包的管理. 以上的举例是本人的H5DS的真实的后台管理项目,这个项目正在盛情融资中,各位多多捧点人场.关注一下软件发展的动态,说不定以后就是您的生活不可或缺的软件哟. 点击打开链接.闲话少说.现在切入正题. 第二,写点配置文件 第三,spring data -设计一个简单的po关系,这里需要下载一

Spring boot整合jsp

这几天在集中学习Spring boot+Shiro框架,因为之前view层用jsp比较多,所以想在spring boot中配置jsp,但是spring boot官方不推荐使用jsp,因为jsp相对于一些模板引擎,性能都比较低,官方推荐使用thymeleaf,但是Spring boot整合jsp的过程已经完成,在这里记录一下. 这篇博文是在LZ上篇文章spring boot+mybatis整合基础上写的,开发工具仍然是Intellij idea.这篇文章的重点是Intellij idea的设置,否