springboot 整合apache shiro

这几天因为项目需要,学习了下shiro,由此留下一些记录,也希望对初学shiro的朋友有帮助。

springboot 是这两年新兴起来的一个项目,它的出现是为了减少springmvc开发过程中需要引入各种的jar包,各种xml配置文件,它充分利用了JavaConfig的配置模式以及“约定优于配置”的理念,帮开发者配置大部分需要的东西,在github上的springboot项目里面,提供了很多列子,

而apache shiro 是一个轻量级的身份验证与授权框架,与spring security 相比较,简单易用,灵活性高,springboot本身是提供了对security的支持,毕竟是自家的东西。springboot暂时没有集成shiro,这得自己配。

网上找了一些资料,配置shiro的,有很多需要在web.xml、application.xml里面各种配置,然而springboot 并没有这些,而且springboot提倡无xml,使用javaconfig的配置方式,对这个不是很熟悉,但有人使用javaconfig的方式配置了shiro,参见这位csdn里面一位同学的博客spring boot 集成shiro的配置,下载了demo,然后模仿着成功配置了下。但习惯了xml的配置方式,感觉javaconfig的方式并不是很直观,于是自己又把它换成了xml的方式。以下是主要的配置过程

首先是spring-shiro.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- =========================================================
                   Shiro Components
        ========================================================= -->

    <!-- 缓存管理器 使用Ehcache实现 -->
    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <property name="cacheManagerConfigFile" value="classpath:app/config/ehcache-shiro.xml" />
    </bean>

    <!-- 会话Cookie模板 -->
    <bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
        <constructor-arg value="sid" />
        <property name="httpOnly" value="true" />
        <property name="maxAge" value="1800000" />  <!-- 20天 -->
    </bean>

    <!-- 会话ID生成器 -->
    <bean id="sessionIdGenerator" class="org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator" />

    <bean id="sessionDAO"
        class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">
        <property name="activeSessionsCacheName" value="shiro-activeSessionCache" />
        <property name="sessionIdGenerator" ref="sessionIdGenerator" />
    </bean> 

    <!-- 会话管理器 -->
     <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
         <property name="globalSessionTimeout" value="10800" />
        <property name="deleteInvalidSessions" value="true" />
        <property name="sessionDAO" ref="sessionDAO" />
    </bean>

    <!-- 需要自己写的realm类 充当shiro 和应用的安全数据的桥梁  -->
    <bean id="MonitorRealm" class="com.test.MonitorRealm"></bean>

    <!-- 安全管理器 -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realms">
            <list>
                <ref bean="MonitorRealm" />
            </list>
        </property>
        <property name="sessionManager" ref="sessionManager" />
        <property name="cacheManager" ref="cacheManager" />
    </bean>

    <!-- Shiro生命周期处理器 -->
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />

    <!-- Shiro的Web过滤器 -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager" />
        <property name="loginUrl" value="/cooka-user-web/login" />
        <property name="unauthorizedUrl" value="/unauthorized  " />
        <property name="filters">
            <util:map>
                <entry key="authc">
                    <bean
                        class="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter" />
                </entry>
            </util:map>
        </property>
        <property name="filterChainDefinitions">
            <value>
                # 无需认证便可以访问的的文件放在前面
                /js/* = anon
                /css/* = anon
                /img/* = anon
                /images/* = anon
                #需要认证后才能访问的url,这里要写全
                /user-web/login = anon
                /logout = logout
                /user-web/* = authc
                /backend-web/* = authc
            </value>
        </property>
    </bean>

    <!-- 异常拦截 -->
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <prop key="org.apache.shiro.authz.UnauthorizedException">
                    /unauthorized                            <!-- 未授权处理页 -->
                </prop>
                <prop key="org.apache.shiro.authz.UnauthenticatedException">
                    /user-web/login                             <!-- 身份没有验证 -->
                </prop>
            </props>
        </property>
    </bean>

</beans>  

接着是Ehcache.xml文件。

ehcache是一个纯Java的进程内缓存框架,相关介绍可以看这里

<ehcache updateCheck="false" name="shiroCache">
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="false"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            />
</ehcache>

springboot加载xml配置文件

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@SpringBootApplication
@ComponentScan
@EnableAutoConfiguration
public class Application {
	public static void main(String[] args) {
		SpringApplication.run(new String[] {
				"classpath*:app/config/spring-*.xml",
				"classpath*:app/config/spring-session-redis.xml",
				"classpath*:/user/captcha.xml"
				//....
			}, args);
	}
}

这样。spingboot以xml形式配置shiro就完成了,后面在controller的方法上面使用注解的的方式,就可以进行权限控制。

这里没有提供MonitorRealm类,里面要实现doGetAuthorizationInfo(授权)和doGetAuthenticationInfo(认证)两个方法,还有就是loginController里面要做一些改动,有需要的朋友可以参考这篇SpringMVC整合Shiro博文。

时间: 2024-08-25 12:36:21

springboot 整合apache shiro的相关文章

SpringBoot集成Apache Shiro

笔者因为项目转型的原因,对Apache Shiro安全框架做了一点研究工作,故想写点东西以便将来查阅.之所以选择Shiro也是看了很多人的推荐,号称功能丰富强大,而且易于使用.实践下来的确如大多数人所说简约优美,小巧精悍. 介绍demo项目前,简单说明一下Shiro框架的特性. 1.  Apache Shiro Features 从上图可以看出Shiro具备应用程序安全框架的四大基石":身份验证.授权.会话管理和密码. Authentication:有时被称为'登录',这是需要明确用户是谁 Au

单体物联平台系统(Springboot整合shiro实现多realm多用户表多权限表登陆)

参考实现:http://www.qchcloud.cn/tn/article/30 一.技术框架 本项目基于Spring,整合Apache Shiro框架,实现用户管理和权限控制,主要内容如下: 1.登录(带验证码),包括"记住我"的功能: 2.加密,存储的密码不采用明文: 3.session管理:使用shiro默认的session管理替代Tomcat的HttpSession: 4.shiro拦截器:对静态文件(HTML/JS/CSS等)进行权限控制,无权限则请求不到: 5.后台接口权

springboot学习笔记-5 springboot整合shiro

http://www.cnblogs.com/hlhdidi/p/6376457.html 亲自验证,该帖真实有效 shiro是一个权限框架,具体的使用可以查看其官网 http://shiro.apache.org/  它提供了很方便的权限认证和登录的功能. 而springboot作为一个开源框架,必然提供了和shiro整合的功能!接下来就用springboot结合springmvc,mybatis,整合shiro完成对于用户登录的判定和权限的验证. 1.准备数据库表结构 这里主要涉及到五张表:

springboot整合shiro应用

1.Shiro是Apache下的一个开源项目,我们称之为Apache Shiro.它是一个很易用与Java项目的的安全框架,提供了认证.授权.加密.会话管理,与spring Security 一样都是做一个权限的安全框架,但是与Spring Security 相比,在于 Shiro 使用了比较简单易懂易于使用的授权方式.shiro属于轻量级框架,相对于security简单的多,也没有security那么复杂.所以我这里也是简单介绍一下shiro的使用. 2.非常简单:其基本功能点如下图所示: A

SpringBoot系列十二:SpringBoot整合 Shiro

1.概念:SpringBoot 整合 Shiro 2.具体内容 Shiro 是现在最为流行的权限认证开发框架,与它起名的只有最初的 SpringSecurity(这个开发框架非常不好用,但是千万不要 以为 SpringSecurity 没有用处,它在 SpringCloud 阶段将发挥重大的作用).但是现在如果要想整合 Shiro 开发框架有一点很遗憾, SpringBoot 没有直接的配置支持,它不像整合所谓的 Kafka.Redis.DataSource,也就是说如果要想整合 Shiro 开

SpringBoot整合Shiro (二)

Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码学和会话管理.相比较Spring Security,shiro有小巧.简单.易上手等的优点.所以很多框架都在使用shiro. Shiro包含了三个核心组件:Subject, SecurityManager 和 Realms. Subject 代表了当前用户的安全操作. SecurityManager 则管理所有用户的安全操作.它是Shiro框架的核心,典型的Facade模式,Shiro通过SecurityMana

SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统

1.前言本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelper.Mapper插件.druid.dataTables.ztree.jQuery 开发工具:intellij idea 数据库:mysql.redis 2.表结构还是是用标准的5张表来展现权限.如下图:image 分别为用户表,角色表,资源表,用户角色表,角色资源表.在这个demo中使用了mybat

SpringMVC+Apache Shiro+JPA(hibernate)整合配置

序: 关于标题: 说是教学,实在愧不敢当,但苦与本人文笔有限,实在找不到更合理,谦逊的词语表达,只能先这样定义了. 其实最真实的想法,只是希望这个关键词能让更多的人浏览到这篇文章,也算是对于自己写文章的一个肯定吧.^_^! 关于内容: 再写这系列文章之前,本人和许多人一样都是伸手党,并深深的了解咱伸手党且英文较差的朋友对于新知识的学习及获取中文资料少的痛苦.所以本着"取之于民,共享与民"的原则,记录下实际工作中对SpringMVC+Shiro整合应用的部分心得.本人技术水平有限,仅希望

Apache shiro配置与使用(Spring整合)

网络上大部分的博文是关于Apache shiro与Spring MVC的整合,以及使用教程,最近在做一个物流项目的时候使用的是Apache shiro与Spring进行整合,期间遇到了一些问题,花费了一些时间才得到解决,所以本文首先会从介绍shiro框架到理解shiro以及使用shiro框架几个角度进行描述如何正确的使用与理解该框架: 1.权限概述(正确理解认证.授权的基本概念) 2.常见的权限控制的方式(URL拦截的方式.方法注解的方式) 3.权限涉及到的数据表以及模型关系 4.Apache