Shiro

Shiro简介

  • (1)Shiro是什么?
    Apache Shiro是一个强大且易用的Java安全框架/权限框架
    本质:预先定义好的权限代码(过滤器,RBAC模型设计,JSP标签等)
  • (2)Shiro有什么用?
    执行身份验证、授权(查询有什么权限)、密码学(md5,sha1)和会话管理
  • (3)Shiro有什么特点?
    常见的权限框架有apache Shiro ,与spring Security等

Shiro环境搭建

Shiro搭建步骤介绍

搭建shiro环境并实现认证,步骤如下:

  • (1)项目添加shiro依赖(在parent工程已经有了)

shiro依赖

       <!--shiro-->
        <!--shiro和spring整合-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!--shiro核心包-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>1.3.2</version>
        </dependency>
  • (2)配置web.xml
  • (3)配置applicationContext-shiro.xml, Spring整合shiro
  • (4)创建自定义realm类:AuthRealm,继承AuthorizingRealm
    AuthRealm extends AuthorizingRealm

web.xml中配置的过滤器

 <!-- shiro 搭建1 :配置过滤器 DelegatingFilterProxy
      配置Shiro的过滤器:拦截需要认证或授权的请求 -->
  <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>

当前过滤器会使用一个叫做过滤器工厂的对象创建多个用途过滤器
如何找到spring管理的过滤器工厂,根据filter-name的值与bean的id的值 一致

applicationContext-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.xsd">

    <!--shiro搭建2  2.3 配置shiroFilter 认证或授权逻辑处理对象-->
    <!--注入:这里的shiroFilter必须和web.xml的filter-name保持一致-->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!--注入SecurityManager-->
        <property name="securityManager" ref="securityManager"/>
    </bean>

    <!--2.1.创建 SecurityManager-->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!--注入Realm-->
        <property name="realm" ref="authRealm"/>
    </bean>

    <!--2.2.创建Realm-->
    <bean id="authRealm" class="com.wzx.web.shiro.AuthRealm"/>

</beans>

AuthRealm

//不需要再添加@Component 因为在xml中使用bean标签配置了
//AuthorizingRealm 认证(登录账号密码)和 授权(查有什么权限)
public class AuthRealm extends AuthorizingRealm {
    private Logger l = LoggerFactory.getLogger(this.getClass());
    //spring是通过反射调用无参构造函数
    public AuthRealm(){
        l.info("AuthRealm 无参构造函数执行了");
    }
    //授权(查有什么权限)
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        l.info("AuthRealm doGetAuthorizationInfo 函数执行了");
        return null;
    }
    //认证(登录账号密码)
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        l.info("AuthRealm doGetAuthenticationInfo 函数执行了");
        return null;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值