有具体问题的可以参考之前的关于shiro的博文,关于shiro的博文均是一次工程的内容 !

认证策略:

修改认证策略:
applicationContext.xml
     <!-- 认证器 -->
<bean id="autheniicator"
class="org.apache.shiro.authc.pam.ModularRealmAuthenticator">
<property name="realms">
<list>
<ref bean="jdbcRealm"/>
<ref bean="SecondRealm"/>
</list>
</property> <!-- 认证策略 -->
<property name="authenticationStrategy">
<bean class="org.apache.shiro.authc.pam.AllSuccessfulStrategy"></bean>
</property>

</bean>

 授权:

授权之前的小说明:

此时的访问并没有问题:

注:在做授权的时候需要在securityManager 中读取realms

 <bean id="securityManager"  class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="cacheManager" ref="cacheManager"/>
<property name="authenticator" ref="autheniicator"></property> <property name="realms">
<list>
<ref bean="jdbcRealm"/>
<ref bean="SecondRealm"/>
</list>
</property>
</bean>

代码开始
新建一个类

list.sjp页面中

<body>
list.
<br>
<a href="admin.jsp">TO Admin</a>
<br>
<a href="user.jsp">TO User</a>
<br>
<a href="shiro/logout">Logout</a>
</body>

applicationContext.jsp

<!--
配置那些页面需要受保护,以及访问这些页面需要的的权限
)anon 可以被匿名访问
)authc 必须认证即登陆后才可以访问的页面
).logout登出
4)roles 角色过滤器
-->
<property name="filterChainDefinitions">
<value>
/login.jsp = anon
/shiro/login = anon
/shiro/logout = logout /user.jsp = roles[user]
/admin.jsp =
roles[admin] # everything else requires authentication:
/** = authc
</value>
</property>

此时点击访问user.jsp/admin.jsp的超链接都会去没有权限访问的页面

 授权流程:

需要实现AuthorizingRealm的.....
public abstract class AuthorizingRealm extends  AuthenticatingRealm
implements Authorizer, Initializable, PermissionResolverAware, RolePermissionResolverAware
AuthorizingRealm继承AuthenticatingRealm,但是没有实现AuthenticatingRealm的doGetAuthenticationInfo的方法
所以认证和授权只需要继承AuthorizingRealm就可以了,同时实现它的两个抽象方法
public class ShiroReamlTest extends AuthorizingRealm{

     //授权
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
// TODO Auto-generated method stub
return null;
} //加密
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
// TODO Auto-generated method stub
return null;
}
}

授权需要继承的类以及实现的方法

实现:

public class ShiroRealm extends AuthorizingRealm {
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken arg0) throws AuthenticationException {
。。。。。。。
}
//授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { System.out.println(principals);
//1.PrincipalCollection获取登陆的用户信息
Object principal = principals.getPrimaryPrincipal();
System.out.println(principal);
//2.利用登陆的用户信息获取当前用户角色的权限
Set<String> roles = new HashSet<String>();
roles.add("user");
if("admin".equals(principal)){
roles.add("admin");
}
//3.创建SimpleAuthorizationInfo,并且设置其reles属性
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(roles);
//4.
return info;
} }

此时的设置之后 是可以成功访问的!

shiro认证策略,授权的更多相关文章

  1. 【shiro】(4)---Shiro认证、授权案例讲解

    Shiro认证.授权案例讲解 一.认证  1. 认证流程     2.用户密码已经加密.加盐的用户认证 (1)测试类 // 用户登陆和退出,这里我自定了一个realm(开发肯定需要自定义realm获取 ...

  2. shiro认证和授权

    一.shiro基础概念 Authentication:身份认证 / 登录,验证用户是不是拥有相应的身份: Authorization:授权,即权限验证,验证某个已认证的用户是否拥有某个权限:即判断用户 ...

  3. shiro源码篇 - shiro认证与授权,你值得拥有

    前言 开心一刻 我和儿子有个共同的心愿,出国旅游.昨天儿子考试得了全班第一,我跟媳妇合计着带他出国见见世面,吃晚饭的时候,一家人开始了讨论这个.我:“儿子,你的心愿是什么?”,儿子:“吃汉堡包”,我: ...

  4. shiro框架学习-2-springboot整合shiro及Shiro认证授权流程

    1. 添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  5. 在web项目中使用shiro(认证、授权)

    一.在web项目中实现认证 第一步,在web项目中导入shiro依赖的包 第二步,在web.xml中声明shiro拦截权限的过滤器 <filter> <filter-name> ...

  6. shiro认证授权

    一.shiro基础概念 Authentication:身份认证 / 登录,验证用户是不是拥有相应的身份: Authorization:授权,即权限验证,验证某个已认证的用户是否拥有某个权限:即判断用户 ...

  7. 源码分析shiro认证授权流程

    1. shiro介绍 Apache Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理等功能: 认证 - 用户身份识别,常被称为用户“登录”: 授权 - 访问控制: 密码加密 ...

  8. 用户登录安全框架shiro—用户的认证和授权(一)

     ssm整合shiro框架,对用户的登录操作进行认证和授权,目的很纯粹就是为了增加系统的安全线,至少不要输在门槛上嘛. 这几天在公司独立开发一个供公司内部人员使用的小管理系统,客户不多但是登录一直都是 ...

  9. Shiro集成web环境[Springboot]-认证与授权

    Shiro集成web环境[Springboot]--认证与授权 在登录页面提交登陆数据后,发起请求也被ShiroFilter拦截,状态码为302 <form action="${pag ...

随机推荐

  1. 2.VS2012为创建的类添加注释的模板

    在项目中给类添加注释的优点: 1.方便查看这个类是为了那些功能 2.是成员小组中的谁负责编写的 根据自己的vs的安装路径找到类模板的位置:D:\Program Files (x86)\Microsof ...

  2. JMS - ActiveMQ集成Spring

    下面是ActiveMQ官网提供的文档.http://activemq.apache.org/spring-support.html 下面是我添加的一些dependency: <!-- jms a ...

  3. [javaSE] JDBC的批处理

    向数据库发送多条sql语句 create database batch use batch create table batch_table( id int primary key auto_incr ...

  4. ajax上传数据

    ---恢复内容开始--- ajax上传数据,(简洁版) 1.上传普通同表单标签内容. 1.获取表单的内容 1. var file=$('#file').val();(放在点击事件后面) 2. var ...

  5. Cardinality Estimation算法学习(二)(Linear Counting算法、最大似然估计(MLE))

    在上篇,我了解了基数的基本概念,现在进入Linear Counting算法的学习. 理解颇浅,还请大神指点! http://blog.codinglabs.org/articles/algorithm ...

  6. python caser运行编码

    #!/usr/bin/env python# -*- coding:utf-8 -*-import os def encryption(): str_raw = raw_input("请输入 ...

  7. js内存空间详细图解-笔记

    原文参考http://mp.weixin.qq.com/s/NGqdjhoU3MR9LD0yH6tKIw 栈-先进后出堆-类比成书于书架(形象),只要知道Key就可以找到value 基础数据类型(Un ...

  8. 本地存储localStroage的用法及示例

    localStorage是HTML5在在客户端存储数据的新方法,存储的数据没有时间限制. localStorage的主要API: localStorage.setItem(key,value);   ...

  9. Storm Flow

    A Stream represents the core data model in Trident, and can be thought of as a "stream" of ...

  10. OpenCV 小图重叠至大图指定位置

    Android OpenCV Java: Codes: smallImg.copyTo( bigImg.submat( y, smallImg.rows(), x, smallImg.cols() ) ...