Subject进行Shiro的授权操作,首先委托给Security Manager,在由Security Manager 委托给Authenticator,由Authenticator授权器进行真正的授权操作。

  这里记录在Spring环境下配置Shiro框架。

  1、加入依赖:org.apache.shiro-core ,org.apache.shiro-web,org.apache.shiro-spring 版本都为1.2.2

  2、在web.xml中加入ShiroFilter代理类

    shiro和spring的集成需要代理类,这个shiro过滤器会从spring的容器中寻找真正的ShiroFilter

      注:filter-name必须和配置文件真实的Bean一致

<filter>
<filter-name>shiroFilter</filter-name><!--shiroFilter必须和shiro配置文件(spring-shiro.xml)中真实的ShiroFilterBeanId一致-->
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

  3、在IOC容器中配置真正的ShiroFilter,这里创建一个专门用于Shiro的配置文件

<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="SecurityManager"/>
<property name="loginUrl" value="/webroot/html/login.html"/>
<property name="filterChainDefinitions">
<value>
/webroot/js/**=anon
/wxbackstage/login=anon
/**=authc
</value>
</property>

  在shiroFilter Bean的属性中,securityManager是必须的,这里依赖SecurityManager

  loginUrl,没有登录的用户请求需要登录的页面时自动跳转到登录页面,不是必须的属性,不输入地址的话会自动寻找项目web项目的根目录下的”/login.jsp”页面。

  successUrl :登录成功默认跳转页面,不配置则跳转至”/”。如果登陆前点击的一个需要登录的页面,则在登录自动跳转到那个需要登录的页面。不跳转到此。

  unauthorizedUrl :没有权限默认跳转的页面

  filterChainDefinitions(重点):这是一个Shiro过滤链,配置哪些web的资源需要认证(Authentication)才能访问,哪些资源需要授权(Authorization)才能访问。

      格式:url=过滤器

      默认的过滤器:anon – org.apache.shiro.web.filter.authc.AnonymousFilter 
             authc – org.apache.shiro.web.filter.authc.FormAuthenticationFilter 
             authcBasic – org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter 
             perms – org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter 
             port – org.apache.shiro.web.filter.authz.PortFilter 
                rest – org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter 
             roles – org.apache.shiro.web.filter.authz.RolesAuthorizationFilter 
                ssl – org.apache.shiro.web.filter.authz.SslFilter 
             user – org.apache.shiro.web.filter.authc.UserFilter 
             logout – org.apache.shiro.web.filter.authc.LogoutFilter

     配置例子:

             /**=anon:所有的资源匿名也可以访问

            /admin/**=authc:admin下的所有网页或者请求方法都需要认证才能访问

            /admin/user=roles[admin]:admin/user下的网页或者请求方法需要有admin这个角色才能访问,参数是多个必须逗号分开,加上引号,如:rolse["admin1,admin2"]

            /**=rest[user]:分区请求方法,相当于/**=perms[user:method],其中,method代表,get,post,put,delete等等

            /**=port[8081],请求的url端口为8081,不是的话跳转到该端口进行url访问

            /admins/user/**=authcBasic:没有参数表示httpBasic认证

            /admins/user/**=ssl:没有参数,表示安全的url请求,协议为https

            /admins/user/**=user:没有参数表示必须存在用户,当登入操作时不做检查

     4、配置在Shiro配置文件中配置SecurityManager

 <bean id="SecurityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="StudentRealm"></property>
<!--配置多个realm 可增加数据安全性
<property name="realms" >
<list>
<ref bean="Realm1"/>
<ref bean="Realm2"/>
</list>
-->
</bean>

        这个Security Manager依赖自定义的 realm,自定义Realm可以是一个,也可以是多个

5、配置自定义realm

<bean id="StudentRealm" class="common.Shiro.StudentRealm">
<property name="wxMemberDAO" ref="wxMemberDAOImpl"></property>
<!--加密器,不管-->
<property name="credentialsMatcher" ref="credentialsMarcher"></property>
</bean>

自定义的realm来自SutdentRealm,同时注入了一个wxMemberDAO接口,并引入实例,从而访问数据库。(在wxMemberDAOImpl class中加入@Repository,从而获取该类bean实例)

  5、配置自定义realm

public class StudentRealm extends AuthorizingRealm {

    //注入DAO ,同时在spring-shiro的自定义Realm进行配置
@Setter
public WxMemberDAO wxMemberDAO; private Log log = LogFactory.getLog(StudentRealm.class);
public String getName() {
return "StudentRealm";
}
/*
* @Description: 授权器,验证角色身份权限的API
* @Param: principalCollection
* @return: org.apache.shiro.authc.AuthenticationInfo
* @Author: zhijie
* @Date: 2019/2/6
*/
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
return null;
} /*
* @Description:验证器,判断用户的登录 用户输入的账号密码封装成UserNamePasswordToken
* SimpleAuthenticationInfo封装查询之后的结果
* @Param: [authenticationToken]
* @return: org.apache.shiro.authc.AuthenticationInfo
* @Author: zhijie
* @Date: 2019/2/6
*/ protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { //通过 token获取用户名,查询数据库返回用户信息
String memberId = (String) authenticationToken.getPrincipal();
GroupMember groupMember = wxMemberDAO.login(memberId);
if(groupMember == null) {
log.info(memberId+"没找到帐号");
throw new AuthenticationException();
} SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(memberId,groupMember.getmemberpassword(), ByteSource.Util.bytes(memberId),getName()); return simpleAuthenticationInfo;
}
}

  继承AuthorizingRealm,更方便的继承认证和授权两个方法,先通过@Setter注入wxMemberDAO实例,在从第四步进行真正的注入。在重点看AuthenticationInfo的doGetAuthenticationInfo的方法。

    通过getPrincipal()获取username,在通过login方法获取member的对象,如果不存在,则抛出异常,否则返回一个SimpleAuthenticationInfo的对象。

      这个方法的参数:用户名,密码,盐(不管),这个realm的名字。

  6.从service中执行login方法。

 public ConcurrentSkipListMap login(String memberid,String password) {
//创建当前访问路径的主体
Subject subject = SecurityUtils.getSubject();
//判断是否已经进行认证
if(subject.isAuthenticated() == false) {
//将用户名和密码封装成UserNamePasswordToken
UsernamePasswordToken token = new UsernamePasswordToken(memberid,password);
subject.login(token);
}
System.out.println(subject.isAuthenticated());
GroupMember memberDepartmentPosition = wxMemberDAO.login(memberid);
concurrentSkipListMap.put("AuthorizationStatus",memberDepartmentPosition.getDepartmentposition()); return concurrentSkipListMap;
}

 因为已经在配置文件中配置了Security Manager,所以这里直接获取访问主体即可。判断是否已经进行了认证,最终在result中返回json的格式。

  

Shiro-Spring 授权操作的更多相关文章

  1. Spring与Shiro整合 登陆操作

    Spring与Shiro整合 登陆操作 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 编写登陆Controller方法  讲解: 首先,如果你登陆失败的时候,它会把你的异常信息丢到 ...

  2. Shiro —— Spring 环境下的使用

    一.使用 1.搭建基础环境 (1)导入 Spring 和 Shiro 的 Jar 包 正常导入 spring jar包 导入日志包 log4j-1.2.15.jar slf4j-api-1.6.1.j ...

  3. Shiro【授权、整合Spirng、Shiro过滤器】

    前言 本文主要讲解的知识点有以下: Shiro授权的方式简单介绍 与Spring整合 初始Shiro过滤器 一.Shiro授权 上一篇我们已经讲解了Shiro的认证相关的知识了,现在我们来弄Shiro ...

  4. Shiro【授权过滤器、与ehcache整合、验证码、记住我】

    前言 本文主要讲解的知识点有以下: Shiro授权过滤器使用 Shiro缓存 与Ehcache整合 Shiro应用->实现验证码功能 记住我功能 一.授权过滤器测试 我们的授权过滤器使用的是pe ...

  5. shiro的授权与认证

    shiro的授权与认证 package com.cy.pj.common.aspect;import java.lang.reflect.Method;import java.util.Arrays; ...

  6. shiro+spring相关配置

    首先pom中添加所需jar包: <!-- shiro start --> <dependency> <groupId>org.apache.shiro</gr ...

  7. Shrio00 Shiro角色授权、Shiro权限授权、开启Shiro缓存

    1 需求01 用户进行过认证登录后,某些接口是有权限限制的:如何实现只有相应权限的用户才可以调用相应接口 2 修改shiro配置类  ShiroConfiguration package cn.xia ...

  8. Java Spring mvc 操作 Redis 及 Redis 集群

    本文原创,转载请注明:http://www.cnblogs.com/fengzheng/p/5941953.html 关于 Redis 集群搭建可以参考我的另一篇文章 Redis集群搭建与简单使用 R ...

  9. MySQL账号授权操作

    Mysql权限控制 - 允许用户远程连接 设置mysql root密码: mysql -u root mysql> SET PASSWORD FOR 'root'@'localhost' = P ...

随机推荐

  1. Scss 与 Sass 是什么,他们的区别在哪里?

    转载自:http://yunkus.com/difference-between-scss-sass/ 要想了解Scss 与 Sass 是什么以及他们的区别又在哪里,我们不过不先从他们各自的定义说起. ...

  2. 【设计模式】抽象工厂模式(Abstract Factory Pattern)

    [前言] 上次针对自己的理解书写一篇工厂模式的文章,后面对于工厂模式进行更加多的学习,于是了解到了抽象工厂模式.其实网上大多数人们都是抽象工厂模式是工厂模式的升级版本,虽然我并不知道所说的升级是什么意 ...

  3. English trip V2 - 3. A Healthy Diet Teacher:Corrine Key:各种前缀 im- un- in- re- over- under-

    In this lesson you will learn to talk about foot and drink for a healthy diet. 课上内容(Lesson) What do ...

  4. Ketlle

    public class Kettle { private int volume; public Kettle(int water) {  volume =water;  System.out.pri ...

  5. mysql5.7 timestamp错误:there can be only oneTIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE

    #1293 - Incorrect table definition; there can be only oneTIMESTAMP column with CURRENT_TIMESTAMP in ...

  6. CSS设计模式

    关于web设计的网站 https://www.smashingmagazine.com/

  7. 交换机telnet配置

    新开箱交换机开机配置Telnet需要三个步骤: 1.开启telnet是能:系统视图模式下输入命令: telnet server enable #开启telnet功能# 2.Telnet创建账号:aaa ...

  8. mysql Access denied for user root @localhost (using password:YES)错误

    C:\AppServ\MySQL> mysql -u root -p Enter password:  ERROR 1045 (28000): Access denied for user 'r ...

  9. python mac 环境配置

    1, Mac安装 HTMLTestRunner,参考:https://blog.csdn.net/walter_chan/article/details/50555123 cd /Library/Py ...

  10. FPM六:接五,跳转到明细

    1.在RESULT的Feeder class里添加点击单号事件: method IF_FPM_GUIBB_LIST~GET_DEFINITION. DATA:GW_ACTION TYPE FPMGB_ ...