一》引入shirojar包

<!-- shiro登陆权限控制 -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-web</artifactId>
            <version>1.3.2</version>
        </dependency>

<dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
二》web配置文件配置shiro过滤器

http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
     <!--shrio  -->
 
       <filter>  
            <filter-name>shiroFilter</filter-name>  
                <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
                <init-param>  
                <!-- 该值缺省为false,表示生命周期由SpringApplicationContext管理,设置为true则表示由ServletContainer管理 -->  
            <param-name>targetFilterLifecycle</param-name>  
            <param-value>true</param-value>  
            </init-param>  
        </filter>
        
        <filter-mapping>  
            <filter-name>shiroFilter</filter-name>  
            <url-pattern>*.do</url-pattern>  
        </filter-mapping>

三》导入spring-shiro.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    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/jdbc
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache-3.1.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">

<!-- Shiro默认会使用Servlet容器的Session,可通过sessionMode属性来指定使用Shiro原生Session -->  
   <bean id="adminRealm" class="com.wskj.app.shiro.AdminRealm"/>  
    <!-- 即<property name="sessionMode" value="native"/>,详细说明见官方文档 -->  
    <!-- 这里主要是设置自定义的单Realm应用,若有多个Realm,可使用'realms'属性代替 -->  
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
        <property name="realm" ref="adminRealm"/>  
    </bean>
    
    <!-- Shiro主过滤器本身功能十分强大,其强大之处就在于它支持任何基于URL路径表达式的、自定义的过滤器的执行 -->  
    <!-- Web应用中,Shiro可控制的Web请求必须经过Shiro主过滤器的拦截,Shiro对基于Spring的Web应用提供了完美的支持 -->  
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
        <!-- Shiro的核心安全接口,这个属性是必须的 -->  
        <property name="securityManager" ref="securityManager"/>  
         
        <property name="loginUrl" value="/adminLogin/login.do" />
         
        <property name="unauthorizedUrl" value="/admin/userInfo.do"></property>
           
        <!-- anon:它对应的过滤器里面是空的,什么都没做-->  
        <!-- authc:该过滤器下的页面必须验证后才能访问,它是Shiro内置的一个拦截器org.apache.shiro.web.filter.authc.FormAuthenticationFilter -->  
        <property name="filterChainDefinitions">  
            <value>  
                /adminLogin/*.do=anon <!-- 任何人都可以访问,匿名  -->
                /add*.do=perms[add]
                /edit*.do=perms[edit]
                /del*.do=perms[delete]
                /admin/*.do=authc <!-- 需要登录 -->  
                /visit/*.do=authc <!-- 需要登录 -->  
            </value>  
        </property>   
    </bean>
</beans>

四》创建实体com.wskj.app.shiro.AdminRealm   (权限控制我应用了user注意)

package com.wskj.app.shiro;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.druid.sql.visitor.functions.Right;
import com.wskj.app.service.AdminService;
import com.wskj.app.service.UserService;
import com.wskj.app.vo.ShiroAdmin;
import com.wskj.app.vo.ShiroUser;

@Component(value="adminRealm")
public class AdminRealm extends AuthorizingRealm{
    
    @Autowired
    private AdminService adminService;
    
    @Autowired
    private UserService userService;
    /**
     *
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    
        // 从用户身份集合中读到一个主要身份
         ShiroUser suser = (ShiroUser) principals.getPrimaryPrincipal();
        // 如果用户只做了登录,但从来没有做过权限验证,则需要先从数据库中取得当前用户的权限值,并放入到ShiroUser中
        // ShiroUser是自定义的存储用户身份信息的类
        if  ( suser.getRights().isEmpty()) {
            //查询出用户信息
            Map<String, Object> usMap= userService.getUserByUserName(suser.getUserName());
            Integer noid1=(Integer) usMap.get("noid");
            //通过用户的noid查询出用户权限
            
            List<String> rights=userService.getRightsbyUserId(noid1);
        for (String p : rights) {
                suser.getRights().add(p)  ;
            }
        }
        // 将获得的用户的角色和权限列表放入到auth中,并直接返回即可
        // user_add user_edit
        SimpleAuthorizationInfo auth = new SimpleAuthorizationInfo();
        auth.addStringPermissions(suser.getRights());

return auth;
        
 
    }
    /**
     *
     * 做登录处理
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) arg0;
        String uname = token.getUsername();

Map<String , Object> ui =adminService.getAdminByName(uname);
        if ( ui!=null) {
            // 创建一个验证用户名和密码的
            ShiroAdmin suser = new ShiroAdmin();
            Integer noid=(Integer) ui.get("noid");
            suser.setNoid(noid);
            suser.setAdminName((String)ui.get("admin_name"));
            suser.setAdminCode(uname);
            SimpleAuthenticationInfo saci = new SimpleAuthenticationInfo(suser, ui.get("admin_pwd"), (String) ui.get("admin_code"));
            return saci;
        }
        return null;
  
 }
}
五》vo 实体建一个ShiroAdmin  把用户的验证信息存储起来

package com.wskj.app.vo;

import java.util.ArrayList;
import java.util.List;

public class ShiroAdmin {
    
    private Integer noid;
    
    private String adminName;
    
    private String adminCode;
    
    private List<String> permCodes=new ArrayList<String>();

public Integer getNoid() {
        return noid;
    }

public void setNoid(Integer noid) {
        this.noid = noid;
    }

public String getAdminName() {
        return adminName;
    }

public void setAdminName(String adminName) {
        this.adminName = adminName;
    }

public List<String> getPermCodes() {
        return permCodes;
    }

public void setPermCodes(List<String> permCodes) {
        this.permCodes = permCodes;
    }

public String getAdminCode() {
        return adminCode;
    }

public void setAdminCode(String adminCode) {
        this.adminCode = adminCode;
    }

}

至此,shiro的登陆验证就完成了

shiro登陆权限验证的更多相关文章

  1. Struts2 自定义拦截器实例—登陆权限验证

    实现一个登陆权限验证的功能 message.jsp: <body> message:${message } </body> login.jsp: <% request.g ...

  2. 登陆权限验证Session和Cookie用法及BasePage类使用

    最近在做ASP.NET的项目时,接触到了登陆权限模块,所有总结了一下登陆时用到的知识和方法技巧. 如图说明:实现的效果如图,由于验证码验证比较简单这里就不介绍了 首先用代码生成器生成项目,以三层为例进 ...

  3. 自定义shiro实现权限验证方法isAccessAllowed

    由于Shiro filterChainDefinitions中 roles默认是and, admin= user,roles[system,general] 比如:roles[system,gener ...

  4. .net core 2.0 登陆权限验证

    首先在Startup的ConfigureServices方法添加一段权限代码 services.AddAuthentication(x=> { x.DefaultAuthenticateSche ...

  5. Spring Security 自定义 登陆 权限验证

    转载于:https://www.jianshu.com/p/6b8fb59b614b 项目简介 基于Spring Cloud 的项目,Spring Cloud是在Spring Boot上搭建的所以按照 ...

  6. Struts2自定义拦截器实例—登陆权限验证

    版本:struts2.1.6 此实例实现功能:用户需要指定用户名登陆,登陆成功进入相应页面执行操作,否则返回到登陆页面进行登陆,当直接访问操作页面(登陆后才能访问的页面)时则不允许,须返回登陆页面. ...

  7. Struts2自己定义拦截器实例—登陆权限验证

    版本号:struts2.1.6 此实例实现功能:用户须要指定username登陆,登陆成功进入对应页面运行操作,否则返回到登陆页面进行登陆,当直接訪问操作页面(登陆后才干訪问的页面)时则不同意,须返回 ...

  8. Struts2他们拦截器实例定义—登陆权限验证

    版本号:struts2.1.6 这种情况下实现功能:用户需要指定username登陆,进入相应的页面运行成功登陆作战,否则,它返回到着陆的登录页面,当直接进入操作页面(登陆访问页面后的能力)如果不同意 ...

  9. @RequiresPermissions注解的作用,超级简单的权限验证

    是shiro里面权限验证的一个注解 @RequiresPermissions(value = {"engineeringPause:download", "workCon ...

随机推荐

  1. nexus3的安装和使用

    参考:https://www.cnblogs.com/2YSP/p/9533506.html http://www.54tianzhisheng.cn/2017/10/14/Nexus3-Maven/ ...

  2. check_nrpe: ERROR - could not complete SSL handshake

    情景描述: 发现的问题是 在监控端执行 ./check_nrpe -H 被监控端ip 正常返回nrpe版本 在被监控端执行 ./check_nrpe -H 监控端ip 报错 check_nrpe: E ...

  3. 【Codeforces 429B】Working out

    [链接] 我是链接,点我呀:) [题意] 两个人,一个人在左上角,一个人在左下角. 左上角要到右下角去 左下角要到右上角去 只能走到相邻的格子(不能往回走,即一个往右下,一个往右上走) 要求这两个人必 ...

  4. Android音乐、视频类APP常用控件:DraggablePanel(1)

     Android音乐.视频类APP常用控件:DraggablePanel(1) Android的音乐视频类APP开发中,常涉及到用户拖曳视频.音乐播放器产生一定交互响应的设计需求,最典型的以You ...

  5. mysql-sql语句中变量的使用

    最近工作中用到mysql,发现mysql和Oracle差别挺大的,其不像Oracle中存在丰富的分析函数(开窗函数),如rank(),lag(),leaf()等,只能用变量来获取以便达到分析函数的效果 ...

  6. windows 2013(codevs 1695)

    题目描述 Description 话说adamyi编的Windows 2013超时了(- -!),所以他不得不在自己家门口亲眼见证这个电影般的场景.虽然他不想错过这个美妙的时刻,但是他的肚子一再抗议, ...

  7. [NOIP2016day2T1] 組合數問題(problem)

    题目描述 组合数C(n,m)表示的是从n个物品中选出m个物品的方案数.举个例子,从(1,2,3) 三个物品中选择两个物品可以有(1,2),(1,3),(2,3)这三种选择方法.根据组合数的定 义,我们 ...

  8. [bzoj1855][Scoi2010]股票交易_动态规划_单调队列

    股票交易 bzoj-1855 Scoi-2010 题目大意:说不明白题意系列++...题目链接 注释:略. 想法:这个题还是挺难的. 动态规划没跑了 状态:dp[i][j]表示第i天手里有j个股票的最 ...

  9. Hibernate注解开发教程

    目录 第一章 类级别注解 1-1 本章简介 一.Hibernate注解简介 二.JPA与Hibernate的关系 三.Hibernate注解的分类 1-2 准备工作 1-3 @Entity注解 1-4 ...

  10. 微信推送给服务器的XML消息解析-springmvc 解析xml数据流

    微信推送给服务器的XML消息解析: 可以使用request.getInputStream(); 获取输入的消息流:但是需要自己解析流: spring mvc自带解析功能: controller中: @ ...