Spring整合Shiro
apache shiro 是一个安全认证框架,和 spring security 相比,在于他使用了比较简洁易懂的 认证和授权方式。其提供的 native-session(即把用户认证后的授权信息保存在其自身提供 Session 中)机制,这样就可以和 HttpSession、EJB Session Bean 的基于容器的 Session 脱耦,到和客户端应用、Flex 应用、远程方法调用等都可以使用它来配置权限认证。 在 exit-web-framework 里的 vcs-admin 例子用到该框架,具体使用说明可以参考官方帮助文档。 在这里主要讲解如何与 spring 结合、动态创建 filterchaindefinitions、以及认证、授权、和 缓存处理。
核心功能:认证、授权、加密、会话管理
执行流程:

Application Code:应用程序代码,程序员编写的代码
Subject:接口,代表当前用户,由shiro框架提供的
SecurityManager:安全管理器,由shiro框架提供
Realm:用于操作安全数据(用户、权限、角色等)的,类似于DAO,shiro框架提供,业可以自己编写
- apache shiro 结合 spring(登录shiro应用)
第一步:在pom.xml中引入shiro的坐标:
<!-- 权限控制 框架 -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-all</artifactId>
<version>${shiro.version}</version>
</dependency>
第二步:在web.xml中配置整合shiro的过滤器
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
第三步:在spring容器中配置一个bean,id和上面的过滤器name相同
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 注入安全管理器对象 -->
<property name="securityManager" ref="securityManager"/>
<!--
private String loginUrl;
private String successUrl;
private String unauthorizedUrl;
-->
<property name="loginUrl" value="/login.html"/>
<property name="successUrl" value="/index.html"/>
<property name="unauthorizedUrl" value="/unauthorizedUrl.html"></property>
<!-- 第一种权限控制方式:url拦截实现权限控制 -->
<property name="filterChainDefinitions">
<!--
authc:代表shiro框架提供的一个过滤器,作用是用于检查当前用户是否已经完成登录(认证)
如果已经完成登录,就放行,如果没有完成登录,跳转到登录页面
anon:代表框架提供的一个过滤器,作用是可以匿名(未登录)访问
-->
<value>
/login.html = anon
/js/** = anon
/css/** = anon
/images/** = anon
/validatecode.jsp* = anon
/userAction_login.action = anon
/** = authc
</value>
</property>
</bean>
第四步:配置安全管理器
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"></bean>
第五步:提供UserAction,创建login方法
package cn.itcast.bos.web.action;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import cn.mytest.bos.domain.User;
import cn.mytest.bos.service.IUserService;
import cn.mytest.bos.utils.MD5Utils;
@ParentPackage("struts-default")
@Namespace("/")
@Actions
@Controller
@Scope("prototype")
public class UserAction extends BaseAction<User> {
@Resource
private IUserService userService;
private String checkcode;
public void setCheckcode(String checkcode) {
this.checkcode = checkcode;
}
@Action(value = "userAction_login", results = {
@Result(name = "success", location = "/index.jsp", type = "redirect"),
@Result(name = "login", location = "/login.jsp", type = "redirect") })
public String login() {
String key = (String) ServletActionContext.getRequest().getSession().getAttribute("key");
if (StringUtils.isNotBlank(checkcode) && checkcode.equals(key)) {
Subject subject = SecurityUtils.getSubject();
AuthenticationToken token = new UsernamePasswordToken(model.getUsername(),
MD5Utils.md5(model.getPassword()));
try {
subject.login(token);
return "success";
} catch (Exception e) {
e.printStackTrace();
return "login";
}
} else {
return "login";
}
}
}
第六步:自定义一个realm
package cn.itcast.bos.myrealm;
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 cn.mytest.bos.domain.User;
import cn.mytest.bos.service.IUserService;
public class MyRealm extends AuthorizingRealm {
@Autowired
private IUserService userservice;
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection paramPrincipalCollection) {
// TODO Auto-generated method stub
SimpleAuthorizationInfo sai = new SimpleAuthorizationInfo();
sai.addStringPermission("courier");
return sai;
}
/*
* 认证
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken paramAuthenticationToken)
throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) paramAuthenticationToken;
String username = token.getUsername();
User us = userservice.findByUsername(username);
if (null != us) {
return new SimpleAuthenticationInfo(us, us.getPassword(), this.getName());
}
return null;
}
}
提供UserDao接口:
package cn.itcast.bos.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import cn.mytest.bos.domain.User;
public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecificationExecutor<User>{
User findByUsername(String username);
}
第七步配置spring 配置文件
<!-- 注册安全管理器 -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="bosLoginRealm" />
</bean>
<!-- 配置自定义realm -->
<bean id="bosLoginRealm" class="cn.itcast.bos.myrealm.MyRealm" />
Spring整合Shiro的更多相关文章
- Spring整合Shiro做权限控制模块详细案例分析
1.引入Shiro的Maven依赖 <!-- Spring 整合Shiro需要的依赖 --> <dependency> <groupId>org.apache.sh ...
- Spring整合Shiro并扩展使用EL表达式
Shiro是一个轻量级的权限控制框架,应用非常广泛.本文的重点是介绍Spring整合Shiro,并通过扩展使用Spring的EL表达式,使@RequiresRoles等支持动态的参数.对Shiro的介 ...
- Spring 整合 Shiro
一.引入依赖 <!-- spring start --> <dependency> <groupId>org.springframework</groupId ...
- Spring整合Shiro 权限 角色 用户关系分析
Spring整合Shiro 权限 角色 用户关系分析 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 前置内容 之前我们学习了,使用注解的方式去完成权限的控制,当然,也是静态的,也就 ...
- spring整合shiro框架
上一篇文章已经对shiro框架做了一定的介绍,这篇文章讲述使用spring整合shiro框架,实现用户认证已经权限控制 1.搭建环境 这里不在赘述spring环境的搭建,可以简单的搭建一个ssm框架, ...
- 【shiro】2.spring整合shiro,注解控制shiro用户/角色/权限And/OR,没有权限跳转到固定页面
这几天粗浅的把shiro整合到spring中,并且注解控制shiro用户/角色/权限And/OR 步骤: 1.首先maven搭建web项目 2.创建数据库 user/role/authority 其中 ...
- 【原】Spring整合Shiro基础搭建[3]
1.前言 上个Shiro Demo基础搭建是基于官方的快速入门版本,没有集成其他框架,只是简单的通过Main方法来执行Shiro工作流程,并测试一下比较核心的函数:但在企业开发中一般都会集成Sprin ...
- spring整合shiro配置BUG,Tomcat启动不了:Error during artifact deployment. See server log for details
现象 spring配置shiro权限控制之后,项目无法启动 [2019-08-09 09:00:35,800] Artifact export_web_manager:war exploded: Er ...
- spring 整合 shiro框架
shiro是用来干嘛的?从它的官网上(http://shiro.apache.org/)基本可以了解到,她主要提供以下功能: (1)Authentication(认证) (2)Authorizatio ...
随机推荐
- bat、sh等批处理文件(脚本文件)
批处理文件(batch file):也被称为批处理程序或脚本,可以简化日常或重复性任务.本质是无格式的文本文件,它包含一条或多条命令.(1).bat是dos下的批处理文件,在window系统上执行的文 ...
- django基础-01:软件框架,MVC框架,MVT
1. 软件框架 一个公司是由公司中的各部部门来组成的,每一个部门拥有特定的职能,部门与部门之间通过相互的配合来完成让公司运转起来. 一个软件框架是由其中各个软件模块组成的,每一个模块都有特定的功能,模 ...
- Laravel上传产品图片Uploading img
这节我们讲Laravel产品图片上传,有很多方式可以实现,这里我们用intervention/image插件来进行.首先安装intervention/image插件,在命令行输入 composer r ...
- 6种innodb数据字典恢复方法
6种innodb数据字典恢复方法 https://dev.mysql.com/doc/refman/5.7/en/innodb-troubleshooting-datadict.html frm文件重 ...
- Got timeout reading communication packets解决方法
Got timeout reading communication packets解决方法 http://www.th7.cn/db/mysql/201702/225243.shtml [Note] ...
- 各版本系统安装tesseract-ocr
Mac版本 1.tesseract-ocr安装 brew install tesseract-ocr 注意:如果未安装brew命令,可以输入命令: brew官网:http://brew.sh /us ...
- 我是怎么走上python这条路的
看看时间,此刻是零点43分,写了几十行代码,看了3个小时关于Django的视频,连续两个多月的坚持,突然想停下来,想想,感觉挺搞笑的... 为什么学python?我终于正式的问了自己这个问题,我想拿个 ...
- vue中常用的两中页面刷新的方式和页面回退
这个方法的参数是一个整数,意思是在 history 记录中向前或者后退多少步,类似 window.history.go(n) router.push(location) 想要导航到不同的 URL,则使 ...
- [py]super调用父类的方法---面向对象
super()用于调用父类方法 http://www.runoob.com/python/python-func-super.html super() 函数是用于调用父类(超类)的一个方法. clas ...
- powerdesign连接Oracle&Mysql
Oracle部分 想用powerDesign,需要用到oracle数据库,记录配置过程 1,安装win64_11gR2_client,选择安装方式为管理员,按默认选安装,过程大概几分钟就好 2,配置客 ...