SSM整合

匿名 (未验证) 提交于 2019-12-02 23:47:01

  Spring和整合实际上最大的特点就是可以交由Spring实现shiro的管理控制,实际上这种整合才属于新一代的SSM开发框架整合(Spring+Shiro+MyBatis)。

  SSH:Spring + Struts + Hibernate,但是后来Struts框架没落了;

  SSM一代:Spring + SpringMVC + MyBatis,SpringMVC本来就属于Spring的一部分;

  SSM二代:Spring + Shiro + MyBatis;

一、Spring整合Shiro:

  1. 修改pom.xml配置文件,引入Shiro与Spring要整合的依赖库:

<dependency>    <groupId>org.apache.shiro</groupId>    <artifactId>shiro-spring</artifactId></dependency><dependency>    <groupId>org.apache.shiro</groupId>    <artifactId>shiro-web</artifactId></dependency><dependency>    <groupId>org.apache.shiro</groupId>    <artifactId>shiro-core</artifactId></dependency>

2. 修改web.xml配置文件,通过过滤器集成shiro:

<filter-name> 对应的 filter bean. 也可以通过 targetBeanName 的初始化参数来配置 filter bean 的 id.

    <!-- 配置Shiro的 shiroFilter -->    <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><!--    <init-param>            <param-name>targetBeanName</param-name>            <param-value>shiroFilter</param-value>        </init-param>-->    </filter>

3. 定义Realm:

// 此处不使用扫描配置,通过bean配置文件的模式来完成 public class MemberRealm extends AuthorizingRealm {     @Autowired     private IMemberService memberService ;     @Autowired     private IMemberPrivilegeService memberPrivilegeService ;     @Override     protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {         System.err.println("【1】{MemberRealm}============== 用户认证处理 ==============");         String mid = (String) token.getPrincipal() ;         Member member = this.memberService.get(mid) ; // 根据mid查询用户信息         if (member == null) {   // 用户信息不存在             throw new UnknownAccountException(mid + "账户信息不存在!") ;         }         String password = new String((char[]) token.getCredentials()) ;         if (!member.getPassword().equals(password)) {   // 密码不同             throw new IncorrectCredentialsException("错误的用户名或密码!");         }         if (member.getLocked().equals(1)) { // 用户锁定了             throw new LockedAccountException(mid + "账户已经被锁定!");         }         return new SimpleAuthenticationInfo(token.getPrincipal(),token.getCredentials(),this.getName());     }     @Override     protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {         System.err.println("【2】{MemberRealm}************** 用户授权处理 **************");         Map<String, Set<String>> map = this.memberPrivilegeService.getByMember((String) principals.getPrimaryPrincipal());         // 将所有获取的授权信息保存在AuthorizationInfo类的实例之中         SimpleAuthorizationInfo authz = new SimpleAuthorizationInfo() ; // 返回的授权信息         authz.setRoles(map.get("allRoles"));         authz.setStringPermissions(map.get("allActions"));         return authz;     } } 

4. 定义shiro的配置文件“spring-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">     <!-- 1、Shiro整合处理的时候,Realm是整合业务层(RPC端)的核心处理综合点 -->     <bean id="memberReam" class="com.yootk.ssm.realm.MemberRealm"/>     <!-- 2、整个的Shiro里面最为重要的就是SecurityManager,这个类之中需要追加realm配置 -->     <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">         <property name="realm" ref="memberReam"/>   <!-- 配置要使用的Realm处理程序 -->     </bean>     <!-- 3、 在Spring中配置有关Shiro的全部的过滤器的定义,这个名称必须与web.xml文件保持一致-->     <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">         <property name="securityManager" ref="securityManager"/>         <property name="loginUrl" value="/login.action"/> <!-- 配置登录页 -->         <property name="filterChainDefinitions"><!-- 检测路径 -->             <value>                 /pages/**=authc             </value>         </property>     </bean> </beans>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!