之前一直有听说shiro是个很不错的安全框架,最近项目正好用到认证授权这一块,就尝试着自己学了一下,现在把如何去整合的步骤分享给大家,也好让自己做个笔记。
首先我是使用maven开发的项目,既然使用shiro框架首先肯定是要加入相关的支持的,我们要导入shiro相关的jar包
shiro分为好几个jar,在这里我导入一个包括全部的方便以后使用:使用的是1.2.4版本的支持。
<!-- shiro -->
<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-all -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-all</artifactId>
<version>1.2.4</version>
</dependency>
加入之后maven会自动下载到仓库中
第二个需要配置项目的web.xml配置由shiro提供的过滤器
<!-- 配置由Spring提供的过滤器,用于整合shiro框架 -->
<!-- 在项目启动的过程中,当前过滤器会从Spring工厂中提取同名对象 -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
注意:这里的filter-name要和我们的shiro配置文件的管理器的id一致,否则会报异常
接下来创建shiro的配置文件:shiro-context.xml 创建这个配置文件我们要在web.xml中加载上
<servlet>
<servlet-name>SpringMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-mvc.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
这是初始化的springmvc,后面又加一个
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml,
classpath:shiro-context.xml
</param-value>
</context-param>
最基本的配置算是完成了。接下来看一下,shiro-context.xml是如何配置的
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"><!--这里的id和filter-name一致-->
<property name="securityManager" ref="securityManager" /> <!--加载管理器-->
<property name="loginUrl" value="/user/login" /> <!--没有登录的时候,跳转到这个页面-->
<property name="unauthorizedUrl" value="/user/nopermission" /> <!--当没有权限的时候,跳转到这个url-->
<property name="filterChainDefinitions">
<value>
<!-- /*=anon -->
/user/login = anon<!--可以不需要登录-->
<!--/user/readName = authc, perms[/readName] perms 表示需要该权限才能访问的页面 -->
<!-- /user/readData = authc, perms[/readData]-->
/user/* = authc <!-- authc 表示需要认证才能访问的页面 -->
</value>
</property>
</bean>
<!-- 自定义Realm -->
<bean id="myShiroRealm" class="com.mhkjup.artcircle.shiro.MyShiroReaml">
<!-- businessManager 用来实现用户名密码的查询 -->
<property name="shiroService" ref="accountService" />
</bean>
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- 注入realm -->
<property name="realm" ref="myShiroRealm"/>
</bean>
<!--声明一个Service 注入到自定义Realm-->
<bean id="accountService" class="com.mhkjup.artcircle.service.impl.ShiroServiceImpl"/>
<!-- <bean id="shiroCacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<property name="cacheManager" ref="cacheManager" /> </bean> -->
</beans>
其他具体代码就需要自己去实现了