maven在SSM项目中整合shiro框架的用法

1 篇文章 0 订阅

之前一直有听说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>

其他具体代码就需要自己去实现了

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值