shiro 同时实现url和按钮的拦截_Springboot + shiro 整合之Url拦截设置(转)

shiro 整合到springboot 还是比较简单的,只需要新建一个spring-shiro.xml的配置文件:

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"

default-lazy-init="true">

Shiro Configuration

/plugins/** = anon

/images/** = anon

/css/**                     = anon

/**                 = authc

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"

default-lazy-init="true">

<description>Shiro Configuration</description>

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">

<property name="realm" ref="ShiroRealm" />

</bean>

<!-- 項目自定义的Realm -->

<bean id="ShiroRealm" class="org.spring.springboot.interceptor.shiro.ShiroRealm" ></bean>

<!-- Shiro Filter -->

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">

<property name="securityManager" ref="securityManager" />

<property name="loginUrl" value="/login" />

<property name="successUrl" value="/backstage/index" />

<property name="unauthorizedUrl" value="/login" />

<!-- anon:匿名拦截器,即不需要登录即可访问;一般用于静态资源过滤

authc:如果没有登录会跳到相应的登录页面登录

user:用户拦截器,用户已经身份验证/记住我登录的都可 -->

<property name="filterChainDefinitions">

<value>

/plugins/** = anon

/images/** = anon

/css/** = anon

/**= authc

</value>

</property>

</bean>

<!-- 缓存管理器 使用Ehcache实现-->

<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">

<property name="cacheManagerConfigFile" value="classpath:ehcache.xml"/>

</bean>

<!-- AOP式方法级权限检查 -->

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">

<property name="proxyTargetClass" value="true" />

</bean>

<!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->

<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />

自定义的身份验证就不多说了,主要还是关注本文重点,解释一下filterChainDefinitions:

1、springboot 默认访问路径 resources 下的 static(好像最高级别),它就这么定的爱咋咋地。。,所以静态资源文件(js,css,jpg等)的访问配置要去掉 /static  (/static/js/**=anon)

2、/** 必须要放在最下面 ,注意是必须

我个人觉得springboot 的WebMvcConfigurerAdapter bean注解方式不如以前xml方便,只需要导入一下这个xml 文件,并将这个文件放到classpath 下就OK了。

@Configuration   //标注此文件为一个配置项,spring boot才会扫描到该配置。

@ImportResource(locations={"classpath:spring-shiro.xml"})

public class BootConfiguration extends WebMvcConfigurerAdapter {

}

上面这个类可以随便放src下的任意目录,springboot 它会找到的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值