ssm+shiro框架的详细配置

首先导入maven的pom文件

<!--Shiro安全框架集成,主要用来更便捷的认证,授权,加密,会话管理-->
    <!-- shiro配置 -->
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-core</artifactId>
      <version>${shiro.version}</version>
    </dependency>
    <!-- Enables support for web-based applications. -->
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-web</artifactId>
      <version>${shiro.version}</version>
    </dependency>
    <!-- Enables AspectJ support for Shiro AOP and Annotations. -->
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-aspectj</artifactId>
      <version>${shiro.version}</version>
    </dependency>
    <!-- Enables Ehcache-based famework caching. -->
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-ehcache</artifactId>
      <version>${shiro.version}</version>
    </dependency>
    <!-- Enables Spring Framework integration. -->
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-spring</artifactId>
      <version>${shiro.version}</version>
    </dependency>

或者直接导入一个
<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-all -->
    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-all</artifactId>
      <version>1.4.0</version>
      <type>pom</type>
    </dependency>

新建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" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <!--shiro 核心安全接口  -->
        <property name="securityManager" ref="securityManager"></property>
        <!--登录时的连接  -->
        <property name="loginUrl" value="/login"></property>       
        <!--未授权时跳转的连接  -->
        <property name="unauthorizedUrl" value="/unauthorized.jsp"></property>
           <!-- 其他过滤器 -->
           <property name="filters">
               <map>
                   <!-- <entry key="rememberMe" value-ref="RememberMeFilter"></entry> -->
                   <entry key="kickout" value-ref="KickoutSessionControlFilter"/>
               </map>
           </property>
               
        <!-- 读取初始自定义权限内容-->
        <!-- 如果使用authc验证,需重写实现rememberMe的过滤器,或配置formAuthenticationFilter的Bean -->
        <property name="filterChainDefinitions">
            <value>
                /js/**=anon
                /css/**=anon
                /images/**=anon
                /skin/**=anon
                   /lib/**=anon
                   /nodel/**=anon
                   /WEB-INF/jsp/**=anon
                   /adminUserLogin/**=anon
                   
       
                   
                   /**/submitLogin.do=anon
                /**=user,kickout
            </value>
        </property>
    </bean>
    
    
    
    
    <!-- Shiro生命周期处理器-->
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
    
    <!-- 安全管理器 -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <property name="realm" ref="MyRealm"/>
        <property name="rememberMeManager" ref="rememberMeManager"/>
    </bean>
    
    <bean id="MyRealm" class="com.sys.shiro.MyRealm" >
        <property name="cachingEnabled" value="false"/>
    </bean>
    
    <!-- 相当于调用SecurityUtils.setSecurityManager(securityManager) -->
    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/>
        <property name="arguments" ref="securityManager"/>
    </bean>
    
    <!-- sessionIdCookie:maxAge=-1表示浏览器关闭时失效此Cookie -->
    <bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">  
        <constructor-arg value="rememberMe"/>  
        <property name="httpOnly" value="true"/>  
        <property name="maxAge" value="-1"/>  
    </bean>
         
    <!-- 用户信息记住我功能的相关配置 -->
    <bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
        <constructor-arg value="rememberMe"/>
        <property name="httpOnly" value="true"/>
        <!-- 配置存储rememberMe Cookie的domain为 一级域名        这里如果配置需要和Session回话一致更好。-->
        <property name="maxAge" value="604800"/><!-- 记住我==保留Cookie有效7天 -->
    </bean>
    
    <!-- rememberMe管理器 -->
    <bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">
        <!-- rememberMe cookie加密的密钥 建议每个项目都不一样 默认AES算法 密钥长度(128 256 512 位)-->
        <property name="cipherKey"
                  value="#{T(org.apache.shiro.codec.Base64).decode('3AvVhmFLUs0KTA3Kprsdag==')}"/>
        <property name="cookie" ref="rememberMeCookie"/>
    </bean>
    
    <!-- 记住我功能设置session的Filter -->
    <bean id="RememberMeFilter" class="com.sys.shiro.RememberMeFilter" />
    
    <!-- rememberMeParam请求参数是 boolean 类型,true 表示 rememberMe -->
    <!-- shiro规定记住我功能最多得user级别的,不能到authc级别.所以如果使用authc,需打开此配置或重写实现rememberMe的过滤器 -->
    <!-- <bean id="formAuthenticationFilter" class="org.apache.shiro.web.filter.authc.FormAuthenticationFilter">
        <property name="rememberMeParam" value="rememberMe"/>
    </bean> -->    
    
    <bean id="KickoutSessionControlFilter" class="com.sys.shiro.KickoutSessionControlFilter">
    </bean>
               
    
</beans>

在applicationContext.xml配置文件中加入

<!-- 包含shiro的配置文件 -->
<import resource="classpath:applicationContext-shiro.xml"/>

在springmvc-config.xml中加入

 <!--启用shiro注解 -->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
        <property name="proxyTargetClass" value="true" />
    </bean>
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager" />
    </bean>
    <!-- shiro为集成springMvc 拦截异常,使用注解时无权限的跳转 -->
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="exceptionMappings">
            <props>
                <!-- 这里你可以根据需要定义N多个错误异常转发 -->
                <prop key="org.apache.shiro.authz.UnauthorizedException">redirect:/unauthorized</prop>
                <prop key="org.apache.shiro.authz.UnauthenticatedException">redirect:/unauthorized</prop>
                <prop key="java.lang.IllegalArgumentException">/error</prop>  <!-- 参数错误(bizError.jsp) -->
                <prop key="java.lang.Exception">/error</prop>  <!-- 其他错误为'未定义错误'(unknowError.jsp) -->
            </props>
        </property>
    </bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSM框架中使用Shiro进行免登录功能的实现可以按照以下步骤进行: 1. 在用户登录成功后,生成一个唯一的标识符,例如UUID,并将其存储到Cookie中。可以使用`HttpServletResponse`对象的`addCookie()`方法来设置Cookie。 2. 将该标识符作为key,用户密码作为value,存储到Redis中。可以使用Redis的客户端库(例如Jedis)来连接Redis服务器并执行存储操作。 3. 在用户访问需要认证的页面时,通过Shiro的过滤器进行权限验证。在Shiro配置文件中添加相应的过滤器和拦截规则。 4. 创建一个自定义的Shiro Realm,在其中重写`doGetAuthenticationInfo()`方法,用于从Redis中根据Cookie的值获取用户密码,并验证用户身份。 下面是一个简单示例代码: 1. 在登录成功后生成并设置Cookie: ```java // 生成UUID作为标识符 String sessionId = UUID.randomUUID().toString(); // 将sessionId存储到Cookie中 Cookie cookie = new Cookie("sessionId", sessionId); response.addCookie(cookie); // 将sessionId和用户密码存储到Redis中 Jedis jedis = new Jedis("redis_host", redis_port); jedis.set(sessionId, user.getPassword()); jedis.expire(sessionId, expire_time); ``` 2. 在Shiro配置文件中添加过滤器和拦截规则: ```xml <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <!-- ...其他配置... --> <property name="filters"> <map> <!-- ...其他过滤器配置... --> <entry key="authc"> <bean class="org.apache.shiro.web.filter.authc.FormAuthenticationFilter"> <!-- 设置登录页的URL --> <property name="loginUrl" value="/login"/> </bean> </entry> <entry key="user"> <bean class="org.apache.shiro.web.filter.authc.UserFilter"/> </entry> </map> </property> <property name="filterChainDefinitions"> <value> /login = anon /logout = logout /** = user </value> </property> </bean> ``` 3. 创建自定义Shiro Realm并重写`doGetAuthenticationInfo()`方法: ```java public class CustomRealm extends AuthorizingRealm { // ...其他重写的方法... @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { if (!(token instanceof UsernamePasswordToken)) { return null; } UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token; String sessionId = usernamePasswordToken.getUsername(); // 从Redis中根据sessionId获取用户密码 Jedis jedis = new Jedis("redis_host", redis_port); String password = jedis.get(sessionId); if (password == null) { throw new UnknownAccountException("Invalid session"); } // 返回身份验证信息 return new SimpleAuthenticationInfo(sessionId, password, getName()); } } ``` 以上是一种基本的实现方式,你可以根据具体的业务需求进行适当调整和扩展。同时,请确保在生产环境中对Cookie和Redis的安全性进行合理的加固和保护。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值