SSM+Shiro快速入门教程

  1. 掌握SSM的基础,要是不会SSM的整合直接学Shiro请参考作者之前写的博客
  2. 本文只会带你进入Shiro的简单应用,深入请参考官方Shiro的API

开始Shiro之旅,首先准备好SSM项目,保证能够跑通,然后开始了解基本的概念


  1. Security Manager:安全管理器;Shiro的核心,可以把它看成SpringMVC的DispatcherServlet。
  2. Realm:域;可以把Realm看成DataSource,即安全数据源
  3. Subject:主体,可以看到主体可以是任何可以与应用交互的“用户”
  4. Authenticator:认证器
  5. Authrizer:授权器。这两个单词有剧毒,自己注意

Shiro还有包括加密,缓存等等,后续自己去了解
传送门

开始写代码
整合就要写xml配置,粘代码

  <!-- Realm实现 这个要自己实现,例如登录授权等 -->
    <bean id="customReam" class="com.tanoak.realm.CustomReam">
    </bean>
    <!-- 安全管理器 -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="customReam"/>
    </bean>

    <!-- Shiro的Web过滤器 -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <property name="loginUrl" value="/login.jsp"/>
        <property name="unauthorizedUrl" value="/unauthorized.jsp"/>
        <property name="filterChainDefinitions">
            <value>
                /** = anon
            </value>
        </property>
    </bean>

    <!-- Shiro生命周期处理器-->
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>

注意在spring的配置文件中import

<import resource="classpath:shiro-base.xml"/>

附加上自定义的Realm,没有做具体的实现,纯属让Shiro运行起来


public class MyReam  extends AuthorizingRealm{
    @Override
    public void setName(String name) {
        super.setName("My Realm");
    }
    /**
    * 认证
    **/
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //Token是用户输入
        //1取出身份信息
        String userCode = (String) token.getPrincipal();

        if(!userCode.equals("zhangsan")){
            return null ;
        }
        //2 根据用户输入的Token取出身份信息
        String password = "111111" ;

        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(userCode ,password ,this.getName()) ;

        return simpleAuthenticationInfo;
    }

    /**
     * 授权
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

        return null;
    }

接下来注意配置web.xml

<!-- Shiro -->

   <filter>
      <filter-name>shiroFilter</filter-name>
      <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
      <init-param>
          <param-name>targetFilterLifecycle</param-name>
          <param-value>true</param-value>
      </init-param>
  </filter>
  <filter-mapping>
      <filter-name>shiroFilter</filter-name>
      <url-pattern>/*</url-pattern>
      <dispatcher>REQUEST</dispatcher>
  </filter-mapping>

只管粘贴就成,具体意思看文章开头的传送门,有详细介绍
,整合完毕,接下来直接运行就ok了,注意本人的Realm没有做认证,授权,再三强调。
最后附上本项目的github地址,可以直接git下来自己运行
https://github.com/tanoak10/ShiroDemo

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容