搭建shiro

pom.xml
添加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>


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>


创建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 AuthRealm.java文件路径-->
    <bean id="authRealm" class="com.lgp.web.shiro.AuthRealm"/>

</beans>


创建AuthRealm.java

//不需要再添加@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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值