您的位置:首页 > 编程语言 > Java开发

Springboot + shiro 整合之Url拦截设置

2017-06-15 10:45 591 查看
 shiro 整合到springboot 还是比较简单的,只需要新建一个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"
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" />
</beans>

自定义的身份验证就不多说了,主要还是关注本文重点,解释一下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 它会找到的。

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息