SSM+Shiro+用户登录授权

SSM与整合Shiro进行整合实现用户登录验证及权限分配

在前面的学习过程中,已经学习了 Shiro的一些基本组件以及如何利用Shiro进行权限控制。下面学习用目前比较主流的SSM框架来整合Shiro框架,将我们前面学习的一些基本知识运用到实际项目之中。由于本人初学,内容可能比较粗浅,后期进行深入理解后会细化,SSM项目的搭建过程这里不再叙述,可查阅其他人教程学习,可能有时间也会进行回顾记录。

为方便对下述内容进行理解,先画了一个流程图进行辅助

第一步项目配置文件(项目常规操作)

1.spring-config.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    <!-- 让spring为我们自动生成bean -->
    <context:component-scan base-package="com.shiro.test.mvc">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

   <!--  <bean id="iniRealm" class="org.apache.shiro.realm.text.IniRealm">
        <constructor-arg name="resourcePath" value="classpath:shiro-web.ini"/>
    </bean> -->
      <!--   加载数据库连接信息的配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    
         <!--   配置数据源 -->
    <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="driverClassName" value="${jdbc.driver}"/>
       <property name="url" value="${jdbc.url}"/>
       <property name="username" value="${jdbc.username}"/>
       <property name="password" value="${jdbc.password}"/>
    </bean>
    
        <!--配置spring和mybatis整合的SqlSessionFactoryBean -->
        
        <bean name="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
          <!--  注入数据源 -->
          <property name="dataSource" ref="dataSource"/>
          <!--  实体类和mapper文件进行映射 -->
          <property name="typeAliasesPackage" value="com.shiro.test.mvc.pojo"/>
          <property name="mapperLocations" value="classpath:com/shiro/test/mvc/dao/*.xml"/>
        </bean>
        <!-- 声明事物管理器 -->
        <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
     <!--声明事务的增强 , 添加,删除失败回滚,get只读,其他的不支持事务-->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="add*"
                       isolation="REPEATABLE_READ" rollback-for="Exception"/>
            <tx:method name="update*"
                       isolation="REPEATABLE_READ" rollback-for="Exception"/>
            <tx:method name="delete*"
                       isolation="REPEATABLE_READ" rollback-for="Exception"/>
            <tx:method name="get*"
                       isolation="REPEATABLE_READ" read-only="true"/>
            <tx:method name="*"
                       isolation="REPEATABLE_READ" propagation="NOT_SUPPORTED"/>
        </tx:attributes>
    </tx:advice>

    <!--切入点-->
    <aop:config>
        <aop:pointcut
                expression="execution(* com.shiro.test.mvc.service..*(..))" id="point"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="point"/>
    </aop:config>
          <!--  动态的 实现生成类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.shiro.test.mvc.dao"/>
    </bean>
     <!-- 申明自定义数据源给shiro管理器注入 -->   
    <bean id="myShiroRealm" class="com.shiro.test.mvc.filter.MyShiroRealm"/>
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!-- <property name="realm" ref="iniRealm"/> -->
       <property name="realm" ref="myShiroRealm"/> 
    </bean>
    
    
    <!-- 基于注解的时候配置 -->
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
        <property name="securityManager" ref="securityManager"/>
    </bean>
    <!-- Shiro连接约束配置,即过滤链的定义 -->
	<!-- 下面value值的第一个'/'代表的路径是相对于HttpServletRequest.getContextPath()的值来的 -->
	<!-- anon:它对应的过滤器里面是空的,什么都没做,这里.do和.jsp后面的*表示参数,比方说login.jsp?main这种 -->
	<!-- authc:该过滤器下的页面必须验证后才能访问,它是Shiro内置的一个拦截器org.apache.shiro.web.filter.authc.FormAuthenticationFilter -->
    <bean id="shiroFilter" class="com.shiro.test.mvc.filter.MyShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <property name="loginUrl" value="/gologin"/>
        <property name="successUrl" value="/index"/>
        <property name="unauthorizedUrl" value="/error"/>
        <property name="filterChainDefinitions">
            <value>
                /login=anon
                /gologin=anon
                /index = authc
                /role=authc,roles[admin]
                /menu/** = authc,roles[admin,test]
            </value>
        </property>
        <property name="filters">
           <map>
             <entry key="roles">
               <bean class="com.shiro.test.mvc.filter.MyShiroFilter"/>
             </entry>
           </map>
        </property>
    </bean>
</beans>

  <!-- 申明自定义数据源给shiro管理器注入 -->   
    <bean id="myShiroRealm" class="com.shiro.test.mvc.filter.MyShiroRealm"/>
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
        <!-- <property name="realm" ref="iniRealm"/> -->
       <property name="realm" ref="myShiroRealm"/> 
    </bean>

申明自定的数据源,在进行登录验证的时候将会从自定义的数据源里进行查找

2. 从Controller从前台接受到邮箱和密码调用Service

2.Service对Controller传过来的请求进行处理

 自定义MyShiroRealm继承AuthorizingRealm(用户认证及授权)

 此图借用他人的,若侵权联系  删

 

 好了,到这里就已经完成了一个shiro框架对用户信息登录认证的过程。

接下来来看看授权的过程

在授权发方法中

@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		System.out.println("进入授权方法,进行授权");
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		User user = (User) principals.getPrimaryPrincipal();
        //获取用户的角色及角色的模块
        Set<Role> roles = user.getRoles();//通过用户查到用户角色
        for (Role role : roles) {//遍历数据
        Set<Module> modules = role.getModules();//通过用户查到用户模块
        for (Module module : modules) {
        info.addStringPermission(module.getCpermission());
     //通过用户模块查到模块名并添加到info中。也就是说把查到的模块显示到页面中,没有的就代表没授权
    }
    return list;
}

前台页面会通过shiro标签进行判断是否有相应权限并进行显示 

<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro" %>

 //要想使用shiro的标签就一点要导数据标签

举例:

<shiro:hasPermission name="menu:list">

//shiro:hasPermission:拥有权限的资源  name:权限访问的名字,如上:当该用户里有部门管理权限时,才会让其看到部门管理这个按钮

 <li><a href="${ctx}/sysadmin/deptAction_list" οnclick="linkHighlighted(this)" target="main" id="aa_1">menu底下的菜单</a></li></shiro:hasPermission>

注意:以上方法可以使用户看不到自己没有权限的数据,但是如果用户自己写链接还是能访问到数据的

1.配置过滤器

  <bean id="shiroFilter" class="com.shiro.test.mvc.filter.MyShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <property name="loginUrl" value="/gologin"/>
        <property name="successUrl" value="/index"/>
        <property name="unauthorizedUrl" value="/error"/>
        <property name="filterChainDefinitions">
            <value>
                /login=anon
                /gologin=anon
                /index = authc
                /role=authc,roles[admin]
                /menu/** = authc,roles[admin,test]
            </value>
        </property>
        <property name="filters">
           <map>
             <entry key="roles">
               <bean class="com.shiro.test.mvc.filter.MyShiroFilter"/>
             </entry>
           </map>
        </property>
    </bean>

此处,我是使用的自定义的过滤器,如没有必要自定义的话使用org.apache.shiro.web.filter.authc.FormAuthenticationFilter可查阅文档,看如何实现,不再陈述。

2.使用注解方式(别忘了配置文件配置,上面配置文件中有有注解)

//这里代表的时要走这个方法模块中就得有角色管理这个模块,没有就拒绝访问

@RequiresPermissions(value="menu:list")

    public Page<Role> findPage(Specification<Role> spec, Pageable pageable) {

        // TODO Auto-generated method stub

        return roleDao.findAll(spec, pageable);

    }

到这里就完成了授权的过程,希望对你有所帮助,如有错误欢迎指出。

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值